/
main.go
209 lines (198 loc) · 4.54 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
//go:generate go-bindata -o init.go init/
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"time"
)
// Version number of oh-my-posh
var Version = "development"
const (
noExe = "echo \"Unable to find Oh my Posh executable\""
zsh = "zsh"
bash = "bash"
pwsh = "pwsh"
fish = "fish"
powershell5 = "powershell"
)
type args struct {
ErrorCode *int
PrintConfig *bool
PrintShell *bool
Config *string
Shell *string
PWD *string
PSWD *string
Version *bool
Debug *bool
ExecutionTime *float64
Millis *bool
Eval *bool
Init *bool
PrintInit *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"),
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"),
}
flag.Parse()
env := &environment{}
env.init(args)
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
}
settings := GetSettings(env)
if *args.PrintConfig {
theme, _ := json.MarshalIndent(settings, "", " ")
fmt.Println(string(theme))
return
}
if *args.PrintShell {
fmt.Println(env.getShellName())
return
}
if *args.Version {
fmt.Println(Version)
return
}
formats := &ansiFormats{}
formats.init(env.getShellName())
renderer := &AnsiRenderer{
formats: formats,
}
colorer := &AnsiColor{
formats: formats,
}
title := &consoleTitle{
env: env,
settings: settings,
formats: formats,
}
engine := &engine{
settings: settings,
env: env,
color: colorer,
renderer: renderer,
consoleTitle: title,
}
if *args.Debug {
engine.debug()
return
}
engine.render()
}
func initShell(shell, config string) string {
executable, err := os.Executable()
if err != nil {
return noExe
}
switch shell {
case pwsh:
return fmt.Sprintf("Invoke-Expression (@(&\"%s\" --print-init --shell=pwsh --config=\"%s\") -join \"`n\")", executable, config)
case zsh, bash, fish:
return printShellInit(shell, config)
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
}
func printShellInit(shell, config 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, config, "init/omp.ps1")
case zsh:
return getShellInitScript(executable, config, "init/omp.zsh")
case bash:
return getShellInitScript(executable, config, "init/omp.bash")
case fish:
return getShellInitScript(executable, config, "init/omp.fish")
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
}
func getShellInitScript(executable, config, script string) string {
data, err := Asset(script)
if err != nil {
return fmt.Sprintf("echo \"Unable to find initialization script %s\"", script)
}
init := string(data)
init = strings.ReplaceAll(init, "::OMP::", executable)
init = strings.ReplaceAll(init, "::CONFIG::", config)
return init
}