summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xtests/ls-color.sh37
1 files changed, 32 insertions, 5 deletions
diff --git a/tests/ls-color.sh b/tests/ls-color.sh
index 6d33f53..9fdd59c 100755
--- a/tests/ls-color.sh
+++ b/tests/ls-color.sh
@@ -7,17 +7,44 @@
set -e
+function parse_ls_colors() {
+ for key; do
+ local -n var="$key"
+ if [[ "$LS_COLORS" =~ (^|:)$key=(([^:]|\\:)*) ]]; then
+ var="${BASH_REMATCH[2]}"
+ # Interpret escapes
+ var=$(printf "$var" | sed $'s/\^\[/\033/g; s/\\\\:/:/g')
+ fi
+ done
+}
+
+function re_escape() {
+ # https://stackoverflow.com/a/29613573/502399
+ sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1"
+}
+
+rs=0
+lc=$'\033['
+rc=m
+ec=
+no=
+
+parse_ls_colors rs lc rc ec no
+: "${ec:=$lc$rs$rc}"
+
+strip="(($(re_escape "$lc$no$rc"))?($(re_escape "$ec")|$(re_escape "$lc$rc")))+"
+
+function ls_color() {
+ # Strip the leading reset sequence from the ls output
+ ls -1d --color "$@" | sed -E "s/^$strip([a-z].*)$strip/\4/; s/^$strip//"
+}
+
L=
if [ "$1" = "-L" ]; then
L="$1"
shift
fi
-function ls_color() {
- # Strip the leading reset sequence from the ls output
- ls -1d --color "$@" | sed $'s/^\033\\[0m//'
-}
-
DIR="${1%/*}"
if [ "$DIR" = "$1" ]; then
ls_color "$1"