Skip to content

Commit

Permalink
Implement file name colouring in {exa,ls}_colors
Browse files Browse the repository at this point in the history
This commit adds to the parsing of the LS_COLORS and EXA_COLORS variables so that non-two-letter codes (keys other than things like ‘di’ or ‘ln’ or ‘ex’) will be treated as file name globs, and get used to colour files accordingly.

Fixes #116 for good.
  • Loading branch information
ogham committed Sep 13, 2017
1 parent b86074d commit dc45332
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 57 deletions.
276 changes: 236 additions & 40 deletions src/options/style.rs

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/output/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,13 @@ impl FileColours for NoFileColours {
fn colour_file(&self, _file: &File) -> Option<Style> { None }
}

// When getting the colour of a file from a *pair* of colourisers, try the
// first one then try the second one. This lets the user provide their own
// file type associations, while falling back to the default set if not set
// explicitly.
impl<A, B> FileColours for (A, B)
where A: FileColours, B: FileColours {
fn colour_file(&self, file: &File) -> Option<Style> {
self.0.colour_file(file).or_else(|| self.1.colour_file(file))
}
}
55 changes: 38 additions & 17 deletions xtests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ env LANG=ja_JP.UTF-8 $exa $testcases/dates -l | diff -q - $results/dates_jp ||
# These directories are created in the VM user’s home directory (the default
# location) when a Cargo build is done.
(cd; mkdir -p target/debug/build
$exa -1d target target/debug target/debug/build | diff -q - $results/dir_paths) || exit 1
$exa -1d . .. / | diff -q - $results/dirs || exit 1
$exa -1d target target/debug target/debug/build | diff -q - $results/dir_paths) || exit 1
$exa -1d . .. / | diff -q - $results/dirs || exit 1


# Links
COLUMNS=80 $exa $testcases/links 2>&1 | diff -q - $results/links || exit 1
$exa $testcases/links -1 2>&1 | diff -q - $results/links_1 || exit 1
$exa $testcases/links -T 2>&1 | diff -q - $results/links_T || exit 1
$exa $testcases/links -T@ 2>&1 | diff -q - $results/links_T@ || exit 1
$exa /proc/1/root -T 2>&1 | diff -q - $results/proc_1_root || exit 1
$exa /proc/1/root -T@ 2>&1 | diff -q - $results/proc_1_root_@ || exit 1
COLUMNS=80 $exa $testcases/links 2>&1 | diff -q - $results/links || exit 1
$exa $testcases/links -1 2>&1 | diff -q - $results/links_1 || exit 1
$exa $testcases/links -T 2>&1 | diff -q - $results/links_T || exit 1
$exa $testcases/links -T@ 2>&1 | diff -q - $results/links_T@ || exit 1
$exa /proc/1/root -T 2>&1 | diff -q - $results/proc_1_root || exit 1
$exa /proc/1/root -T@ 2>&1 | diff -q - $results/proc_1_root_@ || exit 1

# There’ve been bugs where the target file wasn’t printed properly when the
# symlink file was specified on the command-line directly.
Expand Down Expand Up @@ -230,14 +230,35 @@ $exa $testcases/hiddens -l -a 2>&1 | diff -q - $results/hiddens_la || exit 1
$exa $testcases/hiddens -l -aa 2>&1 | diff -q - $results/hiddens_laa || exit 1


# Themes
LS_COLORS="bd=31:cd=32:pi=34" $exa -1 $testcases/specials 2>&1 | diff -q - $results/themed_specials || exit 1
EXA_COLORS="bd=31:cd=32:pi=34" $exa -1 $testcases/specials 2>&1 | diff -q - $results/themed_specials || exit 1

LS_COLORS="*.deb=1;37:*.tar.*=1;37" $exa -1 $testcases/file-names-exts/compressed.* 2>&1 | diff -q - $results/themed_compresseds || exit 1
EXA_COLORS="*.deb=1;37:*.tar.*=1;37" $exa -1 $testcases/file-names-exts/compressed.* 2>&1 | diff -q - $results/themed_compresseds || exit 1
EXA_COLORS="*.deb=1;37" LS_COLORS="*.tar.*=1;37" $exa -1 $testcases/file-names-exts/compressed.* 2>&1 | diff -q - $results/themed_compresseds || exit 1

LS_COLORS="reset:*.deb=1;37:*.tar.*=1;37" $exa -1 $testcases/file-names-exts/compressed.* 2>&1 | diff -q - $results/themed_compresseds || exit 1
EXA_COLORS="reset:*.deb=1;37:*.tar.*=1;37" $exa -1 $testcases/file-names-exts/compressed.* 2>&1 | diff -q - $results/themed_compresseds_r || exit 1

EXA_COLORS="or=32:mi=32;1;4:cc=34;1:ln=34:lp=36;4:xx=32" $exa -1 $testcases/file-names/links 2>&1 | diff -q - $results/themed_links || exit 1

# EXA_COLORS overrides LS_COLORS
LS_COLORS="bd=32:cd=34:pi=31" EXA_COLORS="bd=31:cd=32:pi=34" $exa -1 $testcases/specials 2>&1 | diff -q - $results/themed_specials || exit 1

