Add -e flag to explain what the previous smiley face means. This

requires us to also know what the previous return signal is, so
if XD is given a return code, it passes it through when finishing
execution.
This commit is contained in:
2025-02-18 14:29:09 -06:00
parent 6c099e3648
commit 6025b2d832
4 changed files with 98 additions and 10 deletions

View File

@ -2,7 +2,7 @@ include config.mk
# flags and incs # flags and incs
PKGS = $(GITLIB) PKGS = $(GITLIB)
CFLAGS = -DVERSION=\"$(VERSION)\" -Wall -pedantic -O3 $(GIT) $(ERR) CFLAGS = -DVERSION=\"$(VERSION)\" -Wall -pedantic -O3 $(GIT) $(ERR) $(EXPLAIN)
LIBS = `$(PKG_CONFIG) --libs --cflags $(PKGS)` LIBS = `$(PKG_CONFIG) --libs --cflags $(PKGS)`
all: XD all: XD

40
XD.1
View File

@ -1,4 +1,4 @@
.Dd December 19, 2024 .Dd Febuary 18, 2025
.Dt XD 1 .Dt XD 1
.Sh NAME .Sh NAME
.Nm XD .Nm XD
@ -6,7 +6,9 @@
.Os .Os
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl v|number .Op Fl v
.Op Fl e
.Op number
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
Displays information using a smiley face like so: :) Displays information using a smiley face like so: :)
@ -40,3 +42,37 @@ c;l.
/;command not found /;command not found
(;previous signal is failure (;previous signal is failure
.TE .TE
.Sh OPTIONS
.Ss -v
Print version information to stdout and exit.
.Ss -e
Explain the output instead of putting a smiley face.
.Sh EXAMPLES
Running
.Nm
and passing in the previous return code ($?) results in a smiley face, if you
then follow that by \fBXD -e $?\fR then you will get an explination of what the
face is explaining.
$ \fBXD $?\fR
.br
:)
.br
$ \fBXD -e $?\fR
.br
Not in a git repository.
.br
Return code of 0, no errors.
$ \fBXD $?\fR
.br
;*O
.br
$ \fBXD -e $?\fR
.br
In a git repository.
.br
The're staged changes.
.br
Return code of 130, ctrl-c was pressed, or SIGTERM sent.

57
XD.c
View File

@ -1,7 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef ERR #include <ctype.h>
#if defined(ERR) || defined(EXPLAIN)
#include <stdarg.h> #include <stdarg.h>
#endif #endif
@ -13,7 +15,7 @@
#define P(X) fwrite(X, 1, 1, stdout) #define P(X) fwrite(X, 1, 1, stdout)
#ifdef ERR #if defined(ERR) || defined(EXPLAIN)
void void
l(const char *fmt, ...) l(const char *fmt, ...)
{ {
@ -30,11 +32,23 @@ l(const char *fmt, ...)
fputc('\n', stderr); fputc('\n', stderr);
} }
} }
#endif
#ifdef ERR
#define L(...) l(__VA_ARGS__) #define L(...) l(__VA_ARGS__)
#else #else
#define L(...) #define L(...)
#endif #endif
#ifdef EXPLAIN
static int explain = 0;
#define E(...) if (explain) { \
l(__VA_ARGS__); \
} else
#else
#define E(...)
#endif
#ifdef GIT #ifdef GIT
/** /**
* @brief search all parent directories for a git repo * @brief search all parent directories for a git repo
@ -225,6 +239,10 @@ main(int argc, char *argv[])
if (argc > 1 && strcmp(argv[1], "-v") == 0) { if (argc > 1 && strcmp(argv[1], "-v") == 0) {
printf("XD [number] v%s\n", VERSION); printf("XD [number] v%s\n", VERSION);
return 0; return 0;
#ifdef EXPLAIN
} else if (argc > 1 && strcmp(argv[1], "-e") == 0) {
explain = 1;
#endif
} }
#ifdef GIT #ifdef GIT
@ -233,19 +251,25 @@ main(int argc, char *argv[])
if ((repo = init_git())) { if ((repo = init_git())) {
/* change the eyes depending on the current git repo's status */ /* change the eyes depending on the current git repo's status */
if (has_stashes(repo)) { if (has_stashes(repo)) {
E("The current git repo has stashed changes.")
P("8"); /* goggle eyes if we have some stashed changes */ P("8"); /* goggle eyes if we have some stashed changes */
} else if (git_repository_is_empty(repo)) { } else if (git_repository_is_empty(repo)) {
E("This is a new git repo.")
P("B"); /* sunglasses if we're in a new repo with no HEAD */ P("B"); /* sunglasses if we're in a new repo with no HEAD */
} else { } else {
E("In a git repository.")
P(";"); /* wink when we're in a git repo */ P(";"); /* wink when we're in a git repo */
} }
/* change the nose depending on the current git repo's status */ /* change the nose depending on the current git repo's status */
if (has_staged(repo)) { if (has_staged(repo)) {
E("The're staged changes.")
P("*"); /* change to broken nose for staged changes */ P("*"); /* change to broken nose for staged changes */
} else if (has_untracked(repo)) { } else if (has_untracked(repo)) {
E("There are untracked changes in the repository.")
P("^"); /* add a little nose when there are untracked changes in the repo */ P("^"); /* add a little nose when there are untracked changes in the repo */
} else if (git_repository_head_detached(repo)) { } else if (git_repository_head_detached(repo)) {
E("The HEAD is detached.")
P("-"); /* add a minus nose when the HEAD is detached */ P("-"); /* add a minus nose when the HEAD is detached */
} }
git_repository_free(repo); git_repository_free(repo);
@ -253,6 +277,7 @@ main(int argc, char *argv[])
} else } else
#endif #endif
if (1) { if (1) {
E("Not in a git repository.")
P(":"); P(":");
} }
@ -264,13 +289,31 @@ main(int argc, char *argv[])
/* change mouth based on exit code */ /* change mouth based on exit code */
if (code >= 0) { if (code >= 0) {
switch (code) { switch (code) {
case 0: P(")"); break; /* all good */ case 0: /* all good */
case 130: P("O"); break; /* Ctrl-c pressed (SIGTERM) */ E("Return code of %d, no errors.", code)
case 126: P("P"); break; /* permission denied */ P(")");
case 127: P("/"); break; /* command not found */ break;
default: P("("); break; /* all other codes (usually the program saying it has failed) */ case 130: /* Ctrl-c pressed (SIGTERM) */
E("Return code of %d, ctrl-c was pressed, or SIGTERM sent.", code)
P("O");
break;
case 126: /* permission denied */
E("Return code of %d, permission denied.", code)
P("P");
break;
case 127: /* command not found */
E("Return code of %d, command not found.", code)
P("/");
break;
default: /* all other codes (usually the program saying it has failed) */
E("Return code of %d, probably an error with the program.", code)
P("(");
break;
} }
} else { } else {
E("No code information passed in.")
P("|"); /* no code info */ P("|"); /* no code info */
} }
return code;
} }

View File

@ -16,6 +16,10 @@ ERR =
# uncomment to enable errors # uncomment to enable errors
# ERR = -DERR # ERR = -DERR
EXPLAIN =
# comment to disable explinations
EXPLAIN = -DEXPLAIN
# add compilation details to VERSION variable # add compilation details to VERSION variable
ifneq ($(GIT),) ifneq ($(GIT),)
VERSION := $(VERSION)"\\nlibgit2 "`$(PKG_CONFIG) --modversion $(GITLIB)` VERSION := $(VERSION)"\\nlibgit2 "`$(PKG_CONFIG) --modversion $(GITLIB)`
@ -25,5 +29,10 @@ ifeq ($(ERR),)
else else
VERSION := $(VERSION)"\\nerrors enabled" VERSION := $(VERSION)"\\nerrors enabled"
endif endif
ifeq ($(EXPLAIN),)
VERSION := $(VERSION)"\\nexplinations disabled"
else
VERSION := $(VERSION)"\\nexplinations enabled"
endif
CC = cc CC = cc