-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
53 lines (48 loc) · 1.09 KB
/
prompt.go
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
package main
import (
"github.com/fatih/color"
"os"
"strings"
)
type Part struct {
Name string
Abbreviation string
NameStyle []color.Attribute
SlashStyle []color.Attribute
Shadowed bool
}
type Prompt []*Part
func InitPrompt() Prompt {
path, _ := os.Getwd()
components := strings.Split(path, "/")
prompt := make([]*Part, len(components))
for i, _ := range prompt {
part := Part{}
part.Name = components[i]
part.Abbreviation = components[i]
part.NameStyle = make([]color.Attribute, 0, 4)
prompt[i] = &part
}
return prompt
}
func (prompt Prompt) Format() string {
ret := ""
for idx, part := range prompt {
ret += ApplyStyles(part.Abbreviation, part.NameStyle...)
is_last := idx == len(prompt)-1
is_abbrd := (part.Abbreviation != part.Name)
is_tilde := (part.Abbreviation == "~" && part.Shadowed)
if is_last {
if is_abbrd && !is_tilde {
ret += ApplyStyles("\\", part.SlashStyle...)
}
} else if !part.Shadowed || is_tilde {
sep := "/"
if is_abbrd && !is_tilde {
sep = "\\"
}
ret += ApplyStyles(sep, part.SlashStyle...)
}
}
return ret
}