-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyles.go
More file actions
81 lines (72 loc) · 1.71 KB
/
styles.go
File metadata and controls
81 lines (72 loc) · 1.71 KB
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
package main
import (
"github.com/fatih/color"
)
func contains(arr []color.Attribute, needle color.Attribute) bool {
for _, a := range arr {
if needle == a {
return true
}
}
return false
}
func isSubset(sub []color.Attribute, super []color.Attribute) bool {
for _, sb := range sub {
if !contains(super, sb) {
return false
}
}
return true
}
func terribleContrast(styles []color.Attribute) bool {
// http://www.thinkui.co.uk/2017/02/high-contrast-colours/
terrible_contrast := make([][]color.Attribute, 1)
terrible_contrast[0] = []color.Attribute{color.FgBlue, color.BgGreen}
for _, pair := range terrible_contrast {
if isSubset(pair, styles) {
return true
}
}
return false
}
func ApplyStyles(s string, styles ...color.Attribute) string {
if s == "" || len(styles) == 0 {
return s
} else {
c := color.New(styles...)
if terribleContrast(styles) {
c.Add(color.Bold)
c.Add(color.FgWhite)
}
c.EnableColor()
return c.Sprint(s)
}
}
func StylePrompt(prompt Prompt, default_color int, symlink_color int, open_write int) {
lastFSRoot := "/"
currentFSRoot := "/"
path := "/"
mounts := GetMounts()
for i := 1; i < len(prompt); i += 1 {
path += prompt[i].Name
style := prompt[i].NameStyle
if !prompt[i].Shadowed {
if IsLink(path) {
style = append(style, color.Attribute(symlink_color))
path = ResolvedPath(path)
} else {
style = append(style, color.Attribute(default_color))
}
if IsOpenWrite(path) {
style = append(style, color.Attribute(open_write))
}
}
currentFSRoot = GetPathRoot(path, mounts)
if currentFSRoot != lastFSRoot {
style = append(style, color.Underline)
lastFSRoot = currentFSRoot
}
prompt[i].NameStyle = style
path += "/"
}
}