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
|
2020-09-15 00:11:47 -07:00
|
|
|
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 {
|
2020-09-26 04:41:28 -07:00
|
|
|
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 {
|
2020-09-20 10:33:55 -07:00
|
|
|
settings, err := loadUserConfiguration(env)
|
|
|
|
if err != nil {
|
|
|
|
return getDefaultSettings()
|
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
2020-09-20 10:33:55 -07:00
|
|
|
func loadUserConfiguration(env environmentInfo) (*Settings, error) {
|
2019-03-13 04:14:30 -07:00
|
|
|
var settings Settings
|
2020-09-20 10:33:55 -07:00
|
|
|
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 {
|
2020-09-20 10:33:55 -07:00
|
|
|
return nil, err
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
jsonParser := json.NewDecoder(defaultSettings)
|
2020-09-20 10:33:55 -07:00
|
|
|
err = jsonParser.Decode(&settings)
|
|
|
|
return &settings, err
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultSettings() *Settings {
|
|
|
|
settings := &Settings{
|
2020-09-26 11:00:28 -07:00
|
|
|
FinalSpace: true,
|
2019-03-13 04:14:30 -07:00
|
|
|
Blocks: []*Block{
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
Type: Prompt,
|
|
|
|
Alignment: Left,
|
2019-03-13 04:14:30 -07:00
|
|
|
Segments: []*Segment{
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
Type: Root,
|
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "",
|
|
|
|
Background: "#ffe9aa",
|
|
|
|
Foreground: "#100e23",
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
Type: Session,
|
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "",
|
|
|
|
Background: "#ffffff",
|
|
|
|
Foreground: "#100e23",
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
Type: Path,
|
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "",
|
|
|
|
Background: "#91ddff",
|
|
|
|
Foreground: "#100e23",
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
Type: Git,
|
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "",
|
|
|
|
Background: "#95ffa4",
|
|
|
|
Foreground: "#100e23",
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
{
|
2020-10-02 02:55:27 -07:00
|
|
|
Type: Python,
|
2020-09-26 04:41:28 -07:00
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "",
|
|
|
|
Background: "#906cff",
|
|
|
|
Foreground: "#100e23",
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
Type: Exit,
|
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "",
|
|
|
|
Background: "#ff8080",
|
|
|
|
Foreground: "#ffffff",
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return settings
|
|
|
|
}
|