-
Notifications
You must be signed in to change notification settings - Fork 0
/
segments.go
144 lines (123 loc) · 3.51 KB
/
segments.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"os"
"os/exec"
"strings"
)
type Segment struct {
fg int
bg int
content string
separator string
separatorFg int
}
func AppendSegment(fg, bg int, content string) {
AppendFullSegment(fg, bg, content, chars.separator, bg)
}
func AppendFullSegment(fg, bg int, content, separator string, separatorFg int) {
segment := new(Segment)
segment.fg = fg
segment.bg = bg
segment.content = content
segment.separator = separator
segment.separatorFg = separatorFg
line = append(line, segment)
}
func InitSegments() map[string]func() {
segments := make(map[string]func())
segments["username"] = AddUserSegment
segments["hostname"] = AddHostSegment
segments["path"] = AddPathSegment
segments["prompt"] = AddPromptSegment
segments["git"] = AddGitSegment
return segments
}
func AddUserSegment() {
fg := colorScheme.UsernameFg
bg := colorScheme.UsernameBg
if os.Geteuid() == 0 {
fg = colorScheme.RootFg
bg = colorScheme.RootBg
}
AppendSegment(fg, bg, "\\u")
}
func AddPromptSegment() {
AppendSegment(colorScheme.PromptFg, colorScheme.PromptBg, "\\$")
}
func AddHostSegment() {
segment := "\\h"
if os.Getenv("SSH_CLIENT") != "" && len(chars.padlock) > 0 {
segment = chars.padlock + " " + segment
}
AppendSegment(colorScheme.HostnameFg, colorScheme.HostnameBg, segment)
}
func AddPathSegment() {
home := os.Getenv("HOME")
path, e := os.Getwd()
if e != nil {
path = os.Getenv("PWD")
}
// Replace home directory with a tilde
if strings.HasPrefix(path, home) {
path = "~" + path[len(home):]
} else if path == "/" {
AppendSegment(colorScheme.CwdFg, colorScheme.PathBg, " ")
return
}
split := strings.Split(path, "/")
// First element might be empty, ignore it
if len(split) > 1 && split[0] == "" {
split = split[1:]
}
// Keep part short
if len(split) > 4 {
tmp := make([]string, 5)
tmp[0] = split[0]
tmp[1] = split[1]
tmp[2] = "\u2026"
tmp[3] = split[len(split)-2]
tmp[4] = split[len(split)-1]
split = tmp
}
for i, dir := range split {
if dir == "~" {
AppendSegment(colorScheme.HomeFg, colorScheme.HomeBg, dir)
} else if i == len(split) - 1 {
AppendSegment(colorScheme.CwdFg, colorScheme.PathBg, dir)
} else {
AppendFullSegment(colorScheme.PathFg, colorScheme.PathBg, dir, chars.separator_thin, colorScheme.SeparatorFg)
}
}
}
func AddGitSegment() {
var (
branch string
filesAdded bool
fg int = colorScheme.RepoCleanFg
bg int = colorScheme.RepoCleanBg
)
cmd := exec.Command("git", "ls-files", "--exclude-standard", "--others")
output, err := cmd.Output()
if err != nil {
return
} else if len(output) > 0 {
filesAdded = true
}
cmd = exec.Command("git", "diff-files", "--quiet")
output, err = cmd.Output()
if err != nil || filesAdded {
fg = colorScheme.RepoDirtyFg
bg = colorScheme.RepoDirtyBg
}
cmd = exec.Command("git", "rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD")
output, err = cmd.Output()
branch = strings.TrimSpace(string(output))
//Add branch character and print it
if len(chars.branch) > 0 {
branch = chars.branch + " " + branch
}
if filesAdded {
branch += " +"
}
AppendSegment(fg, bg, branch)
}