Skip to content

Commit 7922e8e

Browse files
Allow exiting a watch loop when a command returns the specified code
1 parent e3f4249 commit 7922e8e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/builtins.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ static int slash_builtin_watch(struct slash *slash)
115115

116116
unsigned int interval = 1000;
117117
unsigned int count = 0;
118+
int exit = 0xFFFFFF;
118119

119120
optparse_t * parser = optparse_new("watch", "<command...>");
120121
optparse_add_help(parser);
121122
optparse_add_unsigned(parser, 'n', "interval", "NUM", 0, &interval, "interval in milliseconds (default = <env timeout>)");
122123
optparse_add_unsigned(parser, 'c', "count", "NUM", 0, &count, "number of times to repeat (default = infinite)");
124+
optparse_add_unsigned(parser, 'e', "exit", "NUM", 0, &exit, "stop watch when command returns the given exit code (default = ignore exit code completely)");
123125

124126
int argi = optparse_parse(parser, slash->argc - 1, (const char **) slash->argv + 1);
125127
if (argi < 0) {
@@ -137,7 +139,7 @@ static int slash_builtin_watch(struct slash *slash)
137139
}
138140

139141
printf("Executing \"%s\" each %u ms - press <enter> to stop\n", line, interval);
140-
142+
int cmd_exit_code;
141143
while(1) {
142144

143145
/* Make another copy, since slash_exec will modify this */
@@ -149,7 +151,12 @@ static int slash_builtin_watch(struct slash *slash)
149151
clock_gettime(CLOCK_MONOTONIC, &time_before);
150152

151153
/* Execute command */
152-
slash_execute(slash, cmd_exec);
154+
cmd_exit_code = slash_execute(slash, cmd_exec);
155+
if(exit != 0xFFFFFF) {
156+
if(exit == cmd_exit_code) {
157+
break;
158+
}
159+
}
153160

154161
clock_gettime(CLOCK_MONOTONIC, &time_after);
155162

0 commit comments

Comments
 (0)