From 1d72a196946784ee074e51551cea84a8eca250ca Mon Sep 17 00:00:00 2001 From: EDIflyer Date: Sat, 8 Jun 2024 17:51:07 +0100 Subject: [PATCH] fix(image): export with correct file name --- src/cli/config_export.go | 2 + src/image/image.go | 27 +++++++++-- src/image/image_test.go | 94 +++++++++---------------------------- website/docs/share-theme.md | 4 +- 4 files changed, 48 insertions(+), 79 deletions(-) diff --git a/src/cli/config_export.go b/src/cli/config_export.go index b636e2a91bdf..7f3dbb0345a9 100644 --- a/src/cli/config_export.go +++ b/src/cli/config_export.go @@ -88,11 +88,13 @@ func cleanOutputPath(path string, env runtime.Environment) string { path = strings.TrimPrefix(path, "~") path = filepath.Join(env.Home(), path) } + if !filepath.IsAbs(path) { if absPath, err := filepath.Abs(path); err == nil { path = absPath } } + return filepath.Clean(path) } diff --git a/src/image/image.go b/src/image/image.go index 2db4897f85b2..2822c3853bbd 100644 --- a/src/image/image.go +++ b/src/image/image.go @@ -150,10 +150,7 @@ type Renderer struct { func (ir *Renderer) Init(env runtime.Environment) error { ir.env = env - if ir.Path == "" { - match := regex.FindNamedRegexMatch(`.*(\/|\\)(?P.+)\.(json|yaml|yml|toml)`, env.Flags().Config) - ir.Path = fmt.Sprintf("%s.png", strings.TrimSuffix(match[str], ".omp")) - } + ir.setOutputPath(env.Flags().Config) ir.cleanContent() @@ -206,6 +203,28 @@ func (ir *Renderer) Init(env runtime.Environment) error { return nil } +func (ir *Renderer) setOutputPath(config string) { + if len(ir.Path) != 0 { + return + } + + if len(config) == 0 { + ir.Path = "prompt.png" + return + } + + config = filepath.Base(config) + + match := regex.FindNamedRegexMatch(`(\.?)(?P.*)\.(json|yaml|yml|toml|jsonc)`, config) + path := strings.TrimRight(match[str], ".omp") + + if len(path) == 0 { + path = "prompt" + } + + ir.Path = fmt.Sprintf("%s.png", path) +} + func (ir *Renderer) loadFonts() error { var data []byte diff --git a/src/image/image_test.go b/src/image/image_test.go index b35fcc62dc64..11e074306573 100644 --- a/src/image/image_test.go +++ b/src/image/image_test.go @@ -1,88 +1,36 @@ package image import ( - stdOS "os" - "path/filepath" "testing" - "github.com/jandedobbeleer/oh-my-posh/src/runtime" - "github.com/jandedobbeleer/oh-my-posh/src/shell" - "github.com/jandedobbeleer/oh-my-posh/src/terminal" - "github.com/stretchr/testify/assert" ) -var cases = []struct { - Case string - Config string -}{ - {Case: ".omp.json suffix", Config: "~/jandedobbeleer.omp.json"}, - {Case: ".omp.yaml suffix", Config: "~/jandedobbeleer.omp.yaml"}, - {Case: ".omp.yml suffix", Config: "~/jandedobbeleer.omp.yml"}, - {Case: ".omp.toml suffix", Config: "~/jandedobbeleer.omp.toml"}, - {Case: ".json suffix", Config: "~/jandedobbeleer.json"}, - {Case: ".yaml suffix", Config: "~/jandedobbeleer.yaml"}, - {Case: ".yml suffix", Config: "~/jandedobbeleer.yml"}, - {Case: ".toml suffix", Config: "~/jandedobbeleer.toml"}, -} - -func runImageTest(config, content string) (string, error) { - poshImagePath := "jandedobbeleer.png" - file, err := stdOS.CreateTemp("", poshImagePath) - if err != nil { - return "", err - } - - defer func() { - _ = stdOS.Remove(file.Name()) - }() - - terminal.Init(shell.GENERIC) - - image := &Renderer{ - AnsiString: content, - } - - env := &runtime.Terminal{ - CmdFlags: &runtime.Flags{ - Config: config, - }, - } - - err = image.Init(env) - if err != nil { - return "", err - } - - err = image.SavePNG() - if err == nil { - _ = stdOS.Remove(image.Path) +func TestSetOutputPath(t *testing.T) { + cases := []struct { + Case string + Config string + Path string + Expected string + }{ + {Case: "default config", Expected: "prompt.png"}, + {Case: "hidden file", Config: ".posh.omp.json", Expected: "posh.png"}, + {Case: "hidden file toml", Config: ".posh.omp.toml", Expected: "posh.png"}, + {Case: "hidden file yaml", Config: ".posh.omp.yaml", Expected: "posh.png"}, + {Case: "hidden file yml", Config: ".posh.omp.yml", Expected: "posh.png"}, + {Case: "path provided", Path: "mytheme.png", Expected: "mytheme.png"}, + {Case: "relative, no omp", Config: "~/jandedobbeleer.json", Expected: "jandedobbeleer.png"}, + {Case: "relative path", Config: "~/jandedobbeleer.omp.json", Expected: "jandedobbeleer.png"}, + {Case: "invalid config name", Config: "~/jandedobbeleer.omp.foo", Expected: "prompt.png"}, } - return filepath.Base(image.Path), err -} - -func TestStringImageFileWithText(t *testing.T) { for _, tc := range cases { - filename, err := runImageTest(tc.Config, "foobar") - if connectionError, ok := err.(*ConnectionError); ok { - t.Log(connectionError.Error()) - continue + image := &Renderer{ + Path: tc.Path, } - assert.Equal(t, "jandedobbeleer.png", filename, tc.Case) - assert.NoError(t, err) - } -} -func TestStringImageFileWithANSI(t *testing.T) { - prompt := ` jan  ` - for _, tc := range cases { - filename, err := runImageTest(tc.Config, prompt) - if connectionError, ok := err.(*ConnectionError); ok { - t.Log(connectionError.Error()) - continue - } - assert.Equal(t, "jandedobbeleer.png", filename, tc.Case) - assert.NoError(t, err) + image.setOutputPath(tc.Config) + + assert.Equal(t, tc.Expected, image.Path, tc.Case) } } diff --git a/website/docs/share-theme.md b/website/docs/share-theme.md index 003c3d50eef7..8c13b2e5eac9 100644 --- a/website/docs/share-theme.md +++ b/website/docs/share-theme.md @@ -13,7 +13,7 @@ Depending on your config, you might have to tweak the output a little bit. ::: The oh-my-posh executable has the `config export image` command to export your current theme configuration -to an image file (.png). +to a PNG image file (if no other options are specified this will be the name of the config file, or `prompt.png`). ```powershell oh-my-posh config export image @@ -23,6 +23,6 @@ There are a couple of additional flags you can use to tweak the image rendering: - `--author`: the name of the creator, added after `ohmyposh.dev` - `--background-color`: the hex background color to use (e.g. `#222222`) -- `--output`: the file to export to (e.g. `theme.png`) +- `--output`: the file to export to (e.g. `mytheme.png`) For all options, and additional examples, use `oh-my-posh config export image --help`