fix(image): export with correct file name

This commit is contained in:
EDIflyer 2024-06-08 17:51:07 +01:00 committed by Jan De Dobbeleer
parent 31d5de095e
commit 1d72a19694
4 changed files with 48 additions and 79 deletions

View file

@ -88,11 +88,13 @@ func cleanOutputPath(path string, env runtime.Environment) string {
path = strings.TrimPrefix(path, "~") path = strings.TrimPrefix(path, "~")
path = filepath.Join(env.Home(), path) path = filepath.Join(env.Home(), path)
} }
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
if absPath, err := filepath.Abs(path); err == nil { if absPath, err := filepath.Abs(path); err == nil {
path = absPath path = absPath
} }
} }
return filepath.Clean(path) return filepath.Clean(path)
} }

View file

@ -150,10 +150,7 @@ type Renderer struct {
func (ir *Renderer) Init(env runtime.Environment) error { func (ir *Renderer) Init(env runtime.Environment) error {
ir.env = env ir.env = env
if ir.Path == "" { ir.setOutputPath(env.Flags().Config)
match := regex.FindNamedRegexMatch(`.*(\/|\\)(?P<STR>.+)\.(json|yaml|yml|toml)`, env.Flags().Config)
ir.Path = fmt.Sprintf("%s.png", strings.TrimSuffix(match[str], ".omp"))
}
ir.cleanContent() ir.cleanContent()
@ -206,6 +203,28 @@ func (ir *Renderer) Init(env runtime.Environment) error {
return nil 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<STR>.*)\.(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 { func (ir *Renderer) loadFonts() error {
var data []byte var data []byte

View file

@ -1,88 +1,36 @@
package image package image
import ( import (
stdOS "os"
"path/filepath"
"testing" "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" "github.com/stretchr/testify/assert"
) )
var cases = []struct { func TestSetOutputPath(t *testing.T) {
Case string cases := []struct {
Config string Case string
}{ Config string
{Case: ".omp.json suffix", Config: "~/jandedobbeleer.omp.json"}, Path string
{Case: ".omp.yaml suffix", Config: "~/jandedobbeleer.omp.yaml"}, Expected string
{Case: ".omp.yml suffix", Config: "~/jandedobbeleer.omp.yml"}, }{
{Case: ".omp.toml suffix", Config: "~/jandedobbeleer.omp.toml"}, {Case: "default config", Expected: "prompt.png"},
{Case: ".json suffix", Config: "~/jandedobbeleer.json"}, {Case: "hidden file", Config: ".posh.omp.json", Expected: "posh.png"},
{Case: ".yaml suffix", Config: "~/jandedobbeleer.yaml"}, {Case: "hidden file toml", Config: ".posh.omp.toml", Expected: "posh.png"},
{Case: ".yml suffix", Config: "~/jandedobbeleer.yml"}, {Case: "hidden file yaml", Config: ".posh.omp.yaml", Expected: "posh.png"},
{Case: ".toml suffix", Config: "~/jandedobbeleer.toml"}, {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"},
func runImageTest(config, content string) (string, error) { {Case: "relative path", Config: "~/jandedobbeleer.omp.json", Expected: "jandedobbeleer.png"},
poshImagePath := "jandedobbeleer.png" {Case: "invalid config name", Config: "~/jandedobbeleer.omp.foo", Expected: "prompt.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)
}
return filepath.Base(image.Path), err
}
func TestStringImageFileWithText(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
filename, err := runImageTest(tc.Config, "foobar") image := &Renderer{
if connectionError, ok := err.(*ConnectionError); ok { Path: tc.Path,
t.Log(connectionError.Error())
continue
} }
assert.Equal(t, "jandedobbeleer.png", filename, tc.Case)
assert.NoError(t, err)
}
}
func TestStringImageFileWithANSI(t *testing.T) { image.setOutputPath(tc.Config)
prompt := ` jan  `
for _, tc := range cases { assert.Equal(t, tc.Expected, image.Path, tc.Case)
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)
} }
} }

View file

@ -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 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 ```powershell
oh-my-posh config export image 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` - `--author`: the name of the creator, added after `ohmyposh.dev`
- `--background-color`: the hex background color to use (e.g. `#222222`) - `--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` For all options, and additional examples, use `oh-my-posh config export image --help`