fix(config): export JSON correctly

resolves #581
This commit is contained in:
Jan De Dobbeleer 2021-03-27 12:04:43 +01:00 committed by Jan De Dobbeleer
parent 61cb33f4f9
commit f90840e3ec
2 changed files with 23 additions and 1 deletions

View file

@ -8,6 +8,8 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/json"
@ -127,7 +129,14 @@ func exportConfig(configFile, format string) string {
var prettyJSON bytes.Buffer
err := json2.Indent(&prettyJSON, buf.Bytes(), "", " ")
if err == nil {
return prettyJSON.String()
unescapeUnicodeCharactersInJSON := func(rawJSON []byte) string {
str, err := strconv.Unquote(strings.ReplaceAll(strconv.Quote(string(rawJSON)), `\\u`, `\u`))
if err != nil {
return err.Error()
}
return str
}
return unescapeUnicodeCharactersInJSON(prettyJSON.Bytes())
}
case config.Yaml:
prefix := "# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n"

13
src/settings_test.go Normal file
View file

@ -0,0 +1,13 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSettingsExportJSON(t *testing.T) {
content := exportConfig("../themes/jandedobbeleer.omp.json", "json")
assert.NotContains(t, content, "\\u003ctransparent\\u003e")
assert.Contains(t, content, "<transparent>")
}