main.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* main.cpp: parses options, runs slop, prints results.
  2. *
  3. * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
  4. *
  5. * This file is part of Slop.
  6. *
  7. * Slop is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Slop is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Slop. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <iostream>
  21. #include <sstream>
  22. #include "slop.hpp"
  23. #include "options.hpp"
  24. SlopOptions* getOptions( Options& options ) {
  25. SlopOptions* foo = new SlopOptions();
  26. options.getFloat("bordersize", 'b', foo->borderSize);
  27. options.getFloat("padding", 'p', foo->padding);
  28. options.getFloat("tolerance", 't', foo->tolerance);
  29. glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
  30. options.getColor("color", 'c', color);
  31. foo->r = color.r;
  32. foo->g = color.g;
  33. foo->b = color.b;
  34. foo->a = color.a;
  35. options.getBool("highlight", 'h', foo->highlight);
  36. options.getBool("nodecorations", 'n', foo->nodecorations);
  37. return foo;
  38. }
  39. std::string formatOutput( std::string input, SlopSelection selection ) {
  40. std::stringstream output;
  41. for( unsigned int i=0;i<input.length();i++) {
  42. if ( input[i] == '%' ) {
  43. if ( input.length() <= i+1 ) {
  44. throw new std::invalid_argument( "Expected character after `%`, got END." );
  45. }
  46. switch( input[i+1] ) {
  47. case 'x':
  48. case 'X': output << round(selection.x); break;
  49. case 'y':
  50. case 'Y': output << round(selection.y); break;
  51. case 'w':
  52. case 'W': output << round(selection.w); break;
  53. case 'h':
  54. case 'H': output << round(selection.h); break;
  55. case 'g':
  56. case 'G': output << round(selection.w) << "x" << round(selection.h)
  57. << "+" << round(selection.x) << "+" << round(selection.y); break;
  58. case 'i':
  59. case 'I': output << selection.id; break;
  60. case '%': output << "%"; break;
  61. default: throw new std::invalid_argument( std::string()+"Expected x, y, w, h, g, i, or % after % in format. Got `" + input[i+1] + "`." );
  62. }
  63. i++;
  64. continue;
  65. }
  66. output << input[i];
  67. }
  68. return output.str();
  69. }
  70. int app( int argc, char** argv ) {
  71. // Options just validates all of our input from argv
  72. Options options( argc, argv );
  73. // We then parse the options into something slop can understand.
  74. SlopOptions* parsedOptions = getOptions( options );
  75. // We want to validate our format option if we got one, we do that by just doing a dry run
  76. // on a fake selection.
  77. SlopSelection selection(0,0,0,0,0);
  78. std::string format;
  79. bool gotFormat = options.getString("format", 'f', format);
  80. if ( gotFormat ) {
  81. formatOutput( format, selection );
  82. }
  83. // Finally we do the real selection.
  84. bool cancelled = false;
  85. selection = SlopSelect(parsedOptions, &cancelled);
  86. // Here we're done with the parsed option data.
  87. delete parsedOptions;
  88. // We know if we cancelled or not
  89. if ( cancelled ) {
  90. std::cerr << "Selection was cancelled by keystroke or right-click.\n";
  91. return 1;
  92. }
  93. // If we recieved a format option, we output the specified output.
  94. if ( gotFormat ) {
  95. std::cout << formatOutput( format, selection );
  96. return 0;
  97. }
  98. // Otherwise we default to an `eval` compatible format.
  99. std::cout << formatOutput( "X=%x\nY=%y\nW=%w\nH=%h\nG=%g\nID=%i\n", selection );
  100. return 0;
  101. }
  102. int main( int argc, char** argv ) {
  103. try {
  104. return app( argc, argv );
  105. } catch( std::exception* e ) {
  106. std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
  107. return 1;
  108. } // let the operating system handle any other kind of exception.
  109. return 1;
  110. }