forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
executable file
·187 lines (178 loc) · 4.72 KB
/
settings.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
package main
import (
"encoding/json"
"errors"
"os"
)
//Settings holds all the theme for rendering the prompt
type Settings struct {
FinalSpace bool `json:"final_space"`
ConsoleTitle bool `json:"console_title"`
Blocks []*Block `json:"blocks"`
}
//BlockType type of block
type BlockType string
//BlockAlignment aligment of a Block
type BlockAlignment string
const (
//Prompt writes one or more Segments
Prompt BlockType = "prompt"
//LineBreak creates a line break in the prompt
LineBreak BlockType = "newline"
//Left aligns left
Left BlockAlignment = "left"
//Right aligns right
Right BlockAlignment = "right"
)
//Block defines a part of the prompt with optional segments
type Block struct {
Type BlockType `json:"type"`
Alignment BlockAlignment `json:"alignment"`
HorizontalOffset int `json:"horizontal_offset"`
VerticalOffset int `json:"vertical_offset"`
Segments []*Segment `json:"segments"`
}
//GetSettings returns the default configuration including possible user overrides
func GetSettings(env environmentInfo) *Settings {
settings, err := loadUserConfiguration(env)
if err != nil {
return getDefaultSettings(err.Error())
}
return settings
}
func loadUserConfiguration(env environmentInfo) (*Settings, error) {
var settings Settings
settingsFile := *env.getArgs().Config
if settingsFile == "" {
return nil, errors.New("NO CONFIG")
}
if _, err := os.Stat(settingsFile); os.IsNotExist(err) {
return nil, errors.New("INVALID CONFIG PATH")
}
defaultSettings, err := os.Open(settingsFile)
defer func() {
_ = defaultSettings.Close()
}()
if err != nil {
return nil, errors.New("UNABLE TO OPEN CONFIG")
}
jsonParser := json.NewDecoder(defaultSettings)
err = jsonParser.Decode(&settings)
if err != nil {
return nil, errors.New("INVALID CONFIG")
}
return &settings, nil
}
func getDefaultSettings(info string) *Settings {
settings := &Settings{
FinalSpace: true,
Blocks: []*Block{
{
Type: Prompt,
Alignment: Left,
Segments: []*Segment{
{
Type: Session,
Style: Diamond,
Background: "#c386f1",
Foreground: "#ffffff",
LeadingDiamond: "\uE0B6",
TrailingDiamond: "\uE0B0",
},
{
Type: Path,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#ff479c",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uE5FF ",
Style: "folder",
},
},
{
Type: Git,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#fffb38",
Foreground: "#193549",
Properties: map[Property]interface{}{
DisplayStashCount: true,
DisplayUpstreamIcon: true,
},
},
{
Type: Battery,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#f36943",
Foreground: "#193549",
Properties: map[Property]interface{}{
BatteryIcon: "",
ColorBackground: true,
ChargedColor: "#4caf50",
ChargingColor: "#40c4ff",
DischargingColor: "#ff5722",
Postfix: "\uF295 ",
},
},
{
Type: Node,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#6CA35E",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uE718",
DisplayVersion: false,
},
},
{
Type: ShellInfo,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#0077c2",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uFCB5 ",
},
},
{
Type: Root,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#ffff66",
Foreground: "#ffffff",
},
{
Type: Text,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#ffffff",
Foreground: "#111111",
Properties: map[Property]interface{}{
TextProperty: info,
},
},
{
Type: Exit,
Style: Diamond,
PowerlineSymbol: "\uE0B0",
Background: "#2e9599",
Foreground: "#ffffff",
LeadingDiamond: "",
TrailingDiamond: "\uE0B4",
Properties: map[Property]interface{}{
DisplayExitCode: false,
AlwaysEnabled: true,
ErrorColor: "#f1184c",
ColorBackground: true,
Prefix: "<transparent>\uE0B0</> \uE23A",
},
},
},
},
},
}
return settings
}