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"
|
2020-11-11 09:52:45 -08:00
|
|
|
"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"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
2022-01-26 04:53:35 -08:00
|
|
|
"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"
|
2021-03-27 04:04:43 -07:00
|
|
|
"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 {
|
2021-06-23 13:00:40 -07:00
|
|
|
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
|
2022-01-26 01:23:18 -08:00
|
|
|
func GetConfig(env environment.Environment) *Config {
|
2021-03-20 11:32:15 -07:00
|
|
|
cfg, err := loadConfig(env)
|
2020-09-20 10:33:55 -07:00
|
|
|
if err != nil {
|
2021-03-20 11:32:15 -07:00
|
|
|
return getDefaultConfig(err.Error())
|
2020-09-20 10:33:55 -07:00
|
|
|
}
|
2021-03-20 11:32:15 -07:00
|
|
|
return cfg
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
func loadConfig(env environment.Environment) (*Config, error) {
|
2021-03-20 11:32:15 -07:00
|
|
|
var cfg Config
|
2022-01-23 12:37:51 -08:00
|
|
|
configFile := *env.Args().Config
|
|
|
|
eval := *env.Args().Eval
|
2021-03-20 11:32:15 -07:00
|
|
|
if configFile == "" {
|
2020-11-11 09:52:45 -08:00
|
|
|
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)
|
2020-11-11 09:52:45 -08:00
|
|
|
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)
|
2020-11-11 09:52:45 -08:00
|
|
|
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)
|
2020-11-11 09:52:45 -08:00
|
|
|
if err != nil {
|
2021-09-09 11:02:00 -07:00
|
|
|
printConfigError(err, eval)
|
2020-11-11 09:52:45 -08:00
|
|
|
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 {
|
2021-03-27 04:04:43 -07:00
|
|
|
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"
|
2021-04-05 04:56:37 -07:00
|
|
|
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{
|
2020-11-13 04:24:08 -08:00
|
|
|
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{
|
|
|
|
{
|
2020-09-26 04:41:28 -07:00
|
|
|
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,
|
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",
|
2022-01-26 04:53:35 -08:00
|
|
|
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,
|
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",
|
2022-01-26 04:53:35 -08:00
|
|
|
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,
|
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",
|
2022-01-26 04:53:35 -08:00
|
|
|
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,
|
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",
|
2022-01-26 04:53:35 -08:00
|
|
|
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,
|
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",
|
2022-01-26 04:53:35 -08:00
|
|
|
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,
|
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
|
|
|
{
|
2022-01-26 05:10:18 -08:00
|
|
|
Type: TEXT,
|
2020-11-11 09:52:45 -08:00
|
|
|
Style: Powerline,
|
|
|
|
PowerlineSymbol: "\uE0B0",
|
|
|
|
Background: "#ffffff",
|
|
|
|
Foreground: "#111111",
|
2022-01-26 04:53:35 -08:00
|
|
|
Properties: properties.Map{
|
|
|
|
properties.SegmentTemplate: info,
|
2020-11-11 09:52:45 -08:00
|
|
|
},
|
|
|
|
},
|
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",
|
2020-09-26 04:41:28 -07:00
|
|
|
Foreground: "#ffffff",
|
2021-05-18 11:31:37 -07:00
|
|
|
LeadingDiamond: "<transparent,#2e9599>\uE0B0</>",
|
2020-10-16 10:15:11 -07:00
|
|
|
TrailingDiamond: "\uE0B4",
|
2022-01-26 04:53:35 -08:00
|
|
|
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
|
|
|
}
|