Revise example.

This commit is contained in:
Jesse Portnoy
2023-07-18 17:18:48 +01:00
parent 21c71649f6
commit 57b5bfb135
2 changed files with 99 additions and 57 deletions
+59 -40
View File
@@ -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")
}
}
```
+40 -17
View File
@@ -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")
}
}