oh-my-posh/settings.go

171 lines
4.3 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import (
"encoding/json"
"fmt"
"os"
)
//Settings holds all the theme for rendering the prompt
type Settings struct {
2020-10-12 00:02:33 -07:00
FinalSpace bool `json:"final_space"`
ConsoleTitle bool `json:"console_title"`
Blocks []*Block `json:"blocks"`
2019-03-13 04:14:30 -07:00
}
//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"
2019-03-13 04:14:30 -07:00
//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"`
2019-03-13 04:14:30 -07:00
}
//GetSettings returns the default configuration including possible user overrides
func GetSettings(env environmentInfo) *Settings {
settings, err := loadUserConfiguration(env)
if err != nil {
return getDefaultSettings()
}
2019-03-13 04:14:30 -07:00
return settings
}
func loadUserConfiguration(env environmentInfo) (*Settings, error) {
2019-03-13 04:14:30 -07:00
var settings Settings
settingsFileLocation := fmt.Sprintf("%s/.go_my_posh", env.getenv("HOME"))
2019-03-13 04:14:30 -07:00
if _, err := os.Stat(*env.getArgs().Config); !os.IsNotExist(err) {
settingsFileLocation = *env.getArgs().Config
}
defaultSettings, err := os.Open(settingsFileLocation)
defer func() {
_ = defaultSettings.Close()
}()
if err != nil {
return nil, err
2019-03-13 04:14:30 -07:00
}
jsonParser := json.NewDecoder(defaultSettings)
err = jsonParser.Decode(&settings)
return &settings, err
2019-03-13 04:14:30 -07:00
}
func getDefaultSettings() *Settings {
settings := &Settings{
FinalSpace: true,
2019-03-13 04:14:30 -07:00
Blocks: []*Block{
{
Type: Prompt,
Alignment: Left,
2019-03-13 04:14:30 -07:00
Segments: []*Segment{
{
2020-10-16 10:15:11 -07:00
Type: Session,
Style: Diamond,
Background: "#c386f1",
Foreground: "#ffffff",
LeadingDiamond: "\uE0B6",
TrailingDiamond: "\uE0B0",
},
{
Type: Path,
Style: Powerline,
2020-10-15 23:37:43 -07:00
PowerlineSymbol: "\uE0B0",
2020-10-16 10:15:11 -07:00
Background: "#ff479c",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uE5FF ",
Style: "folder",
},
2019-03-13 04:14:30 -07:00
},
{
2020-10-16 10:15:11 -07:00
Type: Git,
Style: Powerline,
2020-10-15 23:37:43 -07:00
PowerlineSymbol: "\uE0B0",
2020-10-16 10:15:11 -07:00
Background: "#fffb38",
Foreground: "#193549",
Properties: map[Property]interface{}{
DisplayStashCount: true,
DisplayUpstreamIcon: true,
},
2019-03-13 04:14:30 -07:00
},
{
2020-10-16 10:15:11 -07:00
Type: Battery,
Style: Powerline,
2020-10-15 23:37:43 -07:00
PowerlineSymbol: "\uE0B0",
2020-10-16 10:15:11 -07:00
Background: "#f36943",
Foreground: "#193549",
Properties: map[Property]interface{}{
BatteryIcon: "",
ColorBackground: true,
ChargedColor: "#4caf50",
ChargingColor: "#40c4ff",
DischargingColor: "#ff5722",
Postfix: "\uF295 ",
},
2019-03-13 04:14:30 -07:00
},
{
2020-10-16 10:15:11 -07:00
Type: Node,
Style: Powerline,
2020-10-15 23:37:43 -07:00
PowerlineSymbol: "\uE0B0",
2020-10-16 10:15:11 -07:00
Background: "#6CA35E",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uE718 ",
},
2019-03-13 04:14:30 -07:00
},
{
2020-10-16 10:15:11 -07:00
Type: ShellInfo,
Style: Powerline,
2020-10-15 23:37:43 -07:00
PowerlineSymbol: "\uE0B0",
2020-10-16 10:15:11 -07:00
Background: "#0077c2",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uFCB5 ",
},
2019-03-13 04:14:30 -07:00
},
{
2020-10-16 10:15:11 -07:00
Type: Root,
Style: Powerline,
2020-10-15 23:37:43 -07:00
PowerlineSymbol: "\uE0B0",
2020-10-16 10:15:11 -07:00
Background: "#ffff66",
Foreground: "#ffffff",
},
{
Type: Exit,
Style: Diamond,
PowerlineSymbol: "\uE0B0",
Background: "#2e9599",
Foreground: "#ffffff",
2020-10-16 10:15:11 -07:00
LeadingDiamond: "",
TrailingDiamond: "\uE0B4",
Properties: map[Property]interface{}{
DisplayExitCode: false,
AlwaysEnabled: true,
ErrorColor: "#f1184c",
ColorBackground: true,
Prefix: "<transparent>\uE0B0</> \uE23A",
},
2019-03-13 04:14:30 -07:00
},
},
},
},
}
return settings
}