1// Package lscolors provides styling of filenames based on file features.
2//
3// This is a reverse-engineered implementation of the parsing and
4// interpretation of the LS_COLORS environmental variable used by GNU
5// coreutils.
6package lscolors
7
8import (
9	"os"
10	"path"
11	"strings"
12	"sync"
13
14	"src.elv.sh/pkg/env"
15	"src.elv.sh/pkg/testutil"
16)
17
18// Colorist styles filenames based on the features of the file.
19type Colorist interface {
20	// GetStyle returns the style for the named file.
21	GetStyle(fname string) string
22}
23
24type colorist struct {
25	styleForFeature map[feature]string
26	styleForExt     map[string]string
27}
28
29const defaultLsColorString = `rs=:di=01;34:ln=01;36:mh=:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=36:*.au=36:*.flac=36:*.mid=36:*.midi=36:*.mka=36:*.mp3=36:*.mpc=36:*.ogg=36:*.ra=36:*.wav=36:*.axa=36:*.oga=36:*.spx=36:*.xspf=36:`
30
31var (
32	lastColorist      *colorist
33	lastColoristMutex sync.Mutex
34	lastLsColors      string
35)
36
37func init() {
38	lastColorist = parseLsColor(defaultLsColorString)
39}
40
41func GetColorist() Colorist {
42	lastColoristMutex.Lock()
43	defer lastColoristMutex.Unlock()
44
45	s := getLsColors()
46	if lastLsColors != s {
47		lastLsColors = s
48		lastColorist = parseLsColor(s)
49	}
50	return lastColorist
51}
52
53func getLsColors() string {
54	lsColorString := os.Getenv(env.LS_COLORS)
55	if len(lsColorString) == 0 {
56		return defaultLsColorString
57	}
58	return lsColorString
59}
60
61var featureForName = map[string]feature{
62	"rs": featureRegular,
63	"di": featureDirectory,
64	"ln": featureSymlink,
65	"mh": featureMultiHardLink,
66	"pi": featureNamedPipe,
67	"so": featureSocket,
68	"do": featureDoor,
69	"bd": featureBlockDevice,
70	"cd": featureCharDevice,
71	"or": featureOrphanedSymlink,
72	"su": featureSetuid,
73	"sg": featureSetgid,
74	"ca": featureCapability,
75	"tw": featureWorldWritableStickyDirectory,
76	"ow": featureWorldWritableDirectory,
77	"st": featureStickyDirectory,
78	"ex": featureExecutable,
79}
80
81// parseLsColor parses a string in the LS_COLORS format into lsColor. Erroneous
82// fields are silently ignored.
83func parseLsColor(s string) *colorist {
84	lc := &colorist{make(map[feature]string), make(map[string]string)}
85	for _, spec := range strings.Split(s, ":") {
86		words := strings.Split(spec, "=")
87		if len(words) != 2 {
88			continue
89		}
90		key, value := words[0], words[1]
91		filterValues := []string{}
92		for _, splitValue := range strings.Split(value, ";") {
93			if strings.Count(splitValue, "0") == len(splitValue) {
94				continue
95			}
96			filterValues = append(filterValues, splitValue)
97		}
98		if len(filterValues) == 0 {
99			continue
100		}
101		value = strings.Join(filterValues, ";")
102		if strings.HasPrefix(key, "*.") {
103			lc.styleForExt[key[1:]] = value
104		} else {
105			feature, ok := featureForName[key]
106			if !ok {
107				continue
108			}
109			lc.styleForFeature[feature] = value
110		}
111	}
112	return lc
113}
114
115func (lc *colorist) GetStyle(fname string) string {
116	mh := strings.Trim(lc.styleForFeature[featureMultiHardLink], "0") != ""
117	// TODO Handle error from determineFeature
118	feature, _ := determineFeature(fname, mh)
119	if feature == featureRegular {
120		if ext := path.Ext(fname); ext != "" {
121			if style, ok := lc.styleForExt[ext]; ok {
122				return style
123			}
124		}
125	}
126	return lc.styleForFeature[feature]
127}
128
129// SetTestLsColors sets LS_COLORS to a value where directories are blue and
130// .png files are red for the duration of a test.
131func SetTestLsColors(c testutil.Cleanuper) {
132	// ow (world-writable directory) needed for Windows.
133	testutil.Setenv(c, "LS_COLORS", "di=34:ow=34:*.png=31")
134}
135