2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-11-11 09:52:45 -08:00
|
|
|
"errors"
|
2019-03-13 04:14:30 -07:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// Settings holds all the theme for rendering the prompt
|
2019-03-13 04:14:30 -07:00
|
|
|
type Settings struct {
|
2020-11-13 04:24:08 -08:00
|
|
|
FinalSpace bool `json:"final_space"`
|
|
|
|
ConsoleTitle bool `json:"console_title"`
|
|
|
|
ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"`
|
|
|
|
Blocks []*Block `json:"blocks"`
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// BlockType type of block
|
2019-03-13 04:14:30 -07:00
|
|
|
type BlockType string
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// BlockAlignment aligment of a Block
|
2019-03-13 04:14:30 -07:00
|
|
|
type BlockAlignment string
|
|
|
|
|
2020-11-13 04:24:08 -08:00
|
|
|
// ConsoleTitleStyle defines how to show the title in the console window
|
|
|
|
type ConsoleTitleStyle string
|
|
|
|
|
2019-03-13 04:14:30 -07:00
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// Prompt writes one or more Segments
|
2019-03-13 04:14:30 -07:00
|
|
|
Prompt BlockType = "prompt"
|
2020-11-12 00:43:32 -08:00
|
|
|
// LineBreak creates a line break in the prompt
|
2020-09-15 00:11:47 -07:00
|
|
|
LineBreak BlockType = "newline"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Left aligns left
|
2019-03-13 04:14:30 -07:00
|
|
|
Left BlockAlignment = "left"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Right aligns right
|
2019-03-13 04:14:30 -07:00
|
|
|
Right BlockAlignment = "right"
|
2020-11-13 04:24:08 -08:00
|
|
|
// FolderName show the current folder name
|
|
|
|
FolderName ConsoleTitleStyle = "folder"
|
|
|
|
// FullPath show the current path
|
|
|
|
FullPath ConsoleTitleStyle = "path"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// Block defines a part of the prompt with optional segments
|
2019-03-13 04:14:30 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// GetSettings returns the default configuration including possible user overrides
|
2019-03-13 04:14:30 -07:00
|
|
|
func GetSettings(env environmentInfo) *Settings {
|
2020-09-20 10:33:55 -07:00
|
|
|
settings, err := loadUserConfiguration(env)
|
|
|
|
if err != nil {
|
2020-11-11 09:52:45 -08:00
|
|
|
return getDefaultSettings(err.Error())
|
2020-09-20 10:33:55 -07:00
|
|
|
}
|
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-11-11 09:52:45 -08:00
|
|
|
settingsFile := *env.getArgs().Config
|
|
|
|
if settingsFile == "" {
|
|
|
|
return nil, errors.New("NO CONFIG")
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2020-11-11 09:52:45 -08:00
|
|
|
if _, err := os.Stat(settingsFile); os.IsNotExist(err) {
|
|
|
|
return nil, errors.New("INVALID CONFIG PATH")
|
|
|
|
}
|
|
|
|
defaultSettings, err := os.Open(settingsFile)
|
2019-03-13 04:14:30 -07:00
|
|
|
defer func() {
|
|
|
|
_ = defaultSettings.Close()
|
|
|
|
}()
|
|
|
|
if err != nil {
|
2020-11-11 09:52:45 -08:00
|
|
|
return nil, errors.New("UNABLE TO OPEN CONFIG")
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
jsonParser := json.NewDecoder(defaultSettings)
|
2020-09-20 10:33:55 -07:00
|
|
|
err = jsonParser.Decode(&settings)
|
2020-11-11 09:52:45 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("INVALID CONFIG")
|
|
|
|
}
|
|
|
|
return &settings, nil
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:52:45 -08:00
|
|
|
func getDefaultSettings(info string) *Settings {
|
2019-03-13 04:14:30 -07:00
|
|
|
settings := &Settings{
|
2020-11-13 04:24:08 -08:00
|
|
|
FinalSpace: true,
|
|
|
|
ConsoleTitle: true,
|
|
|
|
ConsoleTitleStyle: FolderName,
|
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-10-16 10:15:11 -07:00
|
|
|
Type: Session,
|
|
|
|
Style: Diamond,
|
|
|
|
Background: "#c386f1",
|
|
|
|
Foreground: "#ffffff",
|
|
|
|
LeadingDiamond: "\uE0B6",
|
|
|
|
TrailingDiamond: "\uE0B0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Path,
|
2020-09-26 04:41:28 -07:00
|
|
|
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,
|
2020-09-26 04:41:28 -07:00
|
|
|
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,
|
2020-09-26 04:41:28 -07:00
|
|
|
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,
|
2020-09-26 04:41:28 -07:00
|
|
|
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{}{
|
2020-10-22 04:47:42 -07:00
|
|
|
Prefix: " \uE718",
|
|
|
|
DisplayVersion: false,
|
2020-10-16 10:15:11 -07:00
|
|
|
},
|
2019-03-13 04:14:30 -07:00
|
|
|
},
|
|
|
|
{
|
2020-10-16 10:15:11 -07:00
|
|
|
Type: ShellInfo,
|
2020-09-26 04:41:28 -07:00
|
|
|
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,
|
2020-09-26 04:41:28 -07:00
|
|
|
Style: Powerline,
|
2020-10-15 23:37:43 -07:00
|
|
|
PowerlineSymbol: "\uE0B0",
|
2020-10-16 10:15:11 -07:00
|
|
|
Background: "#ffff66",
|
|
|
|
Foreground: "#ffffff",
|
|
|
|
},
|
2020-11-11 09:52:45 -08:00
|
|
|
{
|
|
|
|
Type: Text,
|
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "\uE0B0",
|
|
|
|
Background: "#ffffff",
|
|
|
|
Foreground: "#111111",
|
|
|
|
Properties: map[Property]interface{}{
|
|
|
|
TextProperty: info,
|
|
|
|
},
|
|
|
|
},
|
2020-10-16 10:15:11 -07:00
|
|
|
{
|
|
|
|
Type: Exit,
|
|
|
|
Style: Diamond,
|
|
|
|
PowerlineSymbol: "\uE0B0",
|
|
|
|
Background: "#2e9599",
|
2020-09-26 04:41:28 -07:00
|
|
|
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
|
|
|
|
}
|