add boolean options: --debug and --dry-run v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 01 Apr 2021 17:54:46 +0200
branchv_0
changeset 5 dbf093b8b9ac
parent 4 e5baa07d6e60
child 6 3407386d1f60
add boolean options: --debug and --dry-run
bash-completion.sh
src/CLIParser.h
src/Configuration.h
src/X11Handler.h
--- a/bash-completion.sh	Tue Mar 30 20:26:08 2021 +0200
+++ b/bash-completion.sh	Thu Apr 01 17:54:46 2021 +0200
@@ -22,15 +22,17 @@
 	w2=${COMP_WORDS[COMP_CWORD-2]}
 	w3=${COMP_WORDS[COMP_CWORD-3]}
 
-	WRITE_HEADER=(
+	BOOLEAN_VALUES=(
 		"true"
 		"false"
 	)
 
-	if [[ "$w1" == "--TODO"                                            ]];    then COMPREPLY=($(compgen -W "${WRITE_HEADER[*]}" -- "$w0"))
+	  if [[ "$w1" == "--debug"                                          ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w1" == "--dry-run"                                        ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
 	else
 		OPTIONS=(
-			"--TODO"
+			"--debug"
+			"--dry-run"
 		)
 		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
 	fi
--- a/src/CLIParser.h	Tue Mar 30 20:26:08 2021 +0200
+++ b/src/CLIParser.h	Thu Apr 01 17:54:46 2021 +0200
@@ -48,7 +48,8 @@
 
 public:
 
-	static const relpipe::reader::string_t OPTION_WRITE_HEADER;
+	static const relpipe::reader::string_t OPTION_DEBUG;
+	static const relpipe::reader::string_t OPTION_DRY_RUN;
 
 	Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
 		Configuration c;
@@ -56,8 +57,10 @@
 		for (int i = 0; i < arguments.size();) {
 			relpipe::reader::string_t option = readNext(arguments, i);
 
-			if (option == OPTION_WRITE_HEADER) {
-				c.writeHeader = parseBoolean(readNext(arguments, i));
+			if (option == OPTION_DEBUG) {
+				c.debug = parseBoolean(readNext(arguments, i));
+			} else if (option == OPTION_DRY_RUN) {
+				c.dryRun = parseBoolean(readNext(arguments, i));
 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 		}
 
@@ -68,7 +71,8 @@
 	}
 };
 
-const relpipe::reader::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header";
+const relpipe::reader::string_t CLIParser::OPTION_DEBUG = L"--debug";
+const relpipe::reader::string_t CLIParser::OPTION_DRY_RUN = L"--dry-run";
 
 }
 }
--- a/src/Configuration.h	Tue Mar 30 20:26:08 2021 +0200
+++ b/src/Configuration.h	Thu Apr 01 17:54:46 2021 +0200
@@ -19,7 +19,7 @@
 #include <vector>
 #include <iostream>
 
-#include <relpipe/reader/typedefs.h>
+#include <relpipe/common/type/typedefs.h>
 
 
 namespace relpipe {
@@ -28,7 +28,8 @@
 
 class Configuration {
 public:
-	relpipe::reader::boolean_t writeHeader = true;
+	relpipe::common::type::Boolean debug = false;
+	relpipe::common::type::Boolean dryRun = false;
 
 	virtual ~Configuration() {
 	}
--- a/src/X11Handler.h	Tue Mar 30 20:26:08 2021 +0200
+++ b/src/X11Handler.h	Thu Apr 01 17:54:46 2021 +0200
@@ -130,14 +130,20 @@
 		}
 
 		attributeIndex++;
+		
+		bool debug = configuration.debug;
+		bool run = !configuration.dryRun;
 
 		if (attributeIndex % attributes.size() == 0) {
 			if (currentEvent.type == Event::Type::KEY) {
-				XTestFakeKeyEvent(display.display, currentEvent.key, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
+				if (debug) std::wcerr << L"KEY:    x = " << currentEvent.x << L" y = " << currentEvent.y << L"    key = " << currentEvent.key << L" state = " << (currentEvent.state == Event::State::PRESSED ? L"pressed" : L"released") << std::endl;
+				if (run) XTestFakeKeyEvent(display.display, currentEvent.key, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
 			} else if (currentEvent.type == Event::Type::BUTTON) {
-				XTestFakeButtonEvent(display.display, currentEvent.button, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
+				if (debug) std::wcerr << L"BUTTON: x = " << currentEvent.x << L" y = " << currentEvent.y << L" button = " << currentEvent.button << L" state = " << (currentEvent.state == Event::State::PRESSED ? L"pressed" : L"released") << std::endl;
+				if (run) XTestFakeButtonEvent(display.display, currentEvent.button, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
 			} else if (currentEvent.type == Event::Type::MOTION) {
-				XTestFakeMotionEvent(display.display, currentEvent.screen, currentEvent.x, currentEvent.y, currentEvent.delay);
+				if (debug) std::wcerr << L"MOTION: x = " << currentEvent.x << L" y = " << currentEvent.y << std::endl;
+				if (run) XTestFakeMotionEvent(display.display, currentEvent.screen, currentEvent.x, currentEvent.y, currentEvent.delay);
 			} else {
 				std::wcerr << L"Unsupported event" << std::endl;
 			}