98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package gfishline
|
|
|
|
import (
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.zaclys.com/bvaudour/gob/format"
|
|
"gitea.zaclys.com/bvaudour/gob/shell/scanner"
|
|
)
|
|
|
|
var (
|
|
specialChars = []string{"=", "*", "+", "-", ">", "<", "|", ">>", "<<"}
|
|
)
|
|
|
|
func isString(arg string) bool {
|
|
if len(arg) > 0 && (arg[0] == '\'' || arg[0] == '"') {
|
|
sc := scanner.NewScanner(strings.NewReader(arg))
|
|
return sc.Scan()
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func isIncompleteString(arg string) bool {
|
|
if len(arg) > 0 && (arg[0] == '\'' || arg[0] == '"') {
|
|
sc := scanner.NewScanner(strings.NewReader(arg))
|
|
return !sc.Scan()
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func isNumber(arg string) bool {
|
|
_, err := strconv.ParseInt(arg, 10, 64)
|
|
if err != nil {
|
|
_, err = strconv.ParseFloat(arg, 64)
|
|
}
|
|
|
|
return err == nil
|
|
}
|
|
|
|
type FormatFunc func(in []string) (out []string)
|
|
|
|
func NoFormat(in []string) (out []string) {
|
|
return in
|
|
}
|
|
|
|
func DefaultFormat(commands, options []string) FormatFunc {
|
|
return func(in []string) (out []string) {
|
|
out = make([]string, len(in))
|
|
for i, word := range in {
|
|
var formatOptions []string
|
|
switch {
|
|
case i == 0 && len(commands) > 0:
|
|
if slices.Contains(commands, word) {
|
|
formatOptions = append(formatOptions, "bold")
|
|
} else if isString(word) {
|
|
formatOptions = append(formatOptions, "yellow")
|
|
} else {
|
|
formatOptions = append(formatOptions, "l_red", "normal")
|
|
}
|
|
case i > 0 && len(options) > 0 && slices.Contains(options, word):
|
|
formatOptions = append(formatOptions, "l_green", "normal")
|
|
case isString(word):
|
|
formatOptions = append(formatOptions, "yellow")
|
|
case isIncompleteString(word):
|
|
formatOptions = append(formatOptions, "l_red", "normal")
|
|
case isNumber(word):
|
|
formatOptions = append(formatOptions, "l_yellow", "normal")
|
|
case slices.Contains(specialChars, word):
|
|
formatOptions = append(formatOptions, "l_blue", "normal")
|
|
}
|
|
|
|
if len(formatOptions) > 0 {
|
|
out[i] = format.Apply(word, formatOptions...)
|
|
} else {
|
|
out[i] = word
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
type formater struct {
|
|
*splitter
|
|
}
|
|
|
|
func newFormater(buffer string, cursor int, cb FormatFunc) *formater {
|
|
f := formater{
|
|
splitter: newSplitter(buffer, cursor),
|
|
}
|
|
f.words = cb(f.words)
|
|
|
|
return &f
|
|
}
|