summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2019-01-13 23:15:32 +0200
committerDylan Araps <dylan.araps@gmail.com>2019-01-13 23:15:32 +0200
commit60d123f5d20a4737b78a081bad1f1a264eb79107 (patch)
tree5aac7fa38c865e590367c2dc1fd22f412463c7df
parent002a6f18be12a04e5a81791b0449046a9d568197 (diff)
ls_colors: full working support
-rwxr-xr-xfff45
1 files changed, 26 insertions, 19 deletions
diff --git a/fff b/fff
index 023635f..d58ebb2 100755
--- a/fff
+++ b/fff
@@ -82,18 +82,25 @@ get_ls_colors() {
# Turn $LS_COLORS into an array.
IFS=: read -ra ls_cols <<< "$LS_COLORS"
- # Iterate over the list and separate patterns
- # from '.ext' matching.
- for col in "${ls_cols[@]}"; do
- # Entry is a pattern instead of a file extension.
- [[ $col =~ ^\*[^\.] ]] &&
- ls_patterns+="${col/=*}|"
-
- col="${col/\*./ls_}"
- col="${col/./ls_}"
- col="${col/\*/ls_}"
- ls_exts+="${col//;/\\;};"
- done
+ for ((i=0;i<${#ls_cols[@]};i++)); {
+ # Separate patterns from file types.
+ [[ ${ls_cols[i]} =~ ^\*[^\.] ]] &&
+ ls_patterns+="${ls_cols[i]/=*}|"
+
+ # Prepend 'ls_' to all LS_COLORS items
+ # if they aren't types of files (symbolic links, block files etc.)
+ [[ ${ls_cols[i]} =~ ^(\*|\.) ]] && {
+ ls_cols[i]="${ls_cols[i]#\*}"
+ ls_cols[i]="ls_${ls_cols[i]#.}"
+ }
+
+ ls_cols[i]="${ls_cols[i]//;/\\;};"
+ }
+
+ # Strip non-ascii characters from the string as they're
+ # used as a key to color the dir items and variable
+ # names in bash must be '[a-zA-z0-9_]'.
+ ls_exts="${ls_cols[*]//[^a-zA-Z0-9=\\;]/_}"
# Store the patterns in a '|' separated string
# for use in a REGEX match later.
@@ -196,6 +203,13 @@ print_line() {
elif [[ -S ${list[$1]} ]]; then
format+="\\e[${so:-01;35}m"
+ # Color files that end in a pattern as defined in LS_COLORS.
+ # 'BASH_REMATCH' is an array that stores each REGEX match.
+ elif [[ $ls_patterns && $file_name =~ ($ls_patterns)$ ]]; then
+ match="${BASH_REMATCH[0]}"
+ file_ext="ls_${match//[^a-zA-Z0-9=\\;]/_}"
+ format+="\\e[${!file_ext:-${fi:-37}}m"
+
# Color files based on file extension and LS_COLORS.
# Check if file extension adheres to POSIX naming
# stardard before checking if it's a variable.
@@ -204,13 +218,6 @@ print_line() {
file_ext="ls_${file_ext}"
format+="\\e[${!file_ext:-${fi:-37}}m"
- # Color files that end in a pattern as defined in LS_COLORS.
- # 'BASH_REMATCH' is an array that stores each REGEX match.
- elif [[ $file_name =~ [a-zA-Z_][a-zA-Z0-9_]*$ &&
- $file_name =~ .*($ls_patterns)$ ]]; then
- file_ext="ls_${BASH_REMATCH[1]}"
- format+="\\e[${!file_ext:-${fi:-37}}m"
-
else
format+="\\e[${fi:-37}m"
fi