From edb2abe085f47b35c9379ee6deccf9223cb95d21 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 27 Jul 2017 14:09:10 +0200 Subject: lr: add action "color " to override colors --- NEWS.md | 1 + README.md | 7 +++++++ lr.1 | 1 + lr.c | 16 ++++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/NEWS.md b/NEWS.md index b07959f..40c5c1b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ * Feature: lr now respects the locale, which mainly influences date format. * Feature: new option `-C` to change the color of files. +* Feature: new action `color ` to change the color of files. * Feature: new argument `@file` to read file names from a file. * Bug: lr now reports errors and sets exit code when toplevel arguments can not be stat'ed. diff --git a/README.md b/README.md index d423dcd..b9d3d1a 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ Default: `n`. | | prune -- do not traverse into subdirectories | print -- always true value + | color -- always true value, override 256-color ::= atime | ctime | mtime @@ -292,6 +293,12 @@ List directory total sizes, indented: 0 .git/refs/tags ``` +List all files, but print them in red if they match "havoc": + +``` +% lr -G -t 'name =~ "havoc" && color 160 || print' +``` + ## Installation Use `make all` to build, `make install` to install relative to `PREFIX` diff --git a/lr.1 b/lr.1 index 67af994..b175b34 100644 --- a/lr.1 +++ b/lr.1 @@ -280,6 +280,7 @@ tests are given by the following EBNF: | | prune -- do not traverse into subdirectories | print -- always true value + | color -- always true value, override 256-color ::= atime | ctime | mtime diff --git a/lr.c b/lr.c index d26e963..a92dcb8 100644 --- a/lr.c +++ b/lr.c @@ -165,6 +165,7 @@ enum op { EXPR_REGEXI, EXPR_PRUNE, EXPR_PRINT, + EXPR_COLOR, EXPR_TYPE, EXPR_ALLSET, EXPR_ANYSET, @@ -397,6 +398,16 @@ parse_inner() } else if (token("print")) { struct expr *e = mkexpr(EXPR_PRINT); return e; + } else if (token("color")) { + struct expr *e = mkexpr(EXPR_COLOR); + int64_t n; + if (parse_num(&n) && n >= 0 && n <= 255) { + e->a.num = n; + return e; + } else { + parse_error("invalid 256-color at '%.15s'", pos); + return 0; + } } else if (token("!")) { struct expr *e = parse_cmp(); struct expr *not = mkexpr(EXPR_NOT); @@ -1140,6 +1151,9 @@ eval(struct expr *e, struct fileinfo *fi) return 1; case EXPR_PRINT: return 1; + case EXPR_COLOR: + fi->color = e->a.num; + return 1; case EXPR_LT: case EXPR_LE: case EXPR_EQ: @@ -1217,6 +1231,8 @@ eval(struct expr *e, struct fileinfo *fi) case EXPR_REGEXI: return regexec(e->b.regex, s, 0, 0, 0) == 0; } } + default: + parse_error("invalid operation %d, please file a bug.", e->op); } return 0; -- cgit 1.4.1