forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (143 loc) · 4.37 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
package main
import (
"flag"
"fmt"
"os"
"strings"
)
type segment struct {
content string
foreground uint8
background uint8
separator string
separatorForeground uint8
priority int
}
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
IgnoreRepos *string
PrevError *int
}
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)){
"cwd": segmentCwd,
"docker": segmentDocker,
"exit": segmentExitCode,
"git": segmentGit,
"gitlite": segmentGitLite,
"hg": segmentHg,
"host": segmentHost,
"jobs": segmentJobs,
"perlbrew": segmentPerlbrew,
"perms": segmentPerms,
"root": segmentRoot,
"ssh": segmentSsh,
"time": segmentTime,
"user": segmentUser,
"venv": segmentVirtualEnv,
}
func main() {
args := args{
CwdMode: flag.String("cwd-mode", "fancy",
"How to display the current directory\n"+
" "),
CwdMaxDepth: flag.Int("cwd-max-depth", 5,
"Maximum number of directories to show in path\n"+
" "),
CwdMaxDirSize: flag.Int("cwd-max-dir-size", -1,
"Maximum number of letters displayed for each directory in the path\n"+
" "),
ColorizeHostname: flag.Bool("colorize-hostname", false,
"Colorize the hostname based on a hash of itself"),
EastAsianWidth: flag.Bool("east-asian-width", false,
"Use East Asian Ambiguous Widths"),
PromptOnNewLine: flag.Bool("newline", false,
"Show the prompt on a new line"),
Mode: flag.String("mode", "patched",
"The characters used to make separators between segments.\n"+
" (valid choices: patched, compatible, flat)\n"+
" "),
Theme: flag.String("theme", "default",
"Set this to the theme you want to use\n"+
" (valid choices: default, low-contrast)\n"+
" "),
Shell: flag.String("shell", "bash",
"Set this to your shell type\n"+
" (valid choices: bare, bash, zsh)\n"+
" "),
Modules: flag.String("modules",
"venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root",
"The list of modules to load. Separate with ','\n"+
" (valid choices: cwd, docker, exit, git, gitlite, hg, host, jobs, perlbrew, perms, root, ssh, time, user, venv)\n"+
" "),
Priority: flag.String("priority",
"root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit",
"Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','\n"+
" (valid choices: cwd, docker, exit, git-branch, git-status, hg, host, jobs, perlbrew, perms, root, ssh, time, user, venv)\n"+
" "),
MaxWidthPercentage: flag.Int("max-width",
50,
"Maximum width of the shell that the prompt may use, in percent. Setting this to 0 disables the shrinking subsystem.\n"+
" "),
PrevError: flag.Int("error", 0,
"Exit code of previously executed command"),
IgnoreRepos: flag.String("ignore-repos",
"",
"A list of git repos to ignore. Separate with ','\n"+
"Repos are identified by their root directory."),
}
flag.Parse()
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())
}