Skip to content

Commit

Permalink
fix - Fixed add function for KeyMapNode
Browse files Browse the repository at this point in the history
Allowed an existing KeyMapNode without a command to have a command

When adding a command, if the corresponding string exists but doesn't have a
command, then its `cmd` and `hasCmd` variables are updated
  • Loading branch information
OZoneGuy committed Jan 10, 2021
1 parent 74489bc commit 7da8094
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/headers/key_map_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ struct KeyMapNode {
* The key of this node.
*/
const char key;

public:
/**
* Shows whether or not this node is a command node/has a command
*/
const bool hasCmd;
bool hasCmd;
/**
* The command associated with the node, could be NULL/empty.
*/
const std::string cmd;
std::string cmd;

public:

/**
* @brief KeyMapNode constructor without a command
Expand All @@ -51,6 +51,10 @@ struct KeyMapNode {
*/
KeyMapNode(const char &key, const std::string &cmd);

bool has_cmd() const;

std::string get_cmd() const;

/**
* @brief Returns true if the next 'key' sequence has a command
*
Expand Down
15 changes: 14 additions & 1 deletion src/key_map_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ void KeyMapNode::add(const string &keySeq, const string &cmd) {
if (keySeq.size() == 1) {
if (has(keySeq[0]))
throw invalid_argument("Key string command already defined");
nextKeys.emplace(keySeq[0], new KeyMapNode(keySeq[0], cmd));
else {
auto search = nextKeys.find(keySeq[0]);
if (search == nextKeys.end())
nextKeys.emplace(keySeq[0], new KeyMapNode(keySeq[0], cmd));
else {
KeyMapNode *node = search->second;
node->cmd = cmd;
node->hasCmd = true;
}
}
} else {
auto search = nextKeys.find(keySeq[0]);
if (search == nextKeys.end()) {
Expand All @@ -42,3 +51,7 @@ KeyMapNode* KeyMapNode::get(const char &key) {
else
return search->second;
}

bool KeyMapNode::has_cmd() const { return hasCmd; }

string KeyMapNode::get_cmd() const { return cmd; }
4 changes: 2 additions & 2 deletions src/key_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void handle_keyboard(KeyMapNode *keyMap) {
if (curNode != nullptr) {

// if it has a command, execute command
if (curNode->hasCmd)
if (curNode->has_cmd())
// execute shell command as a new process
system(("setsid -f " + curNode->cmd).c_str());
system(("setsid -f " + curNode->get_cmd()).c_str());
// if there are no more possible keys, stop
if (curNode->empty())
cont = false;
Expand Down

0 comments on commit 7da8094

Please sign in to comment.