Skip to content

Commit 2f5109c

Browse files
author
Spacehuhn
committed
Pause and unpause CLI
1 parent be0eb41 commit 2f5109c

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,16 @@ Here is a plain overview of all classes and their methods:
298298
```c++
299299
SimpleCLI(int commandQueueSize = 10, int errorQueueSize = 10);
300300

301+
void pause();
302+
void unpause();
303+
301304
void parse(String& input);
302305
void parse(const char* input);
303306
void parse(const char* input, size_t input_len);
304307

305308
bool available() const;
306309
bool errored() const;
310+
bool paused() const;
307311

308312
int countCmdQueue() const;
309313
int countErrorQueue() const;

src/SimpleCLI.cpp

+42-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@ SimpleCLI::~SimpleCLI() {
2020
cmd_error_destroy_rec(errorQueue);
2121
}
2222

23+
void SimpleCLI::pause() {
24+
pauseParsing = true;
25+
}
26+
27+
void SimpleCLI::unpause() {
28+
pauseParsing = false;
29+
30+
// Go through queued errors
31+
while (onError && errored()) {
32+
onError(getError().getPtr());
33+
}
34+
35+
// Go through queued commands
36+
if(available()) {
37+
cmd* prev = NULL;
38+
cmd* current = cmdQueue;
39+
cmd* next = NULL;
40+
41+
while(current) {
42+
next = current->next;
43+
44+
// Has callback, then run it and remove from queue
45+
if(current->callback) {
46+
current->callback(current);
47+
if(prev) prev->next = next;
48+
cmd_destroy(current);
49+
} else {
50+
prev = current;
51+
}
52+
53+
current = next;
54+
}
55+
}
56+
}
57+
2358
void SimpleCLI::parse(const String& input) {
2459
parse(input.c_str(), input.length());
2560
}
@@ -45,15 +80,15 @@ void SimpleCLI::parse(const char* str, size_t len) {
4580

4681
// When parsing was successful
4782
if (e->mode == CMD_PARSE_SUCCESS) {
48-
if (h->callback) h->callback(h);
83+
if (h->callback && !pauseParsing) h->callback(h);
4984
else cmdQueue = cmd_push(cmdQueue, cmd_move(h), commandQueueSize);
5085

5186
success = true;
5287
}
5388

5489
// When command name matches but something else went wrong, exit with error
5590
else if (e->mode > CMD_NOT_FOUND) {
56-
if (onError) {
91+
if (onError && !pauseParsing) {
5792
onError(e);
5893
} else {
5994
errorQueue = cmd_error_push(errorQueue, cmd_error_copy(e), errorQueueSize);
@@ -79,7 +114,7 @@ void SimpleCLI::parse(const char* str, size_t len) {
79114
if (!errored && !success) {
80115
cmd_error* e = cmd_error_create_not_found(NULL, n->words->first);
81116

82-
if (onError) {
117+
if (onError && !pauseParsing) {
83118
onError(e);
84119
} else {
85120
errorQueue = cmd_error_push(errorQueue, cmd_error_copy(e), errorQueueSize);
@@ -104,6 +139,10 @@ bool SimpleCLI::errored() const {
104139
return errorQueue;
105140
}
106141

142+
bool SimpleCLI::paused() const {
143+
return pauseParsing;
144+
}
145+
107146
int SimpleCLI::countCmdQueue() const {
108147
cmd* h = cmdQueue;
109148
int i = 0;

src/SimpleCLI.h

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class SimpleCLI {
1818
private:
1919
bool caseSensetive { false };
20+
bool pauseParsing { false };
2021

2122
cmd* cmdList { NULL }; // List of accessible commands
2223
cmd* cmdQueue { NULL }; // Queue with parsed commands the user has typed in
@@ -36,12 +37,16 @@ class SimpleCLI {
3637
SimpleCLI(int commandQueueSize = 10, int errorQueueSize = 10);
3738
~SimpleCLI();
3839

40+
void pause();
41+
void unpause();
42+
3943
void parse(const String& input);
4044
void parse(const char* input);
4145
void parse(const char* input, size_t input_len);
4246

4347
bool available() const;
4448
bool errored() const;
49+
bool paused() const;
4550

4651
int countCmdQueue() const;
4752
int countErrorQueue() const;

0 commit comments

Comments
 (0)