Skip to content

Add --slow option #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Options:

-q, --quiet only output stderr
-x, --halt halt on failure
-i, --interval <n> interval in seconds or ms defaulting to 1
-i, --interval <n> interval in seconds, 1 is default
-s, --slow pause x10 longer while command fails (useful for reading error output without flicker)
-v, --version output version number

```
Expand All @@ -35,7 +36,7 @@ $ PREFIX=~ make install
This project is very similar to original [watch(1)](http://linux.die.net/man/1/watch) implemented in 1991, differences include:

- ansi escape sequences (colors etc)
- terminal is not cleared
- terminal is not cleared (just add "clear &&" before your command if you want this!)
- lower default interval of 1s
- millisecond interval resolution

Expand Down Expand Up @@ -134,4 +135,4 @@ clean:
.PHONY: clean
```

The one missing component is periodical action, which is where `watch(1)` or similar utilities come in, this functionality coupled with Make as a build system creates a powerful duo.
The one missing component is periodical action, which is where `watch(1)` or similar utilities come in, this functionality coupled with make as a build system creates a powerful duo.
69 changes: 51 additions & 18 deletions src/watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ static int quiet = 0;

static int halt = 0;

/*
* Slow on failure.
*/

static int slow = 0;

/*
* 1 if last execution failed, 0 otherwise
*/
static int failed = 0;

/*
* Output command usage.
*/
Expand All @@ -58,6 +69,7 @@ usage() {
"\n"
" -q, --quiet only output stderr\n"
" -x, --halt halt on failure\n"
" -s, --slow slow down on failure\n"
" -i, --interval <n> interval in seconds or ms defaulting to 1\n"
" -v, --version output version number\n"
" -h, --help output this help information\n"
Expand Down Expand Up @@ -115,6 +127,18 @@ length(char **strs) {
return n + 1;
}

/*
* Calculate how long to sleep depending on slow flag
*/
int
wait_time(int interval) {
if (!slow)
return interval;
if (failed)
return interval * 10;
return interval;
}

/*
* Join the given `strs` with `val`.
*/
Expand Down Expand Up @@ -163,6 +187,12 @@ main(int argc, const char **argv){
continue;
}

// -s, --slow
if (option("-s", "--slow", arg)) {
slow = 1;
continue;
}

// -v, --version
if (option("-v", "--version", arg)) {
printf("%s\n", VERSION);
Expand All @@ -173,15 +203,15 @@ main(int argc, const char **argv){
if (option("-i", "--interval", arg)) {
if (argc-1 == i) {
fprintf(stderr, "\n --interval requires an argument\n\n");
exit(1);
exit(1);
}

arg = argv[++i];
char last = arg[strlen(arg) - 1];
// seconds or milliseconds
interval = last >= 'a' && last <= 'z'
? string_to_milliseconds(arg)
: atoi(arg) * 1000;
? string_to_milliseconds(arg)
: atoi(arg) * 1000;
continue;
}

Expand All @@ -191,7 +221,7 @@ main(int argc, const char **argv){
exit(1);
}

arg:
arg:
args[len++] = (char *) arg;
interpret = 0;
}
Expand All @@ -213,27 +243,30 @@ main(int argc, const char **argv){
switch (pid = fork()) {
// error
case -1:
perror("fork()");
exit(1);
perror("fork()");
exit(1);
// child
case 0:
if (quiet) redirect_stdout("/dev/null");
execvp(cmd[0], cmd);
if (quiet) redirect_stdout("/dev/null");
execvp(cmd[0], cmd);
// parent
default:
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid()");
exit(1);
}
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid()");
exit(1);
}

// exit > 0
if (WEXITSTATUS(status)) {
fprintf(stderr, "\033[90mexit: %d\33[0m\n\n", WEXITSTATUS(status));
if (halt) exit(WEXITSTATUS(status));
}
if (WEXITSTATUS(status)) {
fprintf(stderr, "\033[90mexit: %d\33[0m\n\n", WEXITSTATUS(status));
if (halt) exit(WEXITSTATUS(status));
failed = 1;
}
else
failed = 0;

mssleep(interval);
goto loop;
mssleep(wait_time(interval));
goto loop;
}
}

Expand Down