From b67756001f08dbb23f6ab39b7c2ce4e65708bc16 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Mon, 2 Sep 2024 11:52:44 +0200 Subject: [PATCH] feat(palettes): allow color reuse using palette --- src/config/config.go | 25 +- src/config/config_test.go | 20 ++ website/blog/2022-03-28-idiots-everywhere.md | 2 + website/docs/configuration/colors.mdx | 303 ++++++++++--------- website/docusaurus.config.js | 3 + 5 files changed, 212 insertions(+), 141 deletions(-) diff --git a/src/config/config.go b/src/config/config.go index 28bf2b11..3f77b8e9 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -65,16 +65,31 @@ func (cfg *Config) getPalette() color.Palette { if cfg.Palettes == nil { return cfg.Palette } + tmpl := &template.Text{ Template: cfg.Palettes.Template, Env: cfg.env, } - if palette, err := tmpl.Render(); err == nil { - if p, ok := cfg.Palettes.List[palette]; ok { - return p - } + + key, err := tmpl.Render() + if err != nil { + return cfg.Palette } - return cfg.Palette + + palette, ok := cfg.Palettes.List[key] + if !ok { + return cfg.Palette + } + + for key, color := range cfg.Palette { + if _, ok := palette[key]; ok { + continue + } + + palette[key] = color + } + + return palette } func (cfg *Config) Features() shell.Features { diff --git a/src/config/config_test.go b/src/config/config_test.go index f10f27fd..2cd33f41 100644 --- a/src/config/config_test.go +++ b/src/config/config_test.go @@ -17,6 +17,7 @@ func TestGetPalette(t *testing.T) { "red": "#ff0000", "blue": "#0000ff", } + cases := []struct { Case string Palettes *color.Palettes @@ -69,7 +70,26 @@ func TestGetPalette(t *testing.T) { Case: "no palettes", ExpectedPalette: nil, }, + { + Case: "match, with override", + Palettes: &color.Palettes{ + Template: "{{ .Shell }}", + List: map[string]color.Palette{ + "bash": { + "red": "#ff0001", + "yellow": "#ffff00", + }, + }, + }, + Palette: palette, + ExpectedPalette: color.Palette{ + "red": "#ff0001", + "blue": "#0000ff", + "yellow": "#ffff00", + }, + }, } + for _, tc := range cases { env := &mock.Environment{} env.On("TemplateCache").Return(&cache.Template{ diff --git a/website/blog/2022-03-28-idiots-everywhere.md b/website/blog/2022-03-28-idiots-everywhere.md index 9197b3ec..28f1900e 100644 --- a/website/blog/2022-03-28-idiots-everywhere.md +++ b/website/blog/2022-03-28-idiots-everywhere.md @@ -16,6 +16,8 @@ using an old and new version of Oh My Posh at the same time. Who does that you m using WSL on Windows, or VM’s on any system while sharing the same configuration cross installation. Not the main use-case, but also not exotic either. + + ## What happened? In the beginning of the year, after what has been quite the struggle, I was finally able to deliver **the most important diff --git a/website/docs/configuration/colors.mdx b/website/docs/configuration/colors.mdx index 20a5f514..a29dd7ff 100644 --- a/website/docs/configuration/colors.mdx +++ b/website/docs/configuration/colors.mdx @@ -4,6 +4,8 @@ title: Colors sidebar_label: Colors --- +import Config from "@site/src/components/Config.js"; + ## Standard colors Oh My Posh supports multiple different color references, being: @@ -37,19 +39,19 @@ section in the documentation. The general template properties are listed [here][ The following sample is based on the [AWS Segment][aws]. -```json -{ - "type": "aws", - "style": "powerline", - "powerline_symbol": "\uE0B0", - "foreground": "#ffffff", - "background": "#111111", - "foreground_templates": [ - "{{if contains \"default\" .Profile}}#FFA400{{end}}", - "{{if contains \"jan\" .Profile}}#f1184c{{end}}" - ] -} -``` + The logic is as follows: when `foreground_templates` contains an array, we will check every template line until there's one that returns a non-empty string. So, when the contents of `.Profile` contain the word `default`, the first template @@ -66,53 +68,55 @@ Anything between the color start `<#FF479C>` and end `` will be colored accor If you want to print a colored bracket that isn't the same as the segment's `foreground`, you can do so like this: -```json -"template": "<#CB4B16>┏[", -``` +┏[", + }} +/> If you also wanted to change the background color in the previous command, you would do so like this: -```json -"template": "<#CB4B16,#FFFFFF>┏[", -``` +┏[", + }} +/> To change _only_ the background color, just omit the first color from the above string: -```json -"template": "<,#FFFFFF>┏[", -``` +┏[", + }} +/> ## Palette -If your theme defined the Palette, you can use the _Palette reference_ `p:` in places where the +If your configuration defined the Palette, you can use the _Palette reference_ `p:` in places where the **Standard color** is expected. ### Defining a Palette Palette is a set of named **Standard colors**. To use a Palette, define a `"palette"` object -at the top level of your theme: +at the top level of your configuration: -```json -{ - "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", - "palette": { - "git-foreground": "#193549", - "git": "#FFFB38", - "git-modified": "#FF9248", - "git-diverged": "#FF4500", - "git-ahead": "#B388FF", - "git-behind": "#B388FF", - "red": "#FF0000", - "green": "#00FF00", - "blue": "#0000FF", - "white": "#FFFFFF", - "black": "#111111" + Color names (palette keys) can have any string value, so be creative. Color values, on the other hand, should adhere to the **Standard color** format. @@ -123,48 +127,47 @@ You can now use _Palette references_ in any [Segment's][segment] `foreground`, ` `background`, `background_templates` properties, and other config properties that expect **Standard color** value. _Palette reference_ format is `p:`. Take a look at the [Git][git] segment using _Palette references_: -```json -{ - "type": "git", - "style": "powerline", - "powerline_symbol": "\uE0B0", - "foreground": "p:git-foreground", - "background": "p:git", - "background_templates": [ - "{{ if or (.Working.Changed) (.Staging.Changed) }}p:git-modified{{ end }}", - "{{ if and (gt .Ahead 0) (gt .Behind 0) }}p:git-diverged{{ end }}", - "{{ if gt .Ahead 0 }}p:git-ahead{{ end }}", - "{{ if gt .Behind 0 }}p:git-behind{{ end }}" + -Having all of the colors defined in one place allows you to import existing color themes (usually with slight +Having all of the colors defined in one place allows you to import existing color configurations (usually with slight tweaking to adhere to the format), easily change colors of multiple segments at once, and have a more -organized theme overall. Be creative! +organized configuration overall. Be creative! ### _Palette references_ and **Standard colors** -Using Palette does not interfere with using **Standard colors** in your theme. You can still use **Standard colors** +Using Palette does not interfere with using **Standard colors** in your configuration. You can still use **Standard colors** everywhere. This can be useful if you want to use a specific color for a single segment element, or in a _Color override_ ([Battery segment][battery]): -```json -{ - "type": "battery", - "style": "powerline", - "invert_powerline": true, - "powerline_symbol": "\uE0B2", - "foreground": "p:white", - "background": "p:black", - "properties": { - "discharging_icon": "<#ffa500>- ", - "charging_icon": "+ ", - "charged_icon": "* ", - } -}, -``` +- ", + charging_icon: "+ ", + charged_icon: "* ", + }, + }} +/> ### Handling of invalid references @@ -178,18 +181,16 @@ Palette allows for recursive _Palette reference_ resolution. You can use a _Pale value in Palette. This allows you to define named colors, and use references to those colors as Palette values. For example, `p:foreground` and `p:background` will be correctly set to "#CAF0F80" and "#023E8A": -```json - "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", - "palette": { - "light-blue": "#CAF0F8", - "dark-blue": "#023E8A", - "foreground": "p:light-blue", - "background": "p:dark-blue" + ## Palettes @@ -199,67 +200,97 @@ runtime so your prompt can change at any time based on the outcome of the `templ Take the following configuration: -```json -{ - "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", - "palettes": { - "template": "{{ if eq .Shell \"pwsh\" }}latte{{ else }}frappe{{ end }}", - "list": { - "latte": { - "black": "#262B44", - "green": "#59C9A5", - "orange": "#F07623", - "red": "#e64553", - "white": "#E0DEF4", - "yellow": "#df8e1d", - "blue": "#7287fd" + In this case, when the shell is `pwsh`, the `latte` palette will be used, otherwise it uses the `frappe` palette. If you want, you could also add `frappe` as the default `palette`, given that one is used as a fallback when not match can be found based on what the `template` resolves to. In case no match is available and no `palette` is defined, it will also fallback to `transparent` for any palette color reference in templates/colors. +If you want to avoid color duplication, you can use palettes in combination with the `palette` property. This way you can define +a color once and reuse it in multiple palettes. For example: + + + +:::info +If a color is defined in both palette and palettes, the palettes' resolved color will take precedence. +::: + ## Cycle When you want to display the same **sequence of colors** (background and foreground) regardless of which segments are active, you can make use of the cycle property. This property is a list of colors which are used one after the other in a continuous loop. A defined cycle always gets precedence over everything else. -```json -... -"cycle": [ - { - "background": "p:blue", - "foreground": "p:white" - }, - { - "background": "p:green", - "foreground": "p:black" - }, - { - "background": "p:orange", - "foreground": "p:white" - } -], -... -``` + [hexcolors]: https://htmlcolorcodes.com/color-chart/material-design-color-chart/ [ansicolors]: https://htmlcolorcodes.com/color-chart/material-design-color-chart/ diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 51bfccf0..b186b369 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -168,6 +168,9 @@ module.exports = { require.resolve('./src/css/custom.css') ], }, + blog: { + onInlineAuthors: 'ignore' + }, }, ], ],