forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
310 lines (295 loc) · 7.04 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 (
_ "embed"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/gookit/config/v2"
)
// Version number of oh-my-posh
var Version = "development"
//go:embed init/omp.ps1
var pwshInit string
//go:embed init/omp.fish
var fishInit string
//go:embed init/omp.bash
var bashInit string
//go:embed init/omp.zsh
var zshInit string
const (
noExe = "echo \"Unable to find Oh My Posh executable\""
zsh = "zsh"
bash = "bash"
pwsh = "pwsh"
fish = "fish"
powershell5 = "powershell"
plain = "shell"
)
type args struct {
ErrorCode *int
PrintConfig *bool
ConfigFormat *string
PrintShell *bool
Config *string
Shell *string
PWD *string
PSWD *string
Version *bool
Debug *bool
ExecutionTime *float64
Millis *bool
Eval *bool
Init *bool
PrintInit *bool
ExportPNG *bool
Author *string
CursorPadding *int
RPromptOffset *int
BGColor *string
StackCount *int
Command *string
PrintTransient *bool
}
func main() {
args := &args{
ErrorCode: flag.Int(
"error",
0,
"Error code of previously executed command"),
PrintConfig: flag.Bool(
"print-config",
false,
"Print the current config in json format"),
ConfigFormat: flag.String(
"config-format",
config.JSON,
"The format to print the config in. Valid options are:\n- json\n- yaml\n- toml\n"),
PrintShell: flag.Bool(
"print-shell",
false,
"Print the current shell name"),
Config: flag.String(
"config",
"",
"Add the path to a configuration you wish to load"),
Shell: flag.String(
"shell",
"",
"Override the shell you are working in"),
PWD: flag.String(
"pwd",
"",
"the path you are working in"),
PSWD: flag.String(
"pswd",
"",
"the powershell path you are working in, useful when working with drives"),
Version: flag.Bool(
"version",
false,
"Print the current version of the binary"),
Debug: flag.Bool(
"debug",
false,
"Print debug information"),
ExecutionTime: flag.Float64(
"execution-time",
0,
"Execution time of the previously executed command"),
Millis: flag.Bool(
"millis",
false,
"Get the current time in milliseconds"),
Eval: flag.Bool(
"eval",
false,
"Run in eval mode"),
Init: flag.Bool(
"init",
false,
"Initialize the shell"),
PrintInit: flag.Bool(
"print-init",
false,
"Print the shell initialization script"),
ExportPNG: flag.Bool(
"export-png",
false,
"Create an image based on the current configuration"),
Author: flag.String(
"author",
"",
"Add the author to the exported image using --export-img"),
CursorPadding: flag.Int(
"cursor-padding",
30,
"Pad the cursor with x when using --export-img"),
RPromptOffset: flag.Int(
"rprompt-offset",
40,
"Offset the right prompt with x when using --export-img"),
BGColor: flag.String(
"bg-color",
"#151515",
"Set the background color when using --export-img"),
StackCount: flag.Int(
"stack-count",
0,
"The current location stack count"),
Command: flag.String(
"command",
"",
"Render a tooltip based on the command value"),
PrintTransient: flag.Bool(
"print-transient",
false,
"Print the transient prompt"),
}
flag.Parse()
env := &environment{}
env.init(args)
defer env.tracer.close()
if *args.Millis {
fmt.Print(time.Now().UnixNano() / 1000000)
return
}
if *args.Init {
init := initShell(*args.Shell, *args.Config)
fmt.Print(init)
return
}
if *args.PrintInit {
init := printShellInit(*args.Shell, *args.Config)
fmt.Print(init)
return
}
if *args.PrintConfig {
fmt.Print(exportConfig(*args.Config, *args.ConfigFormat))
return
}
cfg := GetConfig(env)
if *args.PrintShell {
fmt.Println(env.getShellName())
return
}
if *args.Version {
fmt.Println(Version)
return
}
ansi := &ansiUtils{}
ansi.init(env.getShellName())
colorer := &AnsiColor{
ansi: ansi,
terminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground),
}
title := &consoleTitle{
env: env,
config: cfg,
ansi: ansi,
}
engine := &engine{
config: cfg,
env: env,
colorWriter: colorer,
consoleTitle: title,
ansi: ansi,
}
if *args.Debug {
fmt.Print(engine.debug())
return
}
if *args.PrintTransient {
fmt.Print(engine.renderTransientPrompt())
return
}
if len(*args.Command) != 0 {
fmt.Print(engine.renderTooltip(*args.Command))
return
}
prompt := engine.render()
if !*args.ExportPNG {
fmt.Print(prompt)
return
}
imageCreator := &ImageRenderer{
ansiString: prompt,
author: *args.Author,
cursorPadding: *args.CursorPadding,
rPromptOffset: *args.RPromptOffset,
bgColor: *args.BGColor,
ansi: ansi,
}
imageCreator.init()
match := findNamedRegexMatch(`.*(\/|\\)(?P<STR>.+).omp.(json|yaml|toml)`, *args.Config)
err := imageCreator.SavePNG(fmt.Sprintf("%s.png", match[str]))
if err != nil {
fmt.Print(err.Error())
}
}
func initShell(shell, configFile string) string {
executable, err := os.Executable()
if err != nil {
return noExe
}
switch shell {
case pwsh:
return fmt.Sprintf("(@(&\"%s\" --print-init --shell=pwsh --config=\"%s\") -join \"`n\") | Invoke-Expression", executable, configFile)
case zsh, bash, fish:
return printShellInit(shell, configFile)
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
}
func printShellInit(shell, configFile string) string {
executable, err := os.Executable()
// On Windows, it fails when the excutable is called in MSYS2 for example
// which uses unix style paths to resolve the executable's location.
// PowerShell knows how to resolve both, so we can swap this without any issue.
executable = strings.ReplaceAll(executable, "\\", "/")
if err != nil {
return noExe
}
switch shell {
case pwsh:
return getShellInitScript(executable, configFile, pwshInit)
case zsh:
return getShellInitScript(executable, configFile, zshInit)
case bash:
return getShellInitScript(executable, configFile, bashInit)
case fish:
return getShellInitScript(executable, configFile, fishInit)
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
}
func getShellInitScript(executable, configFile, script string) string {
script = strings.ReplaceAll(script, "::OMP::", executable)
script = strings.ReplaceAll(script, "::CONFIG::", configFile)
return script
}
func getConsoleBackgroundColor(env environmentInfo, backgroundColorTemplate string) string {
if len(backgroundColorTemplate) == 0 {
return backgroundColorTemplate
}
context := struct {
Env map[string]string
}{
Env: map[string]string{},
}
matches := findAllNamedRegexMatch(templateEnvRegex, backgroundColorTemplate)
for _, match := range matches {
context.Env[match["ENV"]] = env.getenv(match["ENV"])
}
template := &textTemplate{
Template: backgroundColorTemplate,
Context: context,
Env: env,
}
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}