12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <iostream>
  2. #include <sstream>
  3. #include "slop.hpp"
  4. #include "options.hpp"
  5. SlopOptions* getOptions( Options& options ) {
  6. SlopOptions* foo = new SlopOptions();
  7. options.getFloat("bordersize", 'b', foo->borderSize);
  8. options.getFloat("padding", 'p', foo->padding);
  9. glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
  10. options.getColor("color", 'c', color);
  11. foo->r = color.r;
  12. foo->g = color.g;
  13. foo->b = color.b;
  14. foo->a = color.a;
  15. options.getBool("highlight", 'h', foo->highlight);
  16. return foo;
  17. }
  18. std::string formatOutput( std::string input, SlopSelection selection ) {
  19. std::stringstream output;
  20. for( unsigned int i=0;i<input.length();i++) {
  21. if ( input[i] == '%' ) {
  22. if ( input.length() <= i+1 ) {
  23. throw new std::invalid_argument( "Expected character after `%`, got END." );
  24. }
  25. switch( input[i+1] ) {
  26. case 'x':
  27. case 'X': output << round(selection.x); break;
  28. case 'y':
  29. case 'Y': output << round(selection.y); break;
  30. case 'w':
  31. case 'W': output << round(selection.w); break;
  32. case 'h':
  33. case 'H': output << round(selection.h); break;
  34. case 'g':
  35. case 'G': output << round(selection.w) << "x" << round(selection.h)
  36. << "+" << round(selection.x) << "+" << round(selection.y); break;
  37. case '%': output << "%"; break;
  38. default: throw new std::invalid_argument( std::string()+"Expected x, y, w, h, g, or % after % in format. Got `" + input[i+1] + "`." );
  39. }
  40. i++;
  41. continue;
  42. }
  43. output << input[i];
  44. }
  45. return output.str();
  46. }
  47. int app( int argc, char** argv ) {
  48. // Options just validates all of our input from argv
  49. Options options( argc, argv );
  50. // We then parse the options into something slop can understand.
  51. SlopOptions* parsedOptions = getOptions( options );
  52. // We want to validate our format option if we got one, we do that by just doing a dry run
  53. // on a fake selection.
  54. SlopSelection selection(0,0,0,0);
  55. std::string format;
  56. bool gotFormat = options.getString("format", 'f', format);
  57. if ( gotFormat ) {
  58. formatOutput( format, selection );
  59. }
  60. // Finally we do the real selection.
  61. bool cancelled = false;
  62. selection = SlopSelect(parsedOptions, &cancelled);
  63. // Here we're done with the parsed option data.
  64. delete parsedOptions;
  65. // We know if we cancelled or not
  66. if ( cancelled ) {
  67. std::cerr << "Selection was cancelled by keystroke or right-click.\n";
  68. return 1;
  69. }
  70. // If we recieved a format option, we output the specified output.
  71. if ( gotFormat ) {
  72. std::cout << formatOutput( format, selection );
  73. return 0;
  74. }
  75. // Otherwise we default to an `eval` compatible format.
  76. std::cout << formatOutput( "X=%x\nY=%y\nW=%w\nH=%h\nG=%g\n", selection );
  77. return 0;
  78. }
  79. int main( int argc, char** argv ) {
  80. try {
  81. return app( argc, argv );
  82. } catch( std::exception* e ) {
  83. std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
  84. return 1;
  85. } // let the operating system handle any other kind of exception.
  86. return 1;
  87. }