82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package highlight
|
|
|
|
import (
|
|
"strings"
|
|
"timshome.page/gilo/terminal"
|
|
)
|
|
|
|
// ------------------------------------------------------------------
|
|
// Syntax Type -> Color Mapping
|
|
// ------------------------------------------------------------------
|
|
|
|
var syntaxColorMap = map[int]string{
|
|
Comment: terminal.FGCyan,
|
|
String: terminal.FGMagenta,
|
|
Number: terminal.FGRed,
|
|
Match: terminal.FGBlue,
|
|
Normal: terminal.DefaultFGColor,
|
|
}
|
|
|
|
// SyntaxToColor Take a highlighting type and map it to
|
|
// an ANSI color escape code for display
|
|
func SyntaxToColor(hl int) string {
|
|
color := syntaxColorMap[hl]
|
|
if len(color) == 0 {
|
|
color = terminal.DefaultFGColor
|
|
}
|
|
|
|
return color
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// File Type -> Syntax Mapping
|
|
// ------------------------------------------------------------------
|
|
|
|
type Syntax struct {
|
|
FileType string
|
|
FileMatch []string
|
|
LineCommentStart string
|
|
Flags int
|
|
}
|
|
|
|
// HLDB - The "database" of syntax types
|
|
var HLDB = []*Syntax{{
|
|
"c",
|
|
[]string{".c", ".h", ".cpp"},
|
|
"//",
|
|
HighlightNumbers | HighlightStrings,
|
|
}, {
|
|
"go",
|
|
[]string{".go", "go.mod"},
|
|
"//",
|
|
HighlightNumbers | HighlightStrings,
|
|
}, {
|
|
"makefile",
|
|
[]string{"Makefile", "makefile", "justfile"},
|
|
"#",
|
|
0,
|
|
}}
|
|
|
|
func GetSyntaxByFilename(filename string) *Syntax {
|
|
if filename == "" {
|
|
return nil
|
|
}
|
|
|
|
var ext string = ""
|
|
extInd := strings.LastIndex(filename, ".")
|
|
if extInd > -1 {
|
|
ext = filename[extInd:len(filename)]
|
|
}
|
|
|
|
for i := 0; i < len(HLDB); i++ {
|
|
s := HLDB[i]
|
|
for j := range s.FileMatch {
|
|
if s.FileMatch[j] == ext || strings.Contains(filename, s.FileMatch[j]) {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|