mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-26 19:39:39 -08:00
fix(config): export glyphs correctly escaped
This commit is contained in:
parent
ece3a97a33
commit
01bd42b3eb
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue