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 = filepath.Join(env.Home(), path)
}
if !filepath.IsAbs(path) {
if absPath, err := filepath.Abs(path); err == nil {
path = absPath
}
}
return filepath.Clean(path)
}

View file

@ -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<STR>.+)\.(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<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 {
var data []byte

View file

@ -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 {
func TestSetOutputPath(t *testing.T) {
cases := []struct {
Case string
Config string
Path string
Expected 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"},
{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"},
}
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)
for _, tc := range cases {
image := &Renderer{
AnsiString: content,
Path: tc.Path,
}
env := &runtime.Terminal{
CmdFlags: &runtime.Flags{
Config: config,
},
}
image.setOutputPath(tc.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 {
filename, err := runImageTest(tc.Config, "foobar")
if connectionError, ok := err.(*ConnectionError); ok {
t.Log(connectionError.Error())
continue
}
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)
assert.Equal(t, tc.Expected, image.Path, tc.Case)
}
}

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
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`