oh-my-posh/src/engine/config.go

274 lines
7.1 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
2019-03-13 04:14:30 -07:00
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"
2022-01-26 04:09:21 -08:00
"oh-my-posh/color"
2022-01-26 22:44:35 -08:00
"oh-my-posh/console"
"oh-my-posh/environment"
"oh-my-posh/properties"
2022-01-26 06:54:36 -08:00
"oh-my-posh/segments"
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 {
2022-01-26 22:44:35 -08:00
FinalSpace bool `config:"final_space"`
OSC99 bool `config:"osc99"`
ConsoleTitle bool `config:"console_title"`
ConsoleTitleStyle console.Style `config:"console_title_style"`
ConsoleTitleTemplate string `config:"console_title_template"`
TerminalBackground string `config:"terminal_background"`
Blocks []*Block `config:"blocks"`
Tooltips []*Segment `config:"tooltips"`
TransientPrompt *TransientPrompt `config:"transient_prompt"`
Palette color.Palette `config:"palette"`
2022-01-26 04:09:21 -08:00
}
// MakeColors creates instance of AnsiColors to use in AnsiWriter according to
// environment and configuration.
func (cfg *Config) MakeColors(env environment.Environment) color.AnsiColors {
cacheDisabled := env.Getenv("OMP_CACHE_DISABLED") == "1"
return color.MakeColors(cfg.Palette, !cacheDisabled)
2021-06-15 12:23:08 -07:00
}
type TransientPrompt struct {
Template string `config:"template"`
Background string `config:"background"`
Foreground string `config:"foreground"`
2019-03-13 04:14:30 -07:00
}
2021-09-09 11:02:00 -07:00
func printConfigError(err error, eval bool) {
if eval {
fmt.Println("echo \"Oh My Posh Error:\n\"", err.Error())
return
}
2021-05-18 11:31:12 -07:00
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 environment.Environment) *Config {
2021-03-20 11:32:15 -07:00
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
}
func loadConfig(env environment.Environment) (*Config, error) {
2021-03-20 11:32:15 -07:00
var cfg Config
configFile := *env.Args().Config
eval := *env.Args().Eval
2021-03-20 11:32:15 -07:00
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-09-09 11:02:00 -07:00
printConfigError(err, eval)
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-09-09 11:02:00 -07:00
printConfigError(err, eval)
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-09-09 11:02:00 -07:00
printConfigError(err, eval)
return nil, errors.New("INVALID CONFIG")
}
2021-03-20 11:32:15 -07:00
2021-06-15 12:23:08 -07:00
// initialize default values
if cfg.TransientPrompt == nil {
cfg.TransientPrompt = &TransientPrompt{}
}
2021-03-20 11:32:15 -07:00
return &cfg, nil
}
2022-01-26 23:38:46 -08:00
func ExportConfig(configFile, format string) string {
2021-03-20 11:32:15 -07:00
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-09-09 11:02:00 -07:00
printConfigError(err, false)
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-09-09 11:02:00 -07:00
printConfigError(err, false)
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,
2022-01-26 22:44:35 -08:00
ConsoleTitleStyle: console.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{
{
2022-01-26 05:10:18 -08:00
Type: SESSION,
2020-10-16 10:15:11 -07:00
Style: Diamond,
Background: "#c386f1",
Foreground: "#ffffff",
LeadingDiamond: "\uE0B6",
TrailingDiamond: "\uE0B0",
},
{
2022-01-26 05:10:18 -08:00
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: properties.Map{
properties.Prefix: " \uE5FF ",
properties.Style: "folder",
2020-10-16 10:15:11 -07:00
},
2019-03-13 04:14:30 -07:00
},
{
2022-01-26 05:10:18 -08: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: properties.Map{
2022-01-26 06:54:36 -08:00
segments.FetchStashCount: true,
segments.FetchUpstreamIcon: true,
2020-10-16 10:15:11 -07:00
},
2019-03-13 04:14:30 -07:00
},
{
2022-01-26 05:10:18 -08: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: properties.Map{
properties.Postfix: "\uF295 ",
2020-10-16 10:15:11 -07:00
},
2019-03-13 04:14:30 -07:00
},
{
2022-01-26 05:10:18 -08: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: properties.Map{
properties.Prefix: " \uE718",
properties.FetchVersion: false,
2020-10-16 10:15:11 -07:00
},
2019-03-13 04:14:30 -07:00
},
{
2022-01-26 05:10:18 -08:00
Type: SHELL,
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: properties.Map{
properties.Prefix: " \uFCB5 ",
2020-10-16 10:15:11 -07:00
},
2019-03-13 04:14:30 -07:00
},
{
2022-01-26 05:10:18 -08: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",
},
{
2022-01-26 05:10:18 -08:00
Type: TEXT,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#ffffff",
Foreground: "#111111",
Properties: properties.Map{
properties.SegmentTemplate: info,
},
},
2020-10-16 10:15:11 -07:00
{
2022-01-26 05:10:18 -08:00
Type: EXIT,
2020-10-16 10:15:11 -07:00
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: properties.Map{
properties.AlwaysEnabled: true,
properties.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
}