forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (225 loc) · 6.88 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
runewidth "github.com/mattn/go-runewidth"
)
const (
MinUnsignedInteger uint = 0
MaxUnsignedInteger = ^MinUnsignedInteger
MaxInteger int = int(MaxUnsignedInteger >> 1)
MinInteger = ^MaxInteger
)
type segment struct {
content string
foreground uint8
background uint8
separator string
separatorForeground uint8
priority int
width int
hideSeparators bool
}
type args struct {
CwdMode *string
CwdMaxDepth *int
CwdMaxDirSize *int
ColorizeHostname *bool
EastAsianWidth *bool
PromptOnNewLine *bool
Mode *string
Theme *string
Shell *string
Modules *string
Priority *string
MaxWidthPercentage *int
TruncateSegmentWidth *int
PrevError *int
NumericExitCodes *bool
IgnoreRepos *string
ShortenGKENames *bool
ShellVar *string
PathAliases *string
}
func (s segment) computeWidth() int {
return runewidth.StringWidth(s.content) + runewidth.StringWidth(s.separator) + 2
}
func warn(msg string) {
print("[powerline-go]", msg)
}
func pathExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
} else {
return true
}
}
func getValidCwd() string {
cwd, exists := os.LookupEnv("PWD")
if !exists {
warn("Your current directory is invalid.")
print("> ")
os.Exit(1)
}
parts := strings.Split(cwd, string(os.PathSeparator))
up := cwd
for len(parts) > 0 && !pathExists(up) {
parts = parts[:len(parts)-1]
up = strings.Join(parts, string(os.PathSeparator))
}
if cwd != up {
warn("Your current directory is invalid. Lowest valid directory: " + up)
}
return cwd
}
var modules = map[string](func(*powerline)){
"aws": segmentAWS,
"cwd": segmentCwd,
"docker": segmentDocker,
"dotenv": segmentDotEnv,
"exit": segmentExitCode,
"git": segmentGit,
"gitlite": segmentGitLite,
"hg": segmentHg,
"host": segmentHost,
"jobs": segmentJobs,
"kube": segmentKube,
"load": segmentLoad,
"newline": segmentNewline,
"perlbrew": segmentPerlbrew,
"perms": segmentPerms,
"root": segmentRoot,
"shell-var": segmentShellVar,
"ssh": segmentSsh,
"termtitle": segmentTermTitle,
"time": segmentTime,
"node": segmentNode,
"user": segmentUser,
"venv": segmentVirtualEnv,
"vgo": segmentVirtualGo,
"nix-shell": segmentNixShell,
}
func comments(lines ...string) string {
return " " + strings.Join(lines, "\n"+" \t ")
}
func commentsWithDefaults(lines ...string) string {
return comments(lines...) + "\n" + " \t"
}
func main() {
args := args{
CwdMode: flag.String(
"cwd-mode",
"fancy",
commentsWithDefaults("How to display the current directory",
"(valid choices: fancy, plain, dironly)")),
CwdMaxDepth: flag.Int(
"cwd-max-depth",
5,
commentsWithDefaults("Maximum number of directories to show in path")),
CwdMaxDirSize: flag.Int(
"cwd-max-dir-size",
-1,
commentsWithDefaults("Maximum number of letters displayed for each directory in the path")),
ColorizeHostname: flag.Bool(
"colorize-hostname",
false,
comments("Colorize the hostname based on a hash of itself")),
EastAsianWidth: flag.Bool(
"east-asian-width",
false,
comments("Use East Asian Ambiguous Widths")),
PromptOnNewLine: flag.Bool(
"newline",
false,
comments("Show the prompt on a new line")),
Mode: flag.String(
"mode",
"patched",
commentsWithDefaults("The characters used to make separators between segments.",
"(valid choices: patched, compatible, flat)")),
Theme: flag.String(
"theme",
"default",
commentsWithDefaults("Set this to the theme you want to use",
"(valid choices: default, low-contrast)")),
Shell: flag.String(
"shell",
"bash",
commentsWithDefaults("Set this to your shell type",
"(valid choices: bare, bash, zsh)")),
Modules: flag.String(
"modules",
"nix-shell,venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root,vgo",
commentsWithDefaults("The list of modules to load, separated by ','",
"(valid choices: aws, cwd, docker, dotenv, exit, git, gitlite, hg, host, jobs, load, nix-shell, perlbrew, perms, root, shell-var, ssh, termtitle, time, user, venv, vgo)")),
Priority: flag.String(
"priority",
"root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path",
commentsWithDefaults("Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','",
"(valid choices: aws, cwd, cwd-path, docker, exit, git-branch, git-status, hg, host, jobs, load, nix-shell, perlbrew, perms, root, ssh, time, user, venv, vgo)")),
MaxWidthPercentage: flag.Int(
"max-width",
0,
commentsWithDefaults("Maximum width of the shell that the prompt may use, in percent. Setting this to 0 disables the shrinking subsystem.")),
TruncateSegmentWidth: flag.Int(
"truncate-segment-width",
16,
commentsWithDefaults("Minimum width of a segment, segments longer than this will be shortened if space is limited. Setting this to 0 disables it.")),
PrevError: flag.Int(
"error",
0,
comments("Exit code of previously executed command")),
NumericExitCodes: flag.Bool(
"numeric-exit-codes",
false,
comments("Shows numeric exit codes for errors.")),
IgnoreRepos: flag.String(
"ignore-repos",
"",
comments("A list of git repos to ignore. Separate with ','.",
"Repos are identified by their root directory.")),
ShortenGKENames: flag.Bool(
"shorten-gke-names",
false,
comments("Shortens names for GKE Kube clusters.")),
ShellVar: flag.String(
"shell-var",
"",
comments("A shell variable to add to the segments.")),
PathAliases: flag.String(
"path-aliases",
"",
comments("One or more aliases from a path to a short name. Separate with ','.",
"An alias maps a path like foo/bar/baz to a short name like FBB.",
"Specify these as key/value pairs like foo/bar/baz=FBB.",
"Use '~' for your home dir. You may need to escape this character to avoid shell substitution.")),
}
flag.Parse()
if strings.HasSuffix(*args.Theme, ".json") {
jsonTheme := themes["default"]
file, err := ioutil.ReadFile(*args.Theme)
if err == nil {
json.Unmarshal(file, &jsonTheme)
}
themes[*args.Theme] = jsonTheme
}
priorities := map[string]int{}
priorityList := strings.Split(*args.Priority, ",")
for idx, priority := range priorityList {
priorities[priority] = len(priorityList) - idx
}
powerline := NewPowerline(args, getValidCwd(), priorities)
for _, module := range strings.Split(*powerline.args.Modules, ",") {
elem, ok := modules[module]
if ok {
elem(powerline)
} else {
println("Module not found: " + module)
}
}
fmt.Print(powerline.draw())
}