forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (291 loc) · 9.78 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
pwl "github.com/justjanne/powerline-go/powerline"
)
type alignment int
const (
alignLeft alignment = iota
alignRight
)
const (
// MinUnsignedInteger minimum unsigned integer
MinUnsignedInteger uint = 0
// MaxUnsignedInteger maximum unsigned integer
MaxUnsignedInteger = ^MinUnsignedInteger
// MaxInteger maximum integer
MaxInteger = int(MaxUnsignedInteger >> 1)
// MinInteger minimum integer
MinInteger = ^MaxInteger
)
type args struct {
CwdMode *string
CwdMaxDepth *int
CwdMaxDirSize *int
ColorizeHostname *bool
HostnameOnlyIfSSH *bool
SshAlternateIcon *bool
EastAsianWidth *bool
PromptOnNewLine *bool
StaticPromptIndicator *bool
GitAssumeUnchangedSize *int64
Mode *string
Theme *string
Shell *string
Modules *string
ModulesRight *string
Priority *string
MaxWidthPercentage *int
TruncateSegmentWidth *int
PrevError *int
NumericExitCodes *bool
IgnoreRepos *string
ShortenGKENames *bool
ShortenEKSNames *bool
ShellVar *string
PathAliases *string
Duration *string
DurationMin *string
Eval *bool
Condensed *bool
}
func warn(msg string) {
print("[powerline-go]", msg)
}
func pathExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func getValidCwd() string {
cwd, err := os.Getwd()
if err != nil {
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) []pwl.Segment{
"aws": segmentAWS,
"cwd": segmentCwd,
"docker": segmentDocker,
"docker-context": segmentDockerContext,
"dotenv": segmentDotEnv,
"duration": segmentDuration,
"exit": segmentExitCode,
"gcp": segmentGCP,
"git": segmentGit,
"gitlite": segmentGitLite,
"hg": segmentHg,
"svn": segmentSubversion,
"host": segmentHost,
"jobs": segmentJobs,
"kube": segmentKube,
"load": segmentLoad,
"newline": segmentNewline,
"perlbrew": segmentPerlbrew,
"plenv": segmentPlEnv,
"perms": segmentPerms,
"root": segmentRoot,
"shell-var": segmentShellVar,
"shenv": segmentShEnv,
"ssh": segmentSSH,
"termtitle": segmentTermTitle,
"terraform-workspace": segmentTerraformWorkspace,
"time": segmentTime,
"node": segmentNode,
"user": segmentUser,
"venv": segmentVirtualEnv,
"vgo": segmentVirtualGo,
"nix-shell": segmentNixShell,
}
func comments(lines ...string) string {
return " " + strings.Join(lines, "\n"+" ")
}
func commentsWithDefaults(lines ...string) string {
return comments(lines...) + "\n"
}
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, or use the PLGO_HOSTNAMEFG and/or PLGO_HOSTNAMEBG env vars.")),
HostnameOnlyIfSSH: flag.Bool(
"hostname-only-if-ssh",
false,
comments("Show hostname only for SSH connections")),
SshAlternateIcon: flag.Bool(
"alternate-ssh-icon",
false,
comments("Show the older, original icon for SSH connections")),
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")),
StaticPromptIndicator: flag.Bool(
"static-prompt-indicator",
false,
comments("Always show the prompt indicator with the default color, never with the error color")),
GitAssumeUnchangedSize: flag.Int64(
"git-assume-unchanged-size",
2048,
comments("Disable checking for changed/edited files in git repositories where the index is larger than this size (in KB), improves performance")),
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",
"venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root",
commentsWithDefaults("The list of modules to load, separated by ','",
"(valid choices: aws, cwd, docker, docker-context, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, root, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo)")),
ModulesRight: flag.String(
"modules-right",
"",
comments("The list of modules to load anchored to the right, for shells that support it, separated by ','",
"(valid choices: aws, cwd, docker, docker-context, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, root, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, 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, docker, docker-context, dotenv, duration, exit, git, gitlite, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, root, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo)")),
MaxWidthPercentage: flag.Int(
"max-width",
0,
comments("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.")),
ShortenEKSNames: flag.Bool(
"shorten-eks-names",
false,
comments("Shortens names for EKS 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.")),
Duration: flag.String(
"duration",
"",
comments("The elapsed clock-time of the previous command")),
DurationMin: flag.String(
"duration-min",
"0",
comments("The minimal time a command has to take before the duration segment is shown")),
Eval: flag.Bool(
"eval",
false,
comments("Output prompt in 'eval' format.")),
Condensed: flag.Bool(
"condensed",
false,
comments("Remove spacing between segments")),
}
flag.Parse()
if strings.HasSuffix(*args.Theme, ".json") {
jsonTheme := themes["default"]
file, err := ioutil.ReadFile(*args.Theme)
if err == nil {
err = json.Unmarshal(file, &jsonTheme)
if err == nil {
themes[*args.Theme] = jsonTheme
} else {
println("Error reading theme")
println(err.Error())
}
}
}
if strings.HasSuffix(*args.Mode, ".json") {
modeTheme := symbolTemplates["compatible"]
file, err := ioutil.ReadFile(*args.Mode)
if err == nil {
err = json.Unmarshal(file, &modeTheme)
if err == nil {
symbolTemplates[*args.Mode] = modeTheme
} else {
println("Error reading mode")
println(err.Error())
}
}
}
priorities := map[string]int{}
priorityList := strings.Split(*args.Priority, ",")
for idx, priority := range priorityList {
priorities[priority] = len(priorityList) - idx
}
p := newPowerline(args, getValidCwd(), priorities, alignLeft)
if p.supportsRightModules() && p.hasRightModules() && !*args.Eval {
panic("Flag '-modules-right' requires '-eval' mode.")
}
fmt.Print(p.draw())
}