From 57b5bfb1350a72f2dcbfe702d4e1979cafb08926 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Tue, 18 Jul 2023 17:18:48 +0100 Subject: [PATCH] Revise example. --- README.md | 99 +++++++++++++++++++++++++++------------------- examples/syncat.go | 57 ++++++++++++++++++-------- 2 files changed, 99 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 27e635b..0823a03 100644 --- a/README.md +++ b/README.md @@ -87,49 +87,68 @@ func helloWorld() { // Now we will print the string lines := strings.Split(inputString, "\n") for lineN, l := range lines { - for colN, c := range l { - // Check if the group changed at the current position - if group, ok := matches[lineN][colN]; ok { - switch group { - case highlight.Groups["statement"]: - color.Set(color.FgGreen) - // There are more possible groups available than just these ones - case highlight.Groups["statement"]: - color.Set(color.FgGreen) - case highlight.Groups["identifier"]: - color.Set(color.FgBlue) - case highlight.Groups["preproc"]: - color.Set(color.FgHiRed) - case highlight.Groups["special"]: - color.Set(color.FgRed) - case highlight.Groups["constant.string"]: - color.Set(color.FgCyan) - case highlight.Groups["constant"]: - color.Set(color.FgCyan) - case highlight.Groups["constant.specialChar"]: - color.Set(color.FgHiMagenta) - case highlight.Groups["type"]: - color.Set(color.FgYellow) - case highlight.Groups["constant.number"]: - color.Set(color.FgCyan) - case highlight.Groups["comment"]: - color.Set(color.FgHiGreen) - default: + colN := 0 + for _, c := range l { + if group, ok := matches[lineN][colN]; ok { + switch group { + case highlight.Groups["statement"]: + fallthrough + case highlight.Groups["green"]: + color.Set(color.FgGreen) + + case highlight.Groups["identifier"]: + fallthrough + case highlight.Groups["blue"]: + color.Set(color.FgHiBlue) + + case highlight.Groups["preproc"]: + //fallthrough + //case highlight.Groups["high.red"]: + color.Set(color.FgHiRed) + + case highlight.Groups["special"]: + fallthrough + case highlight.Groups["red"]: + color.Set(color.FgRed) + + case highlight.Groups["constant.string"]: + fallthrough + case highlight.Groups["constant"]: + fallthrough + case highlight.Groups["constant.number"]: + fallthrough + case highlight.Groups["cyan"]: + color.Set(color.FgCyan) + + case highlight.Groups["constant.specialChar"]: + fallthrough + case highlight.Groups["magenta"]: + color.Set(color.FgHiMagenta) + + case highlight.Groups["type"]: + fallthrough + case highlight.Groups["yellow"]: + color.Set(color.FgYellow) + + case highlight.Groups["comment"]: + fallthrough + case highlight.Groups["high.green"]: + color.Set(color.FgHiGreen) + default: + color.Unset() + } + } + fmt.Print(string(c)) + colN++ + } + if group, ok := matches[lineN][colN]; ok { + if group == highlight.Groups["default"] || group == highlight.Groups[""] { color.Unset() } - } - // Print the character - fmt.Print(string(c)) - } - // This is at a newline, but highlighting might have been turned off at the very end of the line so we should check that. - if group, ok := matches[lineN][len(l)]; ok { - if group == highlight.Groups["default"] || group == highlight.Groups[""] { - color.Unset() - } - } + } - color.Unset() - fmt.Print("\n") + color.Unset() + fmt.Print("\n") } } ``` diff --git a/examples/syncat.go b/examples/syncat.go index 0a371b0..5deecf9 100644 --- a/examples/syncat.go +++ b/examples/syncat.go @@ -22,12 +22,11 @@ func main() { var syn_dir string if gopath == "" { - syn_dir = "/etc/highlight" + syn_dir = os.Getenv("HOME") + "/.config/zaje/syntax_files" } else { syn_dir = gopath + "/src/github.com/jessp01/gohighlight/syntax_files" } - syn_dir = os.Getenv("SYNDIR") var defs []*highlight.Def err := highlight.ParseSyntaxFiles(syn_dir, &defs) if err != nil { @@ -53,28 +52,51 @@ func main() { colN := 0 for _, c := range l { if group, ok := matches[lineN][colN]; ok { - // There are more possible groups available than just these ones - if group == highlight.Groups["statement"] { + switch group { + case highlight.Groups["statement"]: + fallthrough + case highlight.Groups["green"]: color.Set(color.FgGreen) - } else if group == highlight.Groups["identifier"] { - color.Set(color.FgBlue) - } else if group == highlight.Groups["preproc"] { + + case highlight.Groups["identifier"]: + fallthrough + case highlight.Groups["blue"]: + color.Set(color.FgHiBlue) + + case highlight.Groups["preproc"]: + //fallthrough + //case highlight.Groups["high.red"]: color.Set(color.FgHiRed) - } else if group == highlight.Groups["special"] { + + case highlight.Groups["special"]: + fallthrough + case highlight.Groups["red"]: color.Set(color.FgRed) - } else if group == highlight.Groups["constant.string"] { + + case highlight.Groups["constant.string"]: + fallthrough + case highlight.Groups["constant"]: + fallthrough + case highlight.Groups["constant.number"]: + fallthrough + case highlight.Groups["cyan"]: color.Set(color.FgCyan) - } else if group == highlight.Groups["constant"] { - color.Set(color.FgCyan) - } else if group == highlight.Groups["constant.specialChar"] { + + case highlight.Groups["constant.specialChar"]: + fallthrough + case highlight.Groups["magenta"]: color.Set(color.FgHiMagenta) - } else if group == highlight.Groups["type"] { + + case highlight.Groups["type"]: + fallthrough + case highlight.Groups["yellow"]: color.Set(color.FgYellow) - } else if group == highlight.Groups["constant.number"] { - color.Set(color.FgCyan) - } else if group == highlight.Groups["comment"] { + + case highlight.Groups["comment"]: + fallthrough + case highlight.Groups["high.green"]: color.Set(color.FgHiGreen) - } else { + default: color.Unset() } } @@ -87,6 +109,7 @@ func main() { } } + color.Unset() fmt.Print("\n") } }