From ea29c99130c29fedd819b3525e3f156ca1c7bc8f Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 19 Feb 2023 20:21:33 +0100 Subject: [PATCH] feat: add global variables --- src/engine/config.go | 33 +++++++++++----------- src/engine/new.go | 1 + src/platform/shell.go | 7 +++++ src/template/text.go | 1 + website/docs/configuration/overview.mdx | 14 +++++---- website/docs/configuration/templates.mdx | 36 ++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/engine/config.go b/src/engine/config.go index a945598a..076013c1 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -33,22 +33,23 @@ const ( // Config holds all the theme for rendering the prompt type Config struct { - Version int `json:"version"` - FinalSpace bool `json:"final_space,omitempty"` - ConsoleTitleTemplate string `json:"console_title_template,omitempty"` - TerminalBackground string `json:"terminal_background,omitempty"` - AccentColor string `json:"accent_color,omitempty"` - Blocks []*Block `json:"blocks,omitempty"` - Tooltips []*Segment `json:"tooltips,omitempty"` - TransientPrompt *Segment `json:"transient_prompt,omitempty"` - ValidLine *Segment `json:"valid_line,omitempty"` - ErrorLine *Segment `json:"error_line,omitempty"` - SecondaryPrompt *Segment `json:"secondary_prompt,omitempty"` - DebugPrompt *Segment `json:"debug_prompt,omitempty"` - Palette ansi.Palette `json:"palette,omitempty"` - Palettes *ansi.Palettes `json:"palettes,omitempty"` - Cycle ansi.Cycle `json:"cycle,omitempty"` - PWD string `json:"pwd,omitempty"` + Version int `json:"version"` + FinalSpace bool `json:"final_space,omitempty"` + ConsoleTitleTemplate string `json:"console_title_template,omitempty"` + TerminalBackground string `json:"terminal_background,omitempty"` + AccentColor string `json:"accent_color,omitempty"` + Blocks []*Block `json:"blocks,omitempty"` + Tooltips []*Segment `json:"tooltips,omitempty"` + TransientPrompt *Segment `json:"transient_prompt,omitempty"` + ValidLine *Segment `json:"valid_line,omitempty"` + ErrorLine *Segment `json:"error_line,omitempty"` + SecondaryPrompt *Segment `json:"secondary_prompt,omitempty"` + DebugPrompt *Segment `json:"debug_prompt,omitempty"` + Palette ansi.Palette `json:"palette,omitempty"` + Palettes *ansi.Palettes `json:"palettes,omitempty"` + Cycle ansi.Cycle `json:"cycle,omitempty"` + PWD string `json:"pwd,omitempty"` + Var map[string]interface{} `json:"var,omitempty"` // Deprecated OSC99 bool `json:"osc99,omitempty"` diff --git a/src/engine/new.go b/src/engine/new.go index 482b3f72..1406c8ab 100644 --- a/src/engine/new.go +++ b/src/engine/new.go @@ -16,6 +16,7 @@ func New(flags *platform.Flags) *Engine { env.Init() cfg := LoadConfig(env) + env.Var = cfg.Var ansiWriter := &ansi.Writer{ TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground), diff --git a/src/platform/shell.go b/src/platform/shell.go index 962fbdbd..f96d0df1 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -177,6 +177,7 @@ type TemplateCache struct { HostName string Code int Env map[string]string + Var map[string]interface{} OS string WSL bool PromptCount int @@ -271,6 +272,7 @@ func (c *commandCache) get(command string) (string, bool) { type Shell struct { CmdFlags *Flags Version string + Var map[string]interface{} cwd string cmdCache *commandCache @@ -768,6 +770,11 @@ func (env *Shell) TemplateCache() *TemplateCache { PromptCount: env.CmdFlags.PromptCount, } tmplCache.Env = make(map[string]string) + tmplCache.Var = make(map[string]interface{}) + + if env.Var != nil { + tmplCache.Var = env.Var + } const separator = "=" values := os.Environ() diff --git a/src/template/text.go b/src/template/text.go index 403d57e4..d04bd84f 100644 --- a/src/template/text.go +++ b/src/template/text.go @@ -89,6 +89,7 @@ func (t *Text) cleanTemplate() { "Segments", "Templates", "PromptCount", + "Var", } knownVariable := func(variable string) bool { diff --git a/website/docs/configuration/overview.mdx b/website/docs/configuration/overview.mdx index f6074c0e..3519f09a 100644 --- a/website/docs/configuration/overview.mdx +++ b/website/docs/configuration/overview.mdx @@ -126,12 +126,13 @@ For example, the following is a valid `--config` flag: ## General Settings -| Name | Type | Description | -| --------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `final_space` | `boolean` | when true adds a space at the end of the prompt | -| `pwd` | `string` | notify terminal of current working directory, values can be `osc99`, `osc7`, or `osc51` depending on your terminal | -| `terminal_background` | `string` | [color][colors] - terminal background color, set to your terminal's background color when you notice black elements in Windows Terminal or the Visual Studio Code integrated terminal | -| `accent_color` | `string` | [color][colors] - accent color, used as a fallback when the `accent` [color][accent] is not supported | +| Name | Type | Description | +| --------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `final_space` | `boolean` | when true adds a space at the end of the prompt | +| `pwd` | `string` | notify terminal of current working directory, values can be `osc99`, `osc7`, or `osc51` depending on your terminal | +| `terminal_background` | `string` | [color][colors] - terminal background color, set to your terminal's background color when you notice black elements in Windows Terminal or the Visual Studio Code integrated terminal | +| `accent_color` | `string` | [color][colors] - accent color, used as a fallback when the `accent` [color][accent] is not supported | +| `var` | `map[string]any` | config variables to use in [templates][templates]. Can be any value | ### JSON Schema Validation @@ -184,3 +185,4 @@ Converters won't catch this change, so you will need to adjust manually. [themes]: https://github.com/JanDeDobbeleer/oh-my-posh/tree/main/themes [colors]: /docs/configuration/colors [accent]: /docs/configuration/colors#standard-colors +[templates]: /docs/configuration/templates#config-variables diff --git a/website/docs/configuration/templates.mdx b/website/docs/configuration/templates.mdx index ecb902c0..f64fb36a 100644 --- a/website/docs/configuration/templates.mdx +++ b/website/docs/configuration/templates.mdx @@ -89,6 +89,42 @@ end ::: +## Config variables + +| Name | Type | Description | +| -------------- | ----- | -------------------------------------------------------- | +| `.var.VarName` | `any` | Any config variable where `VarName` is the variable name | + +### Example + +```json +{ + "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", + "version": 2, + // highlight-start + "var": { + "Hello": "hello", + "World": "world" + }, + // highlight-end + "blocks": [ + { + "type": "prompt", + "alignment": "left", + "segments": [ + { + "type": "text", + "style": "plain", + "foreground": "p:white", + // highlight-next-line + "template": "{{ .Var.Hello }} {{ .Var.World }} " + } + ] + } + ] +} +``` + ## Template logic