Skip to content

Commit

Permalink
internal: fixed a bug when outputting a string without stylized gave …
Browse files Browse the repository at this point in the history
…an empty string
  • Loading branch information
i582 committed Nov 17, 2020
1 parent 52f4054 commit 131bf09
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
14 changes: 7 additions & 7 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ func Parse(format string) string {
}
}

if len(lastToken) != 0 {
if lastIsFormatGroup {
lastToken = groupStyle(format, lastToken, string(tempToken))
resParts = append(resParts, lastToken)
} else {
resParts = append(resParts, string(tempToken))
}
if lastIsFormatGroup && len(lastToken) != 0 {
lastToken = groupStyle(format, lastToken, string(tempToken))
resParts = append(resParts, lastToken)
}

if !lastIsFormatGroup && len(tempToken) != 0 {
resParts = append(resParts, string(tempToken))
}

return strings.Join(resParts, "")
Expand Down
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,17 @@ func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {

// Printf is the same as fmt.
func Printf(format string, a ...interface{}) (n int, err error) {
text := Sprintf(format, a...)
return fmt.Print(text)
return Fprintf(os.Stdout, format, a...)
}

// Fatalf is the same as fmt.
func Fatalf(format string, a ...interface{}) {
text := Sprintf(format, a...)
fmt.Print(text)
Printf(format, a...)
os.Exit(1)
}

// Fatal is the same as fmt.
func Fatal(a ...interface{}) {
text := Sprint(a...)
fmt.Print(text)
Print(a...)
os.Exit(1)
}
20 changes: 12 additions & 8 deletions tests/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
)

func TestParse(t *testing.T) {
cfmt.Println("{{correct group}}::red|underline")
cfmt.RegisterStyle("code", func(s string) string {
return cfmt.Sprintf("{{%s}}::red|underline", s)
})

cfmt.Println("{{correct group}}::code")
cfmt.Println("{{correct group}}::red|underline and {{other}}::red")
cfmt.Println("{{error group}} ")
cfmt.Println("{{overline group}}::overline")
cfmt.Println("{{reverse group}}::reverse")
cfmt.Println("{{faint group}}::faint")
cfmt.Println("{{blink group}}::blink")
cfmt.Println("{{hex color group}}::#ff00ff")
cfmt.Println("{{background color hex group}}::bg#ffff00")
cfmt.Print("{{error group}} \n")
cfmt.Print("{{overline group}}::overline\n")
cfmt.Print("{{reverse group}}::reverse\n")
cfmt.Print(cfmt.Sprintln("{{faint group}}::faint"))
cfmt.Println(cfmt.Sprint("{{blink group}}::blink"))
cfmt.Printf("{{hex %s}}::#ff00ff\n", "color group")
cfmt.Printf(cfmt.Sprintf("{{background color %s}}::bg#ffff00", "hex color"))
}

0 comments on commit 131bf09

Please sign in to comment.