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

@ -49,6 +49,7 @@ type Config struct {
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

@ -127,11 +127,12 @@ 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 |
| `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 -->