feat: add global variables

This commit is contained in:
Jan De Dobbeleer 2023-02-19 20:21:33 +01:00 committed by Jan De Dobbeleer
parent 23148ea823
commit ea29c99130
6 changed files with 70 additions and 22 deletions

View file

@ -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"`

View file

@ -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),

View file

@ -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()

View file

@ -89,6 +89,7 @@ func (t *Text) cleanTemplate() {
"Segments",
"Templates",
"PromptCount",
"Var",
}
knownVariable := func(variable string) bool {

View file

@ -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

View file

@ -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
<!-- markdownlint-disable MD013 -->