oh-my-posh/src/config.go

255 lines
6.4 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import (
2021-03-20 11:32:15 -07:00
// "encoding/json"
"bytes"
json2 "encoding/json"
"errors"
2021-03-20 11:32:15 -07:00
"fmt"
2019-03-13 04:14:30 -07:00
"os"
"strconv"
"strings"
2020-12-20 17:20:48 -08:00
2021-03-20 11:32:15 -07:00
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/json"
"github.com/gookit/config/v2/toml"
"github.com/gookit/config/v2/yaml"
2021-06-08 11:16:55 -07:00
"github.com/mitchellh/mapstructure"
2019-03-13 04:14:30 -07:00
)
2021-03-20 11:32:15 -07:00
// Config holds all the theme for rendering the prompt
type Config struct {
FinalSpace bool `config:"final_space"`
OSC99 bool `config:"osc99"`
ConsoleTitle bool `config:"console_title"`
ConsoleTitleStyle ConsoleTitleStyle `config:"console_title_style"`
ConsoleTitleTemplate string `config:"console_title_template"`
TerminalBackground string `config:"terminal_background"`
Blocks []*Block `config:"blocks"`
2019-03-13 04:14:30 -07:00
}
const (
// EnableHyperlink enable hyperlink
EnableHyperlink Property = "enable_hyperlink"
2019-03-13 04:14:30 -07:00
)
2021-05-18 11:31:12 -07:00
func printConfigError(err error) {
fmt.Println("Oh My Posh Error:\n", err.Error())
}
2021-03-20 11:32:15 -07:00
// GetConfig returns the default configuration including possible user overrides
func GetConfig(env environmentInfo) *Config {
cfg, err := loadConfig(env)
if err != nil {
2021-03-20 11:32:15 -07:00
return getDefaultConfig(err.Error())
}
2021-03-20 11:32:15 -07:00
return cfg
2019-03-13 04:14:30 -07:00
}
2021-03-20 11:32:15 -07:00
func loadConfig(env environmentInfo) (*Config, error) {
var cfg Config
configFile := *env.getArgs().Config
if configFile == "" {
return nil, errors.New("NO CONFIG")
2019-03-13 04:14:30 -07:00
}
2021-03-20 11:32:15 -07:00
if _, err := os.Stat(configFile); os.IsNotExist(err) {
2021-05-18 11:31:12 -07:00
printConfigError(err)
return nil, errors.New("INVALID CONFIG PATH")
}
2020-12-20 17:20:48 -08:00
2021-03-20 11:32:15 -07:00
config.AddDriver(yaml.Driver)
config.AddDriver(json.Driver)
config.AddDriver(toml.Driver)
config.WithOptions(func(opt *config.Options) {
2021-06-08 11:16:55 -07:00
opt.DecoderConfig = &mapstructure.DecoderConfig{
TagName: "config",
}
2021-03-20 11:32:15 -07:00
})
err := config.LoadFiles(configFile)
2019-03-13 04:14:30 -07:00
if err != nil {
2021-05-18 11:31:12 -07:00
printConfigError(err)
return nil, errors.New("UNABLE TO OPEN CONFIG")
2019-03-13 04:14:30 -07:00
}
2020-12-20 17:20:48 -08:00
2021-03-20 11:32:15 -07:00
err = config.BindStruct("", &cfg)
if err != nil {
2021-05-18 11:31:12 -07:00
printConfigError(err)
return nil, errors.New("INVALID CONFIG")
}
2021-03-20 11:32:15 -07:00
return &cfg, nil
}
func exportConfig(configFile, format string) string {
if len(format) == 0 {
format = config.JSON
}
config.AddDriver(yaml.Driver)
config.AddDriver(json.Driver)
config.AddDriver(toml.Driver)
err := config.LoadFiles(configFile)
if err != nil {
2021-05-18 11:31:12 -07:00
printConfigError(err)
2021-03-20 11:32:15 -07:00
return fmt.Sprintf("INVALID CONFIG:\n\n%s", err.Error())
}
schemaKey := "$schema"
if format == config.JSON && !config.Exists(schemaKey) {
data := config.Data()
data[schemaKey] = "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json"
config.SetData(data)
}
buf := new(bytes.Buffer)
_, err = config.DumpTo(buf, format)
if err != nil {
2021-05-18 11:31:12 -07:00
printConfigError(err)
2021-03-20 11:32:15 -07:00
return "UNABLE TO DUMP CONFIG"
}
switch format {
case config.JSON:
var prettyJSON bytes.Buffer
err := json2.Indent(&prettyJSON, buf.Bytes(), "", " ")
if err == nil {
unescapeUnicodeCharactersInJSON := func(rawJSON []byte) string {
str, err := strconv.Unquote(strings.ReplaceAll(strconv.Quote(string(rawJSON)), `\\u`, `\u`))
if err != nil {
return err.Error()
}
return str
}
return unescapeUnicodeCharactersInJSON(prettyJSON.Bytes())
2021-03-20 11:32:15 -07:00
}
case config.Yaml:
prefix := "# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n"
content := buf.String()
return prefix + content
case config.Toml:
prefix := "#:schema https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n"
2021-03-20 11:32:15 -07:00
content := buf.String()
return prefix + content
}
return buf.String()
2019-03-13 04:14:30 -07:00
}
2021-03-20 11:32:15 -07:00
func getDefaultConfig(info string) *Config {
cfg := &Config{
FinalSpace: true,
ConsoleTitle: true,
ConsoleTitleStyle: FolderName,
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{}{
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{}{
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,
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: 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,
Background: "#2e9599",
Foreground: "#ffffff",
2021-05-18 11:31:37 -07:00
LeadingDiamond: "<transparent,#2e9599>\uE0B0</>",
2020-10-16 10:15:11 -07:00
TrailingDiamond: "\uE0B4",
Properties: map[Property]interface{}{
DisplayExitCode: false,
AlwaysEnabled: true,
ErrorColor: "#f1184c",
ColorBackground: true,
2021-05-18 11:31:37 -07:00
Prefix: " \uE23A",
2020-10-16 10:15:11 -07:00
},
2019-03-13 04:14:30 -07:00
},
},
},
},
}
2021-03-20 11:32:15 -07:00
return cfg
2019-03-13 04:14:30 -07:00
}