feat(cli): export theme to a file

This commit is contained in:
Jan De Dobbeleer 2022-03-18 13:38:20 +01:00 committed by Jan De Dobbeleer
parent 6012e09888
commit 906ce85d5b
4 changed files with 71 additions and 114 deletions

View file

@ -5,14 +5,16 @@ sidebar_label: 💅🏼 Customize
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem"; import TabItem from "@theme/TabItem"
The standard prompt initialization sets Oh My Posh' [default theme][default-theme]. This configuration is downloaded The standard prompt initialization sets Oh My Posh' [default theme][default-theme]. This configuration is downloaded
and kept up-to-date with Oh My Posh' version every time the shell starts. To change/alter the configuration, you need to and kept up-to-date with Oh My Posh' version every time the shell starts. To change/alter the configuration, you need to
add the `--config` command-line option to change the configuration to use a specific file. add the `--config` command-line option to change the configuration to use a specific file.
The best place to get started is by looking at the [themes][themes], as these are predefined configurations to use/alter. ### Get inspiration
To change to for example the [jandedobbeleer][jandedobbeleer] theme, alter the Oh My Posh init line to the following (assuming you
The best place to get started is by looking at the [themes][themes], as these are **predefined configurations** to use/alter.
To change to for example the [jandedobbeleer][jandedobbeleer] theme, alter the Oh My Posh `init` line to the following (assuming you
downloaded the configuration file to your user's home folder): downloaded the configuration file to your user's home folder):
<Tabs <Tabs
@ -58,7 +60,7 @@ eval "$(oh-my-posh prompt init zsh --config ~/.jandedobbeleer.omp.json)"
Once altered, reload your profile for the changes to take effect. Once altered, reload your profile for the changes to take effect.
```bash ```bash
source ~/.zshrc exec zsh
``` ```
</TabItem> </TabItem>
@ -74,12 +76,6 @@ Once altered, reload your profile for the changes to take effect.
exec bash exec bash
``` ```
Or, when using `~/.profile`.
```bash
. ~/.profile
```
</TabItem> </TabItem>
<TabItem value="fish"> <TabItem value="fish">
@ -113,111 +109,37 @@ Once altered, restart nu shell for the changes to take effect.
</TabItem> </TabItem>
</Tabs> </Tabs>
:::tip :::tip wsl
When using oh-my-posh in Windows and the WSL, know that you can share your theme with the WSL by pointing to a theme in your When using oh-my-posh in Windows and the WSL, know that you can **share your theme with the WSL** by pointing to a theme in your
Windows user's home folder. Windows user's home folder.
Inside the WSL, you can find your Windows user's home folder here: `/mnt/c/Users/<WINDOWSUSERNAME>`. Inside the WSL, you can find your Windows user's home folder here: `/mnt/c/Users/<WINDOWSUSERNAME>`.
::: :::
### Override the theme settings :::tip remote config
The `--config` flag can accept either a local filepath or a remotely hosted config file.
Maybe there's a theme you like, but you don't fancy the colors. Or, maybe there's a segment you
want to tweak/add, or replace some of the icons with a different one. Whatever the case, read through all
available options first, by starting with the [configuration guide][configuration].
You can output the current theme (default, or set via `--config`) to the format you like (`json`, `yaml` or `toml`)
which can be used to tweak and store as your own custom theme.
<Tabs
defaultValue="powershell"
groupId="shell"
values={[
{ label: 'powershell', value: 'powershell', },
{ label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', },
{ label: 'fish', value: 'fish', },
]
}>
<TabItem value="powershell">
```powershell
Export-PoshTheme -FilePath "~/.mytheme.omp.json" -Format json
```
Once you're done editing, adjust your `$PROFILE` to use your newly created theme.
```powershell
oh-my-posh prompt init pwsh --config "~/.mytheme.omp.json" | Invoke-Expression
```
</TabItem>
<TabItem value="zsh">
```bash
export_poshconfig "~/.mytheme.omp.json" json
```
Once you're done editing, adjust `~/.zshrc` to use your newly created theme.
```bash
eval "$(oh-my-posh prompt init zsh --config '~/.mytheme.omp.json')"
```
When adjusted, reload your profile for the changes to take effect.
```bash
exec zsh
```
</TabItem>
<TabItem value="bash">
```bash
export_poshconfig "~/.mytheme.omp.json" json
```
Once you're done editing, adjust `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment) to use your newly created theme.
```bash
eval "$(oh-my-posh prompt init bash --config '~/.mytheme.omp.json')"
```
When adjusted, reload your profile for the changes to take effect.
```bash
exec bash
```
</TabItem>
<TabItem value="fish">
```bash
export_poshconfig "~/.mytheme.omp.json" json
```
Once you're done editing, adjust `config.fish` to use your newly created theme.
```bash
oh-my-posh prompt init fish --config '~/.mytheme.omp.json' | source
```
Once adjusted, reload your config for the changes to take effect.
```bash
. ~/.config/fish/config.fish
```
</TabItem>
</Tabs>
:::tip
The `--config` flag can accept either a local filepath or remotely hosted JSON.
For example, the following is a valid `--config` flag: For example, the following is a valid `--config` flag:
`--config 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json'`
```
--config 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json'
```
::: :::
### Export the current theme
Maybe there's a theme you like, but you don't fancy the colors. Or, maybe there's a segment you
want to tweak/add, or replace some of the icons with a different one. Whatever the case, **read through
available options first**, by starting with the [configuration guide][configuration].
You can export the current theme (default, or set via `--config`) to the format you like (`json`, `yaml` or `toml`)
which can be used to tweak and store as your own custom theme.
```bash
oh-my-posh config export --output ~/.mytheme.omp.yml
```
[themes]: themes.md [themes]: themes.md
[configuration]: config-overview.md [configuration]: config-overview.md

View file

@ -90,3 +90,8 @@ html[data-theme='dark'] .header-gk-link:before {
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 182 182' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='white' d='m 176.70092,47.979262 a 3.6,3.6 0 0 0 -7,1.3 2.5,2.5 0 0 0 0.2,1.2 82.6,82.6 0 0 1 5.3,29.5 83.8,83.8 0 0 1 -72.7,82.999998 v -39.1 a 60.2,60.2 0 0 0 7.3,-1.9 v 32.8 a 77,77 0 0 0 19,-142.199998 3.7,3.7 0 0 0 -5.1,1.6 4.5,4.5 0 0 0 -0.3,1.6 3.6,3.6 0 0 0 1.8,3.2 69.7,69.7 0 0 1 -8.1,125.899998 v -29.1 a 11.2,11.2 0 0 0 7.8,-10.6 11,11 0 0 0 -5.5,-9.699998 c 2.6,-25.1 14.1,-18.5 14.1,-26.5 v -4.6 c 0,-12.1 -27.8,-51.1 -40.799999,-52.1 h -2.4 c -13,1 -40.8,40 -40.8,52.1 v 4.6 c 0,8 11.5,1.4 14.1,26.5 a 11.2,11.2 0 0 0 2.4,20.299998 v 29.1 a 69.7,69.7 0 0 1 -8.2,-125.899998 3.6,3.6 0 0 0 1.5,-4.8 3.5,3.5 0 0 0 -3.3,-2 3.3,3.3 0 0 0 -1.7,0.4 77,77 0 0 0 19,142.199998 v -32.8 a 60.2,60.2 0 0 0 7.3,1.9 v 39.1 A 83.8,83.8 0 0 1 7.8009215,79.979262 a 84.8,84.8 0 0 1 5.2999995,-29.5 3.6,3.6 0 0 0 -1.9,-4.6 4,4 0 0 0 -1.4999998,-0.3 3.7,3.7 0 0 0 -3.3999997,2.4 91,91 0 0 0 81.4999995,122.899998 v -46 h 7.4 v 46 a 90.9,90.9 0 0 0 87.299999,-90.899998 89.1,89.1 0 0 0 -5.8,-32 z m -68.4,51.9 a 7.5,7.5 0 1 1 10.6,10.599998 7.5,7.5 0 1 1 -10.6,-10.599998 z M 74.700921,110.47926 a 7.4,7.4 0 0 1 -10.5,0 7.5,7.5 0 0 1 0,-10.599998 7.4,7.4 0 0 1 10.5,0 7.5,7.5 0 0 1 0,10.599998 z'/%3E%3C/svg%3E") background: url("data:image/svg+xml,%3Csvg viewBox='0 0 182 182' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='white' d='m 176.70092,47.979262 a 3.6,3.6 0 0 0 -7,1.3 2.5,2.5 0 0 0 0.2,1.2 82.6,82.6 0 0 1 5.3,29.5 83.8,83.8 0 0 1 -72.7,82.999998 v -39.1 a 60.2,60.2 0 0 0 7.3,-1.9 v 32.8 a 77,77 0 0 0 19,-142.199998 3.7,3.7 0 0 0 -5.1,1.6 4.5,4.5 0 0 0 -0.3,1.6 3.6,3.6 0 0 0 1.8,3.2 69.7,69.7 0 0 1 -8.1,125.899998 v -29.1 a 11.2,11.2 0 0 0 7.8,-10.6 11,11 0 0 0 -5.5,-9.699998 c 2.6,-25.1 14.1,-18.5 14.1,-26.5 v -4.6 c 0,-12.1 -27.8,-51.1 -40.799999,-52.1 h -2.4 c -13,1 -40.8,40 -40.8,52.1 v 4.6 c 0,8 11.5,1.4 14.1,26.5 a 11.2,11.2 0 0 0 2.4,20.299998 v 29.1 a 69.7,69.7 0 0 1 -8.2,-125.899998 3.6,3.6 0 0 0 1.5,-4.8 3.5,3.5 0 0 0 -3.3,-2 3.3,3.3 0 0 0 -1.7,0.4 77,77 0 0 0 19,142.199998 v -32.8 a 60.2,60.2 0 0 0 7.3,1.9 v 39.1 A 83.8,83.8 0 0 1 7.8009215,79.979262 a 84.8,84.8 0 0 1 5.2999995,-29.5 3.6,3.6 0 0 0 -1.9,-4.6 4,4 0 0 0 -1.4999998,-0.3 3.7,3.7 0 0 0 -3.3999997,2.4 91,91 0 0 0 81.4999995,122.899998 v -46 h 7.4 v 46 a 90.9,90.9 0 0 0 87.299999,-90.899998 89.1,89.1 0 0 0 -5.8,-32 z m -68.4,51.9 a 7.5,7.5 0 1 1 10.6,10.599998 7.5,7.5 0 1 1 -10.6,-10.599998 z M 74.700921,110.47926 a 7.4,7.4 0 0 1 -10.5,0 7.5,7.5 0 0 1 0,-10.599998 7.4,7.4 0 0 1 10.5,0 7.5,7.5 0 0 1 0,10.599998 z'/%3E%3C/svg%3E")
no-repeat; no-repeat;
} }
div.admonition.admonition-tip.alert.alert--success {
background-color: #3e8fb0;
border-color: #3e8fb0;
}

View file

@ -8,10 +8,16 @@ import (
"fmt" "fmt"
"oh-my-posh/engine" "oh-my-posh/engine"
"oh-my-posh/environment" "oh-my-posh/environment"
"path/filepath"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
output string
)
// exportCmd represents the export command // exportCmd represents the export command
var exportCmd = &cobra.Command{ var exportCmd = &cobra.Command{
Use: "export", Use: "export",
@ -43,16 +49,34 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
env.Init(false) env.Init(false)
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
if write { if len(output) == 0 {
cfg.Write() fmt.Print(cfg.Export(format))
return return
} }
fmt.Print(cfg.Export(format)) cfg.Output = cleanOutputPath(output, env)
format := strings.TrimPrefix(filepath.Ext(output), ".")
if format == "yml" {
format = engine.YAML
}
cfg.Write(format)
}, },
} }
func cleanOutputPath(path string, env environment.Environment) string {
if strings.HasPrefix(path, "~") {
path = strings.TrimPrefix(path, "~")
path = filepath.Join(env.Home(), path)
}
if !filepath.IsAbs(path) {
if absConfigFile, err := filepath.Abs(path); err == nil {
path = absConfigFile
}
}
return filepath.Clean(path)
}
func init() { // nolint:gochecknoinits func init() { // nolint:gochecknoinits
exportCmd.Flags().BoolVarP(&write, "write", "w", false, "write the migrated configuration back to the config file")
exportCmd.Flags().StringVarP(&format, "format", "f", "json", "configuration format to migrate to") exportCmd.Flags().StringVarP(&format, "format", "f", "json", "configuration format to migrate to")
exportCmd.Flags().StringVarP(&output, "output", "o", "", "config file to export to")
configCmd.AddCommand(exportCmd) configCmd.AddCommand(exportCmd)
} }

