Skip to content

Commit

Permalink
internal: fix parsing for structures
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Apr 9, 2021
1 parent 282da01 commit efcebeb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ func Parse(text string) string {
}

if s == '}' {
countBracketInRow := 0
for i := 0; ; i++ {
if index+i < formatLen && text[index+i] != '}' {
break
}
countBracketInRow++
}

if countBracketInRow > 2 {
index += countBracketInRow - 2 - 1
continue
}

endBracketFound = true
continue
}
Expand Down
7 changes: 7 additions & 0 deletions tests/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"github.com/i582/cfmt"
)

type TestStruct struct {
field1 string
field2 int
}

func TestParse(t *testing.T) {
cfmt.RegisterStyle("code", func(s string) string {
return cfmt.Sprintf("{{%s}}::red|underline", s)
Expand All @@ -20,4 +25,6 @@ func TestParse(t *testing.T) {
cfmt.Println(cfmt.Sprint("{{blink group}}::blink"))
cfmt.Printf("{{hex %s}}::#ff00ff sfas\n", "color group")
cfmt.Printf(cfmt.Sprintf("{{background color %s}}::bg#ffff00\n", "hex color"))
cfmt.Printf("{{{hello}}}::red|underline\n")
cfmt.Printf("{{some test struct: %v}}::red|underline\n", TestStruct{"hello", 1})
}

0 comments on commit efcebeb

Please sign in to comment.