mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-28 20:39:40 -08:00
feat: add global variables
This commit is contained in:
parent
23148ea823
commit
ea29c99130
|
@ -49,6 +49,7 @@ type Config struct {
|
||||||
Palettes *ansi.Palettes `json:"palettes,omitempty"`
|
Palettes *ansi.Palettes `json:"palettes,omitempty"`
|
||||||
Cycle ansi.Cycle `json:"cycle,omitempty"`
|
Cycle ansi.Cycle `json:"cycle,omitempty"`
|
||||||
PWD string `json:"pwd,omitempty"`
|
PWD string `json:"pwd,omitempty"`
|
||||||
|
Var map[string]interface{} `json:"var,omitempty"`
|
||||||
|
|
||||||
// Deprecated
|
// Deprecated
|
||||||
OSC99 bool `json:"osc99,omitempty"`
|
OSC99 bool `json:"osc99,omitempty"`
|
||||||
|
|
|
@ -16,6 +16,7 @@ func New(flags *platform.Flags) *Engine {
|
||||||
|
|
||||||
env.Init()
|
env.Init()
|
||||||
cfg := LoadConfig(env)
|
cfg := LoadConfig(env)
|
||||||
|
env.Var = cfg.Var
|
||||||
|
|
||||||
ansiWriter := &ansi.Writer{
|
ansiWriter := &ansi.Writer{
|
||||||
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),
|
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),
|
||||||
|
|
|
@ -177,6 +177,7 @@ type TemplateCache struct {
|
||||||
HostName string
|
HostName string
|
||||||
Code int
|
Code int
|
||||||
Env map[string]string
|
Env map[string]string
|
||||||
|
Var map[string]interface{}
|
||||||
OS string
|
OS string
|
||||||
WSL bool
|
WSL bool
|
||||||
PromptCount int
|
PromptCount int
|
||||||
|
@ -271,6 +272,7 @@ func (c *commandCache) get(command string) (string, bool) {
|
||||||
type Shell struct {
|
type Shell struct {
|
||||||
CmdFlags *Flags
|
CmdFlags *Flags
|
||||||
Version string
|
Version string
|
||||||
|
Var map[string]interface{}
|
||||||
|
|
||||||
cwd string
|
cwd string
|
||||||
cmdCache *commandCache
|
cmdCache *commandCache
|
||||||
|
@ -768,6 +770,11 @@ func (env *Shell) TemplateCache() *TemplateCache {
|
||||||
PromptCount: env.CmdFlags.PromptCount,
|
PromptCount: env.CmdFlags.PromptCount,
|
||||||
}
|
}
|
||||||
tmplCache.Env = make(map[string]string)
|
tmplCache.Env = make(map[string]string)
|
||||||
|
tmplCache.Var = make(map[string]interface{})
|
||||||
|
|
||||||
|
if env.Var != nil {
|
||||||
|
tmplCache.Var = env.Var
|
||||||
|
}
|
||||||
|
|
||||||
const separator = "="
|
const separator = "="
|
||||||
values := os.Environ()
|
values := os.Environ()
|
||||||
|
|
|
@ -89,6 +89,7 @@ func (t *Text) cleanTemplate() {
|
||||||
"Segments",
|
"Segments",
|
||||||
"Templates",
|
"Templates",
|
||||||
"PromptCount",
|
"PromptCount",
|
||||||
|
"Var",
|
||||||
}
|
}
|
||||||
|
|
||||||
knownVariable := func(variable string) bool {
|
knownVariable := func(variable string) bool {
|
||||||
|
|
|
@ -127,11 +127,12 @@ For example, the following is a valid `--config` flag:
|
||||||
## General Settings
|
## General Settings
|
||||||
|
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
| --------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| --------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `final_space` | `boolean` | when true adds a space at the end of the prompt |
|
| `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 |
|
| `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 |
|
| `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 |
|
| `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
|
### 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
|
[themes]: https://github.com/JanDeDobbeleer/oh-my-posh/tree/main/themes
|
||||||
[colors]: /docs/configuration/colors
|
[colors]: /docs/configuration/colors
|
||||||
[accent]: /docs/configuration/colors#standard-colors
|
[accent]: /docs/configuration/colors#standard-colors
|
||||||
|
[templates]: /docs/configuration/templates#config-variables
|
||||||
|
|
|
@ -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
|
## Template logic
|
||||||
|
|
||||||
<!-- markdownlint-disable MD013 -->
|
<!-- markdownlint-disable MD013 -->
|
||||||
|
|
Loading…
Reference in a new issue