From 2e5fa3f1d29c966b77166440d5fe278ebe5d9342 Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Fri, 6 May 2022 00:06:31 +0800 Subject: [PATCH] feat(image): add `-o, --output` flag --- docs/docs/share-theme.md | 4 ++-- src/cli/config_export.go | 4 ++-- src/cli/config_export_image.go | 9 ++++++++ src/engine/image.go | 10 +++++---- src/engine/image_test.go | 40 +++++++++++++++++++++++++++------- 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/docs/docs/share-theme.md b/docs/docs/share-theme.md index c93a5037..8964bb88 100644 --- a/docs/docs/share-theme.md +++ b/docs/docs/share-theme.md @@ -13,13 +13,13 @@ 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 the current directory. +to an image file (.png). ```powershell oh-my-posh config export image --cursor-padding 50 ``` -There are a couple of additional switches you can use to tweak the image rendering: +There are a couple of additional flags you can use to tweak the image rendering: - `--cursor-padding`: spaces to add after the cursor indication (`_`) - `--rprompt-offset`: spaces to add **before** a block that's right aligned diff --git a/src/cli/config_export.go b/src/cli/config_export.go index 9ce6377e..53c28283 100644 --- a/src/cli/config_export.go +++ b/src/cli/config_export.go @@ -66,8 +66,8 @@ func cleanOutputPath(path string, env environment.Environment) string { path = filepath.Join(env.Home(), path) } if !filepath.IsAbs(path) { - if absConfigFile, err := filepath.Abs(path); err == nil { - path = absConfigFile + if absPath, err := filepath.Abs(path); err == nil { + path = absPath } } return filepath.Clean(path) diff --git a/src/cli/config_export_image.go b/src/cli/config_export_image.go index 51f9e8ab..047a46e7 100644 --- a/src/cli/config_export_image.go +++ b/src/cli/config_export_image.go @@ -16,6 +16,7 @@ var ( cursorPadding int rPromptOffset int bgColor string + outputImage string ) // imageCmd represents the image command @@ -37,6 +38,10 @@ Example usage: Exports the config to an image file called myconfig.png in the current working directory. +> oh-my-posh config export image --config ~/myconfig.omp.json --output ~/mytheme.png + +Exports the config to an image file ~/mytheme.png. + > oh-my-posh config export image --config ~/myconfig.omp.json --author "John Doe" Exports the config to an image file using customized output options.`, @@ -81,6 +86,9 @@ Exports the config to an image file using customized output options.`, BgColor: bgColor, Ansi: ansi, } + if outputImage != "" { + imageCreator.Path = cleanOutputPath(outputImage, env) + } imageCreator.Init(env.Flags().Config) err := imageCreator.SavePNG() if err != nil { @@ -94,5 +102,6 @@ func init() { // nolint:gochecknoinits imageCmd.Flags().StringVar(&bgColor, "background-color", "", "image background color") imageCmd.Flags().IntVar(&cursorPadding, "cursor-padding", 0, "prompt cursor padding") imageCmd.Flags().IntVar(&rPromptOffset, "rprompt-offset", 0, "right prompt offset") + imageCmd.Flags().StringVarP(&outputImage, "output", "o", "", "image file (.png) to export to") exportCmd.AddCommand(imageCmd) } diff --git a/src/engine/image.go b/src/engine/image.go index 71d733c9..efee113e 100644 --- a/src/engine/image.go +++ b/src/engine/image.go @@ -105,7 +105,7 @@ type ImageRenderer struct { BgColor string Ansi *color.Ansi - path string + Path string factor float64 @@ -136,8 +136,10 @@ type ImageRenderer struct { } func (ir *ImageRenderer) Init(config string) { - match := regex.FindNamedRegexMatch(`.*(\/|\\)(?P.+).omp.(json|yaml|toml)`, config) - ir.path = fmt.Sprintf("%s.png", match[str]) + if ir.Path == "" { + match := regex.FindNamedRegexMatch(`.*(\/|\\)(?P.+)\.(json|yaml|yml|toml)`, config) + ir.Path = fmt.Sprintf("%s.png", strings.TrimSuffix(match[str], ".omp")) + } f := 2.0 @@ -446,7 +448,7 @@ func (ir *ImageRenderer) SavePNG() error { x += w } - return dc.SavePNG(ir.path) + return dc.SavePNG(ir.Path) } func (ir *ImageRenderer) shouldPrint() bool { diff --git a/src/engine/image_test.go b/src/engine/image_test.go index 904676b7..f58cd149 100644 --- a/src/engine/image_test.go +++ b/src/engine/image_test.go @@ -5,16 +5,31 @@ import ( "oh-my-posh/color" "oh-my-posh/shell" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" ) -func runImageTest(content string) error { +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 := ioutil.TempFile("", poshImagePath) if err != nil { - return err + return "", err } defer os.Remove(file.Name()) ansi := &color.Ansi{} @@ -23,18 +38,27 @@ func runImageTest(content string) error { AnsiString: content, Ansi: ansi, } - image.Init("~/jandedobbeleer.omp.json") + image.Init(config) err = image.SavePNG() - return err + if err == nil { + os.Remove(image.Path) + } + return filepath.Base(image.Path), err } func TestStringImageFileWithText(t *testing.T) { - err := runImageTest("foobar") - assert.NoError(t, err) + for _, tc := range cases { + filename, err := runImageTest(tc.Config, "foobar") + assert.Equal(t, "jandedobbeleer.png", filename, tc.Case) + assert.NoError(t, err) + } } func TestStringImageFileWithANSI(t *testing.T) { prompt := ` jan  ` - err := runImageTest(prompt) - assert.NoError(t, err) + for _, tc := range cases { + filename, err := runImageTest(tc.Config, prompt) + assert.Equal(t, "jandedobbeleer.png", filename, tc.Case) + assert.NoError(t, err) + } }