Welcome to little lamb

Code » anopa » commit de437c9

Add aa-echo

author Olivier Brunel
2015-02-12 21:52:36 UTC
committer Olivier Brunel
2015-04-04 12:47:33 UTC
parent 53c960aed5132b6a903616ea43a2972ad05a03bb

Add aa-echo

Small helper to easily show anopa-style messages (e.g. from scripts)

.gitignore +1 -0
doc/aa-echo.pod +107 -0
package/modes +1 -0
package/targets.mak +4 -2
src/anopa/aa-echo.c +135 -0
src/anopa/deps-exe/aa-echo +2 -0

diff --git a/.gitignore b/.gitignore
index 2a68ecb..ae76f3b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,5 +12,6 @@
 /src/include/anopa/config.h
 /doc/*.1
 /libanopa.a
+/aa-echo
 /aa-enable
 /aa-start
diff --git a/doc/aa-echo.pod b/doc/aa-echo.pod
new file mode 100644
index 0000000..5b8ccdb
--- /dev/null
+++ b/doc/aa-echo.pod
@@ -0,0 +1,107 @@
+=head1 NAME
+
+aa-echo - Shows a message
+
+=head1 SYNOPSIS
+
+B<aa-enable> [B<-D>] [B<-T> | B<-t> | B<-w> | B<-e>] I<MESSAGE...>
+
+=head1 OPTIONS
+
+=over
+
+=item B<-D, --double-output>
+
+Enable double-output mode. Instead of using stdout for regular output, and
+stderr for warnings and errors, everything is sent both to stdout and stderr.
+This is intended to redirect stderr to a log file, so full output can be both
+shown on console and logged.
+
+=item B<-e, --error>
+
+Show I<MESSAGE...> as an error message. A red prefix "==> ERROR: " will be
+printed before the message.
+
+=item B<-h, --help>
+
+Show help screen and exit.
+
+=item B<-T, --title>
+
+Show I<MESSAGE...> as a main title. A green "==> " prefix will be printed before
+the message. This is the default, if no other option is used.
+
+=item B<-t, --title2>
+
+Show I<MESSAGE...> as a secondary title. A blue "  -> " prefix will be printed
+before the message.
+
+=item B<-V, --version>
+
+Show version information and exit.
+
+=item B<-w, --warning>
+
+Show I<MESSAGE...> as a warning. A yellow "==> WARNING: " prefix will be printed
+before the message.
+
+=back
+
+=head1 DESCRIPTION
+
+This is a little helper to easily print messages using the same color code as
+other B<anopa> tools do. Note that all arguments making up I<MESSAGE> are
+printed one after the other, without adding a blank space; In other words,
+`aa-echo foo bar` will result in the string "==> foobar" being printed.
+
+This is due to the way B<aa-echo>(1) processes its arguments, to allow you to
+set the text color. Any argument making up I<MESSAGE> can indeed be one of:
+
+=over
+
+=item B<+g, +green>
+
+Set color to green.
+
+=item B<+b, +blue>
+
+Set color to blue.
+
+=item B<+y, +yellow>
+
+Set color to yellow.
+
+=item B<+r, +red>
+
+Set color to red.
+
+=item B<+n, +normal>
+
+Reset color to "normal" aka white.
+
+=item B<++TEXT>
+
+To print +TEXT
+
+=back
+
+For example: `aa-echo -w "The file " +r "/foo/bar" +n " doesn't exist"`
+
+=head1 BUGS
+
+They're probably crawling somewhere in there... if you happen to catch one,
+(or more) report it and I'll do my best to squash it.
+
+=head1 REPOSITORY
+
+You can find the latest source code of B<anopa> as well as report bugs and/or
+suggest features on its GitHub repository, available at
+L<https://github.com/jjk-jacky/anopa>
+
+=head1 AUTHOR
+
+=over
+
+=item Olivier Brunel <jjk@jjacky.com>
+
+=back
diff --git a/package/modes b/package/modes
index 7bb2c14..99ef6e0 100644
--- a/package/modes
+++ b/package/modes
@@ -1,2 +1,3 @@
 aa-start                0755
 aa-enable               0755
+aa-echo                 0755
diff --git a/package/targets.mak b/package/targets.mak
index f829a40..ef7f7c2 100644
--- a/package/targets.mak
+++ b/package/targets.mak
@@ -1,10 +1,12 @@
 BIN_TARGETS := \
 aa-start \
-aa-enable
+aa-enable \
+aa-echo
 
 DOC_TARGETS := \
 doc/anopa.1 \
-doc/aa-enable.1
+doc/aa-enable.1 \
+doc/aa-echo.1
 
 ifdef DO_ALLSTATIC
 LIBANOPA := libanopa.a
diff --git a/src/anopa/aa-echo.c b/src/anopa/aa-echo.c
new file mode 100644
index 0000000..6eea326
--- /dev/null
+++ b/src/anopa/aa-echo.c
@@ -0,0 +1,135 @@
+
+#include <getopt.h>
+#include <skalibs/bytestr.h>
+#include <anopa/common.h>
+#include <anopa/output.h>
+
+char const *PROG;
+typedef void (*put_fn) (const char *name, const char *msg, int end);
+
+static void
+put_title (const char *name, const char *msg, int end)
+{
+    aa_put_title (1, name, msg, end);
+}
+
+static void
+put_title2 (const char *name, const char *msg, int end)
+{
+    aa_put_title (0, name, msg, end);
+}
+
+static void
+dieusage (void)
+{
+    aa_die_usage ("[OPTION...] MESSAGE...",
+            " -D, --double-output           Enable double-output mode\n"
+            " -T, --title                   Show a main title (default)\n"
+            " -t, --title2                  Show a secondary title\n"
+            " -w, --warning                 Show a warning\n"
+            " -e, --error                   Show an error\n"
+            " -h, --help                    Show this help screen and exit\n"
+            " -V, --version                 Show version information and exit\n"
+            "\n"
+            "MESSAGE can be used to set the text color:\n"
+            "\n"
+            " +g, +green                    Set color to green\n"
+            " +b, +blue                     Set color to blue\n"
+            " +y, +yellow                   Set color to yellow\n"
+            " +r, +red                      Set color to red\n"
+            " +n, +normal                   Set color to normal (white)\n"
+            " ++TEXT                        To just print +TEXT\n"
+            );
+}
+
+int
+main (int argc, char * const argv[])
+{
+    PROG = "aa-echo";
+    int mode_both = 0;
+    put_fn put = put_title;
+    int where = AA_OUT;
+    int i;
+
+    for (;;)
+    {
+        struct option longopts[] = {
+            { "help",               no_argument,        NULL,   'h' },
+            { "version",            no_argument,        NULL,   'V' },
+            { "double-output",      no_argument,        NULL,   'D' },
+            { "title",              no_argument,        NULL,   'T' },
+            { "title2",             no_argument,        NULL,   't' },
+            { "warning",            no_argument,        NULL,   'w' },
+            { "error",              no_argument,        NULL,   'e' },
+            { NULL, 0, 0, 0 }
+        };
+        int c;
+
+        c = getopt_long (argc, argv, "hVDTtwe", longopts, NULL);
+        if (c == -1)
+            break;
+        switch (c)
+        {
+            case 'D':
+                mode_both = 1;
+                break;
+
+            case 'T':
+                put = put_title;
+                where = AA_OUT;
+                break;
+
+            case 't':
+                put = put_title2;
+                where = AA_OUT;
+                break;
+
+            case 'w':
+                put = aa_put_warn;
+                where = AA_ERR;
+                break;
+
+            case 'e':
+                put = aa_put_err;
+                where = AA_ERR;
+                break;
+
+            case 'V':
+                aa_die_version ();
+
+            default:
+                dieusage ();
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+    if (argc < 1)
+        dieusage ();
+
+    aa_init_output (mode_both);
+    put ("", NULL, 0);
+    for (i = 0; i < argc; ++i)
+    {
+        if (*argv[i] == '+')
+        {
+            if (str_equal (argv[i], "+g") || str_equal (argv[i], "+green"))
+                aa_is_noflush (where, ANSI_HIGHLIGHT_GREEN_ON);
+            else if (str_equal (argv[i], "+b") || str_equal (argv[i], "+blue"))
+                aa_is_noflush (where, ANSI_HIGHLIGHT_BLUE_ON);
+            else if (str_equal (argv[i], "+y") || str_equal (argv[i], "+yellow"))
+                aa_is_noflush (where, ANSI_HIGHLIGHT_YELLOW_ON);
+            else if (str_equal (argv[i], "+r") || str_equal (argv[i], "+red"))
+                aa_is_noflush (where, ANSI_HIGHLIGHT_RED_ON);
+            else if (str_equal (argv[i], "+n") || str_equal (argv[i], "+normal"))
+                aa_is_noflush (where, ANSI_HIGHLIGHT_ON);
+            else
+                aa_bs_noflush (where, argv[i] + 1);
+        }
+        else
+            aa_bs_noflush (where, argv[i]);
+    }
+    aa_bs_flush (where, "\n");
+
+    return 0;
+}
diff --git a/src/anopa/deps-exe/aa-echo b/src/anopa/deps-exe/aa-echo
new file mode 100644
index 0000000..30987b4
--- /dev/null
+++ b/src/anopa/deps-exe/aa-echo
@@ -0,0 +1,2 @@
+${LIBANOPA}
+-lskarnet