2023-10-03 17:02:22 -04:00
|
|
|
package highlight
|
|
|
|
|
2023-10-04 14:22:51 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"timshome.page/gilo/terminal"
|
|
|
|
)
|
|
|
|
|
2023-10-05 11:03:08 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Syntax Highlighting Constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
const (
|
|
|
|
Normal = iota
|
|
|
|
Comment
|
|
|
|
Keyword1
|
|
|
|
Keyword2
|
|
|
|
String
|
|
|
|
Number
|
|
|
|
Match
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DoNumbers = 1 << 0
|
|
|
|
DoStrings = 1 << 1
|
|
|
|
)
|
|
|
|
|
2023-10-04 14:22:51 -04:00
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Syntax Type -> Color Mapping
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
var syntaxColorMap = map[int]string{
|
2023-10-05 11:03:08 -04:00
|
|
|
Comment: terminal.FGCyan,
|
|
|
|
Keyword1: terminal.FGYellow,
|
|
|
|
Keyword2: terminal.FGGreen,
|
|
|
|
String: terminal.FGBrightMagenta,
|
|
|
|
Number: terminal.FGRed,
|
|
|
|
Match: terminal.FGBlue,
|
|
|
|
Normal: terminal.DefaultFGColor,
|
2023-10-04 14:22:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// ------------------------------------------------------------------
|
2023-10-04 10:37:47 -04:00
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
type Syntax struct {
|
2023-10-04 15:08:04 -04:00
|
|
|
FileType string
|
|
|
|
FileMatch []string
|
|
|
|
LineCommentStart string
|
2023-10-05 11:03:08 -04:00
|
|
|
Keywords1 []string
|
|
|
|
Keywords2 []string
|
2023-10-04 15:08:04 -04:00
|
|
|
Flags int
|
2023-10-03 17:02:22 -04:00
|
|
|
}
|
2023-10-04 10:37:47 -04:00
|
|
|
|
|
|
|
// HLDB - The "database" of syntax types
|
|
|
|
var HLDB = []*Syntax{{
|
|
|
|
"c",
|
|
|
|
[]string{".c", ".h", ".cpp"},
|
2023-10-04 15:08:04 -04:00
|
|
|
"//",
|
2023-10-05 11:03:08 -04:00
|
|
|
[]string{"switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "union", "typedef", "static", "enum", "class", "case"},
|
|
|
|
[]string{"int", "long", "double", "float", "char", "unsigned", "signed", "void"},
|
|
|
|
DoNumbers | DoStrings,
|
2023-10-04 10:37:47 -04:00
|
|
|
}, {
|
|
|
|
"go",
|
|
|
|
[]string{".go", "go.mod"},
|
2023-10-04 15:08:04 -04:00
|
|
|
"//",
|
2023-10-05 11:03:08 -04:00
|
|
|
[]string{"break", "case", "chan", "const", "continue", "default", "defer", "else", "fallthrough", "for", "func", "go", "goto", "if", "import", "interface", "map", "package", "range", "return", "select", "struct", "switch", "type", "var", "iota"},
|
|
|
|
[]string{"bool", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float32", "float64", "complex64", "complex128", "byte", "rune", "uint", "int", "uintptr", "string", "struct", "type", "map"},
|
|
|
|
DoNumbers | DoStrings,
|
2023-10-04 10:37:47 -04:00
|
|
|
}, {
|
|
|
|
"makefile",
|
2023-10-04 15:08:04 -04:00
|
|
|
[]string{"Makefile", "makefile", "justfile"},
|
|
|
|
"#",
|
2023-10-05 11:03:08 -04:00
|
|
|
[]string{},
|
|
|
|
[]string{},
|
2023-10-04 10:37:47 -04:00
|
|
|
0,
|
|
|
|
}}
|
|
|
|
|
|
|
|
func GetSyntaxByFilename(filename string) *Syntax {
|
|
|
|
if filename == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:08:04 -04:00
|
|
|
var ext string = ""
|
2023-10-04 10:37:47 -04:00
|
|
|
extInd := strings.LastIndex(filename, ".")
|
2023-10-04 15:08:04 -04:00
|
|
|
if extInd > -1 {
|
|
|
|
ext = filename[extInd:len(filename)]
|
|
|
|
}
|
2023-10-04 10:37:47 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|