View file

@ -43,6 +43,8 @@ type Config struct {
DebugPrompt *ExtraPrompt `json:"debug_prompt,omitempty"` DebugPrompt *ExtraPrompt `json:"debug_prompt,omitempty"`
Palette color.Palette `json:"palette,omitempty"` Palette color.Palette `json:"palette,omitempty"`
Output string
format string format string
origin string origin string
eval bool eval bool
@ -192,13 +194,17 @@ func (cfg *Config) Export(format string) string {
func (cfg *Config) BackupAndMigrate(env environment.Environment) { func (cfg *Config) BackupAndMigrate(env environment.Environment) {
origin := cfg.backup() origin := cfg.backup()
cfg.Migrate(env) cfg.Migrate(env)
cfg.Write() cfg.Write(cfg.format)
cfg.print(fmt.Sprintf("\nOh My Posh config migrated to version %d\nBackup config available at %s\n\n", cfg.Version, origin)) cfg.print(fmt.Sprintf("\nOh My Posh config migrated to version %d\nBackup config available at %s\n\n", cfg.Version, origin))
} }
func (cfg *Config) Write() { func (cfg *Config) Write(format string) {
content := cfg.Export(cfg.format) content := cfg.Export(format)
f, err := os.OpenFile(cfg.origin, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) destination := cfg.Output
if len(destination) == 0 {
destination = cfg.origin
}
f, err := os.OpenFile(destination, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
cfg.exitWithError(err) cfg.exitWithError(err)
_, err = f.WriteString(content) _, err = f.WriteString(content)
cfg.exitWithError(err) cfg.exitWithError(err)