EXA_COLORS="di=38;5;195:fi=38;5;250:xx=38;5;237:ur=38;5;194:uw=38;5;193:ux=38;5;192:gr=38;5;191:gw=38;5;190:gx=38;5;118:tr=38;5;119:tw=38;5;120:tx=38;5;121:su=38;5;51:sf=38;5;50:sn=38;5;49:un=38;5;46:da=38;5;47:ex=38;5;48" \
$exa --long $testcases/permissions 2>&1 | diff -q - $results/themed_long || exit 1

EXA_COLORS="reset" $exa $testcases/file-names-exts -1 2>&1 | diff -q - $results/themed_un || exit 1

# Errors
$exa --binary 2>&1 | diff -q - $results/error_useless || exit 1
$exa --ternary 2>&1 | diff -q - $results/error_long || exit 1
$exa -4 2>&1 | diff -q - $results/error_short || exit 1
$exa --time 2>&1 | diff -q - $results/error_value || exit 1
$exa --long=time 2>&1 | diff -q - $results/error_overvalued || exit 1
$exa -l --long 2>&1 | diff -q - $results/error_duplicate || exit 1
$exa -ll 2>&1 | diff -q - $results/error_twice || exit 1
$exa --binary 2>&1 | diff -q - $results/error_useless || exit 1
$exa --ternary 2>&1 | diff -q - $results/error_long || exit 1
$exa -4 2>&1 | diff -q - $results/error_short || exit 1
$exa --time 2>&1 | diff -q - $results/error_value || exit 1
$exa --long=time 2>&1 | diff -q - $results/error_overvalued || exit 1
$exa -l --long 2>&1 | diff -q - $results/error_duplicate || exit 1
$exa -ll 2>&1 | diff -q - $results/error_twice || exit 1


# Debug mode
Expand All @@ -246,8 +267,8 @@ EXA_DEBUG="1" $exa $testcases/attributes/dirs/no-xattrs_empty -lh 2>&1 | tail -n


# And finally...
$exa --help | diff -q - $results/help || exit 1
$exa --help --long | diff -q - $results/help_long || exit 1
$exa --help | diff -q - $results/help || exit 1
$exa --help --long | diff -q - $results/help_long || exit 1


echo "All the tests passed!"
5 changes: 5 additions & 0 deletions xtests/themed_compresseds
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/testcases/file-names-exts/compressed.deb
/testcases/file-names-exts/compressed.tar.gz
/testcases/file-names-exts/compressed.tar.xz
/testcases/file-names-exts/compressed.tgz
/testcases/file-names-exts/compressed.txz
5 changes: 5 additions & 0 deletions xtests/themed_compresseds_r
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/testcases/file-names-exts/compressed.deb
/testcases/file-names-exts/compressed.tar.gz
/testcases/file-names-exts/compressed.tar.xz
/testcases/file-names-exts/compressed.tgz
/testcases/file-names-exts/compressed.txz
3 changes: 3 additions & 0 deletions xtests/themed_links
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
another: [\n] -> /testcases/file-names/new-line-dir: [\n]/another: [\n]
broken -> /testcases/file-names/new-line-dir: [\n]/broken
subfile -> /testcases/file-names/new-line-dir: [\n]/subfile
22 changes: 22 additions & 0 deletions xtests/themed_long
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.--------- 0 cassowary  1 Jan 12:34 000
.--------x 0 cassowary  1 Jan 12:34 001
.-------w- 0 cassowary  1 Jan 12:34 002
.------r-- 0 cassowary  1 Jan 12:34 004
.-----x--- 0 cassowary  1 Jan 12:34 010
.----w---- 0 cassowary  1 Jan 12:34 020
.---r----- 0 cassowary  1 Jan 12:34 040
.--x------ 0 cassowary  1 Jan 12:34 100
.-w------- 0 cassowary  1 Jan 12:34 200
.r-------- 0 cassowary  1 Jan 12:34 400
.rw-r--r-- 0 cassowary  1 Jan 12:34 644
.rwxr-xr-x 0 cassowary  1 Jan 12:34 755
.rwxrwxrwx 0 cassowary  1 Jan 12:34 777
.--------T 0 cassowary  1 Jan 12:34 1000
.--------t 0 cassowary  1 Jan 12:34 1001
.-----S--- 0 cassowary  1 Jan 12:34 2000
.-----s--- 0 cassowary  1 Jan 12:34 2010
.--S------ 0 cassowary  1 Jan 12:34 4000
.--s------ 0 cassowary  1 Jan 12:34 4100
.rwSrwSrwT 0 cassowary  1 Jan 12:34 7666
.rwsrwsrwt 0 cassowary  1 Jan 12:34 7777
d--------- - cassowary  1 Jan 12:34 forbidden-directory
3 changes: 3 additions & 0 deletions xtests/themed_specials
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
block-device
char-device
named-pipe
26 changes: 26 additions & 0 deletions xtests/themed_un
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#SAVEFILE#
backup~
compiled.class
compiled.coffee
compiled.js
compiled.o
compressed.deb
compressed.tar.gz
compressed.tar.xz
compressed.tgz
compressed.txz
COMPRESSED.ZIP
crypto.asc
crypto.signature
document.pdf
DOCUMENT.XLSX
file.tmp
IMAGE.PNG
image.svg
lossless.flac
lossless.wav
Makefile
music.mp3
MUSIC.OGG
VIDEO.AVI
video.wmv

0 comments on commit dc45332

Please sign in to comment.