268 lines
7.4 KiB
Diff
268 lines
7.4 KiB
Diff
From 714971885128e7ce657022e8cec665d115186dae Mon Sep 17 00:00:00 2001
|
||
From: "Richard W.M. Jones" <rjones@redhat.com>
|
||
Date: Tue, 2 Aug 2022 09:08:50 +0100
|
||
Subject: [PATCH 1/3] dump: Move ANSI colours to separate library under
|
||
common/include
|
||
|
||
I've also relicensed the code (which I wrote originally) as BSD, so
|
||
that we can reuse it in nbdkit.
|
||
---
|
||
common/include/Makefile.am | 1 +
|
||
common/include/ansi-colours.h | 98 +++++++++++++++++++++++++++++++++++
|
||
dump/dump.c | 68 +++++-------------------
|
||
3 files changed, 112 insertions(+), 55 deletions(-)
|
||
create mode 100644 common/include/ansi-colours.h
|
||
|
||
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
|
||
index 4f39c3e8c5..8ff4295e02 100644
|
||
--- a/common/include/Makefile.am
|
||
+++ b/common/include/Makefile.am
|
||
@@ -18,6 +18,7 @@
|
||
include $(top_srcdir)/subdir-rules.mk
|
||
|
||
EXTRA_DIST = \
|
||
+ ansi-colours.h \
|
||
array-size.h \
|
||
byte-swapping.h \
|
||
checked-overflow.h \
|
||
diff --git a/common/include/ansi-colours.h b/common/include/ansi-colours.h
|
||
new file mode 100644
|
||
index 0000000000..ebb0b26f6e
|
||
--- /dev/null
|
||
+++ b/common/include/ansi-colours.h
|
||
@@ -0,0 +1,98 @@
|
||
+/* nbdkit
|
||
+ * Copyright (C) 2022 Red Hat Inc.
|
||
+ *
|
||
+ * Redistribution and use in source and binary forms, with or without
|
||
+ * modification, are permitted provided that the following conditions are
|
||
+ * met:
|
||
+ *
|
||
+ * * Redistributions of source code must retain the above copyright
|
||
+ * notice, this list of conditions and the following disclaimer.
|
||
+ *
|
||
+ * * Redistributions in binary form must reproduce the above copyright
|
||
+ * notice, this list of conditions and the following disclaimer in the
|
||
+ * documentation and/or other materials provided with the distribution.
|
||
+ *
|
||
+ * * Neither the name of Red Hat nor the names of its contributors may be
|
||
+ * used to endorse or promote products derived from this software without
|
||
+ * specific prior written permission.
|
||
+ *
|
||
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
|
||
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
||
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
|
||
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
+ * SUCH DAMAGE.
|
||
+ */
|
||
+
|
||
+#ifndef NBDKIT_ANSI_COLOURS_H
|
||
+#define NBDKIT_ANSI_COLOURS_H
|
||
+
|
||
+#include <stdbool.h>
|
||
+
|
||
+/* For the ansi_* functions, the main program should declare this
|
||
+ * variable, and initialize it in main() / option parsing. See
|
||
+ * libnbd.git/dump/dump.c for an example of how to initialize it.
|
||
+ */
|
||
+extern bool colour;
|
||
+
|
||
+/* Restore the terminal colours to the default.
|
||
+ *
|
||
+ * As well as doing this before normal exit, you should also set a
|
||
+ * signal handler which calls this and fflush(fp). See
|
||
+ * libnbd.git/dump/dump.c for an example.
|
||
+ */
|
||
+static inline void
|
||
+ansi_restore (FILE *fp)
|
||
+{
|
||
+ if (colour)
|
||
+ fputs ("\033[0m", fp);
|
||
+}
|
||
+
|
||
+/* Set the terminal colour. */
|
||
+static inline void
|
||
+ansi_colour (const char *c, FILE *fp)
|
||
+{
|
||
+ if (colour)
|
||
+ fprintf (fp, "\033[%sm", c);
|
||
+}
|
||
+
|
||
+#define ANSI_FG_BOLD_BLACK "1;30"
|
||
+#define ANSI_FG_BLUE "22;34"
|
||
+#define ANSI_FG_BRIGHT_BLUE "1;34"
|
||
+#define ANSI_FG_BRIGHT_CYAN "1;36"
|
||
+#define ANSI_FG_BRIGHT_GREEN "1;32"
|
||
+#define ANSI_FG_BRIGHT_MAGENTA "1;35"
|
||
+#define ANSI_FG_BRIGHT_RED "1;31"
|
||
+#define ANSI_FG_BRIGHT_WHITE "1;37"
|
||
+#define ANSI_FG_BRIGHT_YELLOW "1;33"
|
||
+#define ANSI_FG_CYAN "22;36"
|
||
+#define ANSI_FG_GREEN "22;32"
|
||
+#define ANSI_FG_GREY "22;90"
|
||
+#define ANSI_FG_MAGENTA "22;35"
|
||
+#define ANSI_FG_RED "22;31"
|
||
+#define ANSI_FG_YELLOW "22;33"
|
||
+
|
||
+#define ANSI_BG_BLACK "40"
|
||
+#define ANSI_BG_LIGHT_GREY "47"
|
||
+#define ANSI_BG_GREY "100"
|
||
+
|
||
+/* Unconditional versions of above (don't depend on global ‘colour’). */
|
||
+static inline void
|
||
+ansi_force_restore (FILE *fp)
|
||
+{
|
||
+ fputs ("\033[0m", fp);
|
||
+}
|
||
+
|
||
+static inline void
|
||
+ansi_force_colour (const char *c, FILE *fp)
|
||
+{
|
||
+ fprintf (fp, "\033[%sm", c);
|
||
+}
|
||
+
|
||
+#endif /* NBDKIT_ANSI_COLOURS_H */
|
||
diff --git a/dump/dump.c b/dump/dump.c
|
||
index 8bf62f9329..7f0e86e987 100644
|
||
--- a/dump/dump.c
|
||
+++ b/dump/dump.c
|
||
@@ -32,6 +32,7 @@
|
||
|
||
#include <libnbd.h>
|
||
|
||
+#include "ansi-colours.h"
|
||
#include "minmax.h"
|
||
#include "rounding.h"
|
||
#include "version.h"
|
||
@@ -41,7 +42,7 @@ DEFINE_VECTOR_TYPE (uint32_vector, uint32_t)
|
||
|
||
static const char *progname;
|
||
static struct nbd_handle *nbd;
|
||
-static bool colour;
|
||
+bool colour;
|
||
static uint64_t limit = UINT64_MAX; /* --length (unlimited by default) */
|
||
static int64_t size; /* actual size */
|
||
static bool can_meta_context; /* did we get extent data? */
|
||
@@ -244,54 +245,11 @@ do_connect (void)
|
||
}
|
||
}
|
||
|
||
-/* Various ANSI colours, suppressed if --no-colour / not tty output. */
|
||
-static void
|
||
-ansi_restore (void)
|
||
-{
|
||
- if (colour)
|
||
- fputs ("\033[0m", stdout);
|
||
-}
|
||
-
|
||
-static void
|
||
-ansi_blue (void)
|
||
-{
|
||
- if (colour)
|
||
- fputs ("\033[1;34m", stdout);
|
||
-}
|
||
-
|
||
-static void
|
||
-ansi_green (void)
|
||
-{
|
||
- if (colour)
|
||
- fputs ("\033[0;32m", stdout);
|
||
-}
|
||
-
|
||
-static void
|
||
-ansi_magenta (void)
|
||
-{
|
||
- if (colour)
|
||
- fputs ("\033[1;35m", stdout);
|
||
-}
|
||
-
|
||
-static void
|
||
-ansi_red (void)
|
||
-{
|
||
- if (colour)
|
||
- fputs ("\033[1;31m", stdout);
|
||
-}
|
||
-
|
||
-static void
|
||
-ansi_grey (void)
|
||
-{
|
||
- if (colour)
|
||
- fputs ("\033[0;90m", stdout);
|
||
-}
|
||
-
|
||
static void
|
||
catch_signal (int sig)
|
||
{
|
||
printf ("\n");
|
||
- ansi_restore ();
|
||
+ ansi_restore (stdout);
|
||
fflush (stdout);
|
||
_exit (EXIT_FAILURE);
|
||
}
|
||
@@ -417,21 +375,21 @@ do_dump (void)
|
||
memcpy (last, &buffer[i], 16); /* Save the current line. */
|
||
|
||
/* Print the offset. */
|
||
- ansi_green ();
|
||
+ ansi_colour (ANSI_FG_GREEN, stdout);
|
||
printf ("%010" PRIx64, offset + i);
|
||
- ansi_grey ();
|
||
+ ansi_colour (ANSI_FG_GREY, stdout);
|
||
printf (": ");
|
||
|
||
/* Print the hex codes. */
|
||
for (j = i; j < MIN (i+16, n); ++j) {
|
||
if (buffer[j])
|
||
- ansi_blue ();
|
||
+ ansi_colour (ANSI_FG_BRIGHT_BLUE, stdout);
|
||
else
|
||
- ansi_grey ();
|
||
+ ansi_colour (ANSI_FG_GREY, stdout);
|
||
printf ("%02x ", buffer[j]);
|
||
if ((j - i) == 7) printf (" ");
|
||
}
|
||
- ansi_grey ();
|
||
+ ansi_colour (ANSI_FG_GREY, stdout);
|
||
for (; j < i+16; ++j) {
|
||
printf (" ");
|
||
if ((j - i) == 7) printf (" ");
|
||
@@ -442,23 +400,23 @@ do_dump (void)
|
||
for (j = i; j < MIN (i+16, n); ++j) {
|
||
char c = (char) buffer[j];
|
||
if (isalnum (c)) {
|
||
- ansi_red ();
|
||
+ ansi_colour (ANSI_FG_BRIGHT_RED, stdout);
|
||
printf ("%c", c);
|
||
}
|
||
else if (isprint (c)) {
|
||
- ansi_magenta ();
|
||
+ ansi_colour (ANSI_FG_BRIGHT_MAGENTA, stdout);
|
||
printf ("%c", c);
|
||
}
|
||
else {
|
||
- ansi_grey ();
|
||
+ ansi_colour (ANSI_FG_GREY, stdout);
|
||
printf ("%s", dot);
|
||
}
|
||
}
|
||
- ansi_grey ();
|
||
+ ansi_colour (ANSI_FG_GREY, stdout);
|
||
for (; j < i+16; ++j)
|
||
printf (" ");
|
||
printf ("%s\n", pipe);
|
||
- ansi_restore ();
|
||
+ ansi_restore (stdout);
|
||
}
|
||
|
||
offset += n;
|
||
--
|
||
2.37.0.rc2
|
||
|