-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorfiles
executable file
·104 lines (98 loc) · 3.64 KB
/
colorfiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/gawk -f
######################################################################
######################################################################
## Author: Adam Michael Danischewski
## GitHub: https://github.com/AdamDanischewski/cf
## Created: 2019-11-12
## Name: colorfiles.awk
## Version: v1.03
## Last Modified: 2019-11-23
## Issues: If you find any issues emai1 me at <my first name> (dot)
## <my last name> (at) gmail (dot) com.
##
## Requirements: gawk
##
## Allows the user to fine tune and customize their color scheme for
## ls/find and any other utility that produces filenames for display on
## the commandline.
##
## Why not just use ls --color via LS_COLORS? First ls --color doesn't
## color piped filename data. cf is better documented and easier to
## configure than LS_COLORS and allows the user to hit the ground
## running with sensible defaults.
##
## This script allows specific sRGB colors for any file extension and
## has special logic for links, hidden files/dirs, directories and
## executables. (See man cf and man cf_config for information).
##
## The default configuration file is located at:
## /etc/cf/cf_default.conf
##
## User defined config file is located at (not installed by default):
## ~/.config/cf/cf_user.conf
##
## To customize filename colors copy the default file extension color
## mapping config (cf -C) and edit as you wish.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
##
## Released under Creative Commons License: CC BY-SA 4.0
## https://creativecommons.org/licenses/by-sa/4.0/
######################################################################
######################################################################
function init() {
startcolor="\033[38;2;" ## TrueColor RGB triplet prefix
endcolor="\033[0m" ## End ANSI color
}
function wrap_color(s) {
srchstr=r=s
## Match from 1-5 penultimate file-extension followed by up
## to 1-5 final extension. If no hit on the penultimate,
## match file extensions up to 10 characters long.
match(srchstr,/\.([^.]{1,5})?(\.)?([^.]{1,5})$/,srcharr)
## Our file extension, possibly doubled up, now lowered
## for case insensitive matching.
srchstr=tolower(srcharr[0])
gsub(/^\.|^.*\/|[*]$/,"",srchstr)
## Presuming filenames are on the RHS.
## Get rid of everything up to a space. (Eg. -l ls fmt'ing)
gsub(/^.* /,"",r)
## Links have coloring precedence
## Check -l/-F ls fmt (file@ -or- file "->" /goes/somewhere)
if (colormap["links"]!="" && s ~ /@$|->/)
selc=colormap["links"]
## Next in precedence are hidden files
else if (colormap["hidden"]!="" && r ~ /^\.[^./]/)
selc=colormap["hidden"]
## Next in precedence are directories
else if (colormap["dir"]!="" && r ~ /\/$/)
selc=colormap["dir"]
else if (colormap["executable"]!="" && r ~ /*$/)
selc=colormap["executable"]
else
selc=colormap[srchstr]
if(selc=="") { ## No hit yet
gsub(/^.*\./,"",srchstr)
selc=colormap[srchstr]
}
## Fall back logic
if(selc=="") {
if (colormap["fb_executable"]!="" && r ~ /*$/)
selc=colormap["fb_executable"]
else if(colormap["fb_hidden"]!="" && r ~ /^\.[^./]/)
selc=colormap["fb_hidden"]
else if(colormap["fb_links"]!="" && s ~ /@$|->/)
selc=colormap["fb_links"]
}
if(selc=="")
return s # Our dear string unmodified.
else
return (startcolor selc "m" s endcolor) # Our dear colored string.
}
BEGIN {init()}
## Main
{
print wrap_color($0)
}