fix(config): export glyphs correctly escaped

This commit is contained in:
Jan De Dobbeleer 2022-02-01 16:40:15 +01:00 committed by Jan De Dobbeleer
parent ece3a97a33
commit 01bd42b3eb
2 changed files with 31 additions and 34 deletions

View file

@ -10,7 +10,6 @@ import (
"oh-my-posh/environment"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gookit/config/v2"
@ -136,20 +135,6 @@ func (cfg *Config) Export(format string) string {
cfg.format = format
}
unicodeEscape := func(s string) string {
var builder strings.Builder
for _, r := range s {
if r < 0x1000 {
builder.WriteRune(r)
continue
}
quoted := strconv.QuoteRune(r)
quoted = strings.Trim(quoted, "'")
builder.WriteString(quoted)
}
return builder.String()
}
config.AddDriver(yaml.Driver)
config.AddDriver(toml.Driver)
@ -163,7 +148,7 @@ func (cfg *Config) Export(format string) string {
jsonEncoder.SetIndent("", " ")
err := jsonEncoder.Encode(data)
cfg.exitWithError(err)
return unicodeEscape(result.String())
return escapeGlyphs(result.String())
}
_, err := config.DumpTo(&result, cfg.format)
@ -175,7 +160,7 @@ func (cfg *Config) Export(format string) string {
case TOML:
prefix = "#:schema https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n"
}
return prefix + unicodeEscape(result.String())
return prefix + escapeGlyphs(result.String())
}
func (cfg *Config) Write() {
@ -188,3 +173,16 @@ func (cfg *Config) Write() {
cfg.exitWithError(err)
}
}
func escapeGlyphs(s string) string {
var builder strings.Builder
for _, r := range s {
if r < 0x1000 {
builder.WriteRune(r)
continue
}
quoted := fmt.Sprintf("\\u%04x", r)
builder.WriteString(quoted)
}
return builder.String()
}

View file

@ -9,23 +9,6 @@ import (
"github.com/stretchr/testify/assert"
)
// func TestSettingsExportJSON(t *testing.T) {
// defer testClearDefaultConfig()
// configFile := "../../themes/jandedobbeleer.omp.json"
// debug := false
// args := &environment.Args{
// Config: &configFile,
// Debug: &debug,
// Eval: &debug,
// }
// env := &environment.ShellEnvironment{}
// env.Init(args)
// cfg := LoadConfig(env)
// content := cfg.Export()
// assert.NotContains(t, content, "\\u003ctransparent\\u003e")
// assert.Contains(t, content, "<transparent>")
// }
func testClearDefaultConfig() {
config.Default().ClearAll()
}
@ -55,3 +38,19 @@ func TestParseMappedLocations(t *testing.T) {
assert.Equal(t, "two", mappedLocations["folder2"])
}
}
func TestEscapeGlyphs(t *testing.T) {
defer testClearDefaultConfig()
cases := []struct {
Input string
Expected string
}{
{Input: "a", Expected: "a"},
{Input: "\ue0b4", Expected: "\\ue0b4"},
{Input: "\ufd03", Expected: "\\ufd03"},
{Input: "}", Expected: "}"},
}
for _, tc := range cases {
assert.Equal(t, tc.Expected, escapeGlyphs(tc.Input), tc.Input)
}
}