summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2018-12-17 17:07:04 -0500
committerTavian Barnes <tavianator@tavianator.com>2018-12-17 17:07:04 -0500
commit9f6acdc2f685383f7cae634778ecfde8b15c5252 (patch)
treece97a5dff5cebae556e66091b8747ef46b475c8c
parent37fc17058b487d4d049296959785e700abc36a88 (diff)
downloadbfs-9f6acdc2f685383f7cae634778ecfde8b15c5252.tar.xz
color: Don't print the leading directory color if there's no leading directory
-rw-r--r--color.c45
-rw-r--r--tests/test_colors.out2
2 files changed, 23 insertions, 24 deletions
diff --git a/color.c b/color.c
index bf6cf4d..dfda9fc 100644
--- a/color.c
+++ b/color.c
@@ -401,6 +401,24 @@ static int print_esc(const char *esc, FILE *file) {
return 0;
}
+static int print_colored(const struct colors *colors, const char *esc, const char *str, size_t len, FILE *file) {
+ if (esc) {
+ if (print_esc(esc, file) != 0) {
+ return -1;
+ }
+ }
+ if (fwrite(str, 1, len, file) != len) {
+ return -1;
+ }
+ if (esc) {
+ if (print_esc(colors->reset, file) != 0) {
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
static int print_path(CFILE *cfile, const struct BFTW *ftwbuf) {
const struct colors *colors = cfile->colors;
FILE *file = cfile->file;
@@ -410,36 +428,17 @@ static int print_path(CFILE *cfile, const struct BFTW *ftwbuf) {
return fputs(path, file) == EOF ? -1 : 0;
}
- const char *filename = path + ftwbuf->nameoff;
-
- if (colors->dir) {
- if (print_esc(colors->dir, file) != 0) {
- return -1;
- }
- }
- if (fwrite(path, 1, ftwbuf->nameoff, file) != ftwbuf->nameoff) {
- return -1;
- }
- if (colors->dir) {
- if (print_esc(colors->reset, file) != 0) {
+ if (ftwbuf->nameoff > 0) {
+ if (print_colored(colors, colors->dir, path, ftwbuf->nameoff, file) != 0) {
return -1;
}
}
+ const char *filename = path + ftwbuf->nameoff;
const char *color = file_color(colors, filename, ftwbuf);
- if (color) {
- if (print_esc(color, file) != 0) {
- return -1;
- }
- }
- if (fputs(filename, file) == EOF) {
+ if (print_colored(colors, color, filename, strlen(filename), file) != 0) {
return -1;
}
- if (color) {
- if (print_esc(colors->reset, file) != 0) {
- return -1;
- }
- }
return 0;
}
diff --git a/tests/test_colors.out b/tests/test_colors.out
index 8f09546..dbdeafa 100644
--- a/tests/test_colors.out
+++ b/tests/test_colors.out
@@ -1,4 +1,4 @@
-links
+links
links/deeply
links/skip
links/symlink