File: main.go

package info (click to toggle)
powerline-go 1.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: sh: 30; makefile: 4
file content (256 lines) | stat: -rw-r--r-- 6,912 bytes parent folder | download | duplicates (2)
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
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
	*/
)

func warn(msg string) {
	if *args.IgnoreWarnings {
		return
	}

	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 {
		var exists bool
		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) []pwl.Segment{
	"aws":                 segmentAWS,
	"bzr":                 segmentBzr,
	"cwd":                 segmentCwd,
	"direnv":              segmentDirenv,
	"docker":              segmentDocker,
	"docker-context":      segmentDockerContext,
	"dotenv":              segmentDotEnv,
	"duration":            segmentDuration,
	"exit":                segmentExitCode,
	"fossil":              segmentFossil,
	"gcp":                 segmentGCP,
	"git":                 segmentGit,
	"gitlite":             segmentGitLite,
	"goenv":               segmentGoenv,
	"hg":                  segmentHg,
	"svn":                 segmentSubversion,
	"host":                segmentHost,
	"jobs":                segmentJobs,
	"kube":                segmentKube,
	"load":                segmentLoad,
	"newline":             segmentNewline,
	"perlbrew":            segmentPerlbrew,
	"plenv":               segmentPlEnv,
	"perms":               segmentPerms,
	"rbenv":               segmentRbenv,
	"root":                segmentRoot,
	"rvm":                 segmentRvm,
	"shell-var":           segmentShellVar,
	"shenv":               segmentShEnv,
	"ssh":                 segmentSSH,
	"termtitle":           segmentTermTitle,
	"terraform-workspace": segmentTerraformWorkspace,
	"time":                segmentTime,
	"node":                segmentNode,
	"user":                segmentUser,
	"venv":                segmentVirtualEnv,
	"vgo":                 segmentVirtualGo,
	"vi-mode":             segmentViMode,
	"wsl":                 segmentWSL,
	"nix-shell":           segmentNixShell,
}

func comments(lines ...string) string {
	return " " + strings.Join(lines, "\n"+" ")
}

func commentsWithDefaults(lines ...string) string {
	return comments(lines...) + "\n"
}

func main() {
	flag.Parse()

	cfg := defaults
	err := cfg.Load()
	if err != nil {
		println("Error loading config")
		println(err.Error())
	}

	flag.Visit(func(f *flag.Flag) {
		switch f.Name {
		case "cwd-mode":
			cfg.CwdMode = *args.CwdMode
		case "cwd-max-depth":
			cfg.CwdMaxDepth = *args.CwdMaxDepth
		case "cwd-max-dir-size":
			cfg.CwdMaxDirSize = *args.CwdMaxDirSize
		case "colorize-hostname":
			cfg.ColorizeHostname = *args.ColorizeHostname
		case "hostname-only-if-ssh":
			cfg.HostnameOnlyIfSSH = *args.HostnameOnlyIfSSH
		case "alternate-ssh-icon":
			cfg.SshAlternateIcon = *args.SshAlternateIcon
		case "east-asian-width":
			cfg.EastAsianWidth = *args.EastAsianWidth
		case "newline":
			cfg.PromptOnNewLine = *args.PromptOnNewLine
		case "static-prompt-indicator":
			cfg.StaticPromptIndicator = *args.StaticPromptIndicator
		case "venv-name-size-limit":
			cfg.VenvNameSizeLimit = *args.VenvNameSizeLimit
		case "jobs":
			cfg.Jobs = *args.Jobs
		case "git-assume-unchanged-size":
			cfg.GitAssumeUnchangedSize = *args.GitAssumeUnchangedSize
		case "git-disable-stats":
			cfg.GitDisableStats = strings.Split(*args.GitDisableStats, ",")
		case "git-mode":
			cfg.GitMode = *args.GitMode
		case "mode":
			cfg.Mode = *args.Mode
		case "theme":
			cfg.Theme = *args.Theme
		case "shell":
			cfg.Shell = *args.Shell
		case "modules":
			cfg.Modules = strings.Split(*args.Modules, ",")
		case "modules-right":
			cfg.ModulesRight = strings.Split(*args.ModulesRight, ",")
		case "priority":
			cfg.Priority = strings.Split(*args.Priority, ",")
		case "max-width":
			cfg.MaxWidthPercentage = *args.MaxWidthPercentage
		case "truncate-segment-width":
			cfg.TruncateSegmentWidth = *args.TruncateSegmentWidth
		case "error":
			cfg.PrevError = *args.PrevError
		case "numeric-exit-codes":
			cfg.NumericExitCodes = *args.NumericExitCodes
		case "ignore-repos":
			cfg.IgnoreRepos = strings.Split(*args.IgnoreRepos, ",")
		case "shorten-gke-names":
			cfg.ShortenGKENames = *args.ShortenGKENames
		case "shorten-eks-names":
			cfg.ShortenEKSNames = *args.ShortenEKSNames
		case "shorten-openshift-names":
			cfg.ShortenOpenshiftNames = *args.ShortenOpenshiftNames
		case "shell-var":
			cfg.ShellVar = *args.ShellVar
		case "shell-var-no-warn-empty":
			cfg.ShellVarNoWarnEmpty = *args.ShellVarNoWarnEmpty
		case "trim-ad-domain":
			cfg.TrimADDomain = *args.TrimADDomain
		case "path-aliases":
			for _, pair := range strings.Split(*args.PathAliases, ",") {
				kv := strings.SplitN(pair, "=", 2)
				cfg.PathAliases[kv[0]] = kv[1]
			}
		case "duration":
			cfg.Duration = *args.Duration
		case "duration-min":
			cfg.DurationMin = *args.DurationMin
		case "duration-low-precision":
			cfg.DurationLowPrecision = *args.DurationLowPrecision
		case "eval":
			cfg.Eval = *args.Eval
		case "condensed":
			cfg.Condensed = *args.Condensed
		case "ignore-warnings":
			cfg.IgnoreWarnings = *args.IgnoreWarnings
		case "time":
			cfg.Time = *args.Time
		case "vi-mode":
			cfg.ViMode = *args.ViMode
		}
	})

	if strings.HasSuffix(cfg.Theme, ".json") {
		file, err := ioutil.ReadFile(cfg.Theme)
		if err == nil {
			theme := cfg.Themes[defaults.Theme]
			err = json.Unmarshal(file, &theme)
			if err == nil {
				cfg.Themes[cfg.Theme] = theme
			} else {
				println("Error reading theme")
				println(err.Error())
			}
		}
	}

	if strings.HasSuffix(cfg.Mode, ".json") {
		file, err := ioutil.ReadFile(cfg.Mode)
		if err == nil {
			symbols := cfg.Modes[defaults.Mode]
			err = json.Unmarshal(file, &symbols)
			if err == nil {
				cfg.Modes[cfg.Mode] = symbols
			} else {
				println("Error reading mode")
				println(err.Error())
			}
		}
	}

	p := newPowerline(cfg, getValidCwd(), alignLeft)
	if p.supportsRightModules() && p.hasRightModules() && !cfg.Eval {
		panic("Flag '-modules-right' requires '-eval' mode.")
	}

	fmt.Print(p.draw())
}