Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #57 from dty1er/bug/dash_in_val
Browse files Browse the repository at this point in the history
fix bug: leading dash is unintendedly removed in yaml printer
  • Loading branch information
Hidetatsu Yaginuma authored Apr 27, 2021
2 parents 2b28875 + 4f8240a commit 6020a1e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
11 changes: 11 additions & 0 deletions manifests/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: kubecolor
spec:
containers:
- name: main
image: remrain/tiny-hello
args:
- -test
9 changes: 4 additions & 5 deletions printer/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (yp *YamlPrinter) printLineAsYamlFormat(line string, w io.Writer, dark bool
trimmedLine := strings.TrimLeft(line, " ")

if yp.inString {
// fmt.Println(trimmedLine)
// if inString is true, the line must be a part of a string which is broken into several lines
fmt.Fprintf(w, "%s%s\n", indent, yp.toColorizedStringValue(trimmedLine, dark))
yp.inString = !yp.isStringClosed(trimmedLine)
Expand Down Expand Up @@ -59,8 +58,8 @@ func (yp *YamlPrinter) printLineAsYamlFormat(line string, w io.Writer, dark bool
func (yp *YamlPrinter) toColorizedYamlKey(key string, indentCnt, basicWidth int, dark bool) string {
hasColon := strings.HasSuffix(key, ":")
hasLeadingDash := strings.HasPrefix(key, "- ")
key = strings.TrimRight(key, ":")
key = strings.TrimLeft(key, "- ")
key = strings.TrimSuffix(key, ":")
key = strings.TrimPrefix(key, "- ")

format := "%s"
if hasColon {
Expand All @@ -81,10 +80,10 @@ func (yp *YamlPrinter) toColorizedYamlValue(value string, dark bool) string {
}

hasLeadingDash := strings.HasPrefix(value, "- ")
value = strings.TrimLeft(value, "- ")
value = strings.TrimPrefix(value, "- ")

isDoubleQuoted := strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)
trimmedValue := strings.TrimRight(strings.TrimLeft(value, `"`), `"`)
trimmedValue := strings.TrimSuffix(strings.TrimPrefix(value, `"`), `"`)

var format string
switch {
Expand Down
23 changes: 23 additions & 0 deletions printer/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ func Test_YamlPrinter_Print(t *testing.T) {
- sleep 30
`),
},
{
name: "a value contains dash",
darkBackground: true,
input: testutil.NewHereDoc(`
apiVersion: v1
items:
- apiVersion: v1
key:
- key2: 415
key3: true
key4:
key: -val`),
expected: testutil.NewHereDoc(`
apiVersion: v1
items:
- apiVersion: v1
key:
- key2: 415
key3: true
key4:
key: -val
`),
},
{
name: "a long string which is broken into several lines can be colored",
darkBackground: true,
Expand Down

0 comments on commit 6020a1e

Please sign in to comment.