summaryrefslogtreecommitdiffstats
path: root/color.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-02-14 14:42:54 -0500
committerTavian Barnes <tavianator@tavianator.com>2016-02-14 14:45:32 -0500
commit0d6c12cbc8370dafcfcb2c1882303ab2610175c6 (patch)
treee43350ff1e022fe904a4a10446132daf30d8e685 /color.c
parentc40f26a15c0dce60ee365ee3f6d0779ef19cd947 (diff)
downloadbfs-0d6c12cbc8370dafcfcb2c1882303ab2610175c6.tar.xz
Refactor color handling.
The main benefit is colored warnings/errors during parsing.
Diffstat (limited to 'color.c')
-rw-r--r--color.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/color.c b/color.c
index f32d5a3..aea2a54 100644
--- a/color.c
+++ b/color.c
@@ -48,6 +48,9 @@ struct color_table {
const char *sticky;
const char *exec;
+ const char *warning;
+ const char *error;
+
struct ext_color *ext_list;
char *data;
@@ -79,6 +82,8 @@ struct color_table *parse_colors(const char *ls_colors) {
colors->ow = "34;42";
colors->sticky = "37;44";
colors->exec = "01;32";
+ colors->warning = "40;33;01";
+ colors->error = "40;31;01";
colors->ext_list = NULL;
if (ls_colors) {
@@ -315,21 +320,36 @@ void pretty_print(const struct color_table *colors, const struct BFTW *ftwbuf) {
fputs("\n", stdout);
}
-void print_error(const struct color_table *colors, const char *path, int error) {
- const char *color = NULL;
- if (colors) {
- color = colors->orphan;
- }
-
+static void pretty_format(const struct color_table *colors, const char *color, const char *format, va_list args) {
if (color) {
print_esc(color, stderr);
}
- fprintf(stderr, "'%s': %s\n", path, strerror(error));
+
+ vfprintf(stderr, format, args);
+
if (color) {
print_esc(colors->reset, stderr);
}
}
+void pretty_warning(const struct color_table *colors, const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+
+ pretty_format(colors, colors ? colors->warning : NULL, format, args);
+
+ va_end(args);
+}
+
+void pretty_error(const struct color_table *colors, const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+
+ pretty_format(colors, colors ? colors->error : NULL, format, args);
+
+ va_end(args);
+}
+
void free_colors(struct color_table *colors) {
if (colors) {
struct ext_color *ext = colors->ext_list;