Skip to content

Commit

Permalink
improvements to microliner, multiline strings, colors
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Jun 8, 2024
1 parent b96f610 commit 838f66c
Showing 1 changed file with 52 additions and 32 deletions.
84 changes: 52 additions & 32 deletions util/microliner.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ func (s *MLState) emitNewLine() {

// State represents an open terminal
type MLState struct {
needRefresh bool
next <-chan KeyEvent
sendBack func(msg string)
enterLine func(line string) string
history []string
historyMutex sync.RWMutex
columns int
needRefresh bool
next <-chan KeyEvent
sendBack func(msg string)
enterLine func(line string) string
history []string
historyMutex sync.RWMutex
columns int
inString bool
lastLineString bool
// killRing *ring.Ring
// completer WordCompleter
// pending []rune
Expand Down Expand Up @@ -285,7 +287,13 @@ func (s *MLState) yank(p []rune, text []rune, pos int) ([]rune, int, interface{}

func trace(t any) {
if false {
trace(t)
fmt.Println(t)
}
}

func trace2(t any) {
if true {
fmt.Println(t)
}
}

Expand Down Expand Up @@ -313,7 +321,11 @@ func (s *MLState) refreshSingleLine(prompt []rune, buf []rune, pos int) error {
if true { // pLen+bLen < s.columns {
// _, err = fmt.Print(VerySimpleRyeHighlight(string(buf)))
// s.cursorPos(0)
s.sendBack(RyeHighlight(string(buf)))
text, inString := RyeHighlight(string(buf), s.lastLineString)
s.sendBack(text)
trace("*************** IN STRING: ******************++")
trace(inString)
s.inString = inString
trace(pLen + pos)
s.cursorPos(pLen + pos)
trace("SETTING CURSOR POS AFER HIGHLIGHT")
Expand Down Expand Up @@ -549,8 +561,13 @@ startOfHere:
switch next.Code {
case 13: // Enter
historyStale = true
s.lastLineString = false
// trace2("NL")
if len(line) > 0 && unicode.IsSpace(line[len(line)-1]) {
s.sendBack(fmt.Sprintf("%s⏎\n\r%s", color_background_red, reset))
s.sendBack(fmt.Sprintf("%s⏎\n\r%s", color_emph, reset))
if s.inString {
s.lastLineString = true
}
} else {
s.sendBack("\n\r")
}
Expand Down Expand Up @@ -1052,12 +1069,12 @@ const white = "\x1b[37m"
const reset = "\x1b[0m"
const reset2 = "\033[39;49m"

const color_word = "\x1b[38;5;45m"
const color_word2 = "\033[38;5;214m"
const color_num2 = "\033[38;5;202m"
const color_string2 = "\033[38;5;148m"
const color_comment = "\033[38;5;247m"
const color_background_red = "\033[41m"
const color_word1 = cyan
const color_word2 = yellow
const color_num2 = magenta
const color_string2 = green
const color_comment = dim + white
const color_emph = bright

type HighlightedStringBuilder struct {
b strings.Builder
Expand All @@ -1071,23 +1088,23 @@ func (h *HighlightedStringBuilder) String() string {
return h.b.String()
}

func (h *HighlightedStringBuilder) ColoredString() string {
return h.getColor() + h.b.String() + reset
func (h *HighlightedStringBuilder) ColoredString(inStr bool) string {
return h.getColor(inStr) + h.b.String() + reset
}

func (h *HighlightedStringBuilder) Reset() {
h.b.Reset()
}

func (h *HighlightedStringBuilder) getColor() string {
func (h *HighlightedStringBuilder) getColor(inStr bool) string {
s := h.b.String()
if len(s) == 0 {
return ""
}
if strings.HasPrefix(s, ";") {
return color_comment
}
if hasPrefixMultiple(s, "\"", "`") {
if inStr || hasPrefixMultiple(s, "\"", "`") {
return color_string2
}
if strings.HasPrefix(s, "%") && len(s) != 1 {
Expand All @@ -1101,19 +1118,19 @@ func (h *HighlightedStringBuilder) getColor() string {
if strings.HasPrefix(s, ":") {
if strings.HasPrefix(s, "::") {
if len(s) != 2 {
return color_background_red
return color_emph + color_word1
}
} else if len(s) != 1 {
return color_word2
return color_word1
}
}
if strings.HasSuffix(s, ":") {
if strings.HasSuffix(s, "::") {
if len(s) != 2 {
return color_background_red
return color_emph + color_word1
}
} else if len(s) != 1 {
return color_word2
return color_word1
}
}
if unicode.IsNumber(rune(s[0])) {
Expand All @@ -1123,8 +1140,8 @@ func (h *HighlightedStringBuilder) getColor() string {
if strings.Contains(s, ":https://") {
return color_string2
}
if strings.HasSuffix(s, "!") {
return color_background_red
if strings.HasSuffix(s, "!") || strings.HasPrefix(s, "set-") {
return color_emph + color_word2
}
return color_word2
}
Expand All @@ -1140,11 +1157,13 @@ func hasPrefixMultiple(s string, prefixes ...string) bool {
return false
}

func RyeHighlight(s string) string {
func RyeHighlight(s string, inStrX bool) (string, bool) {
var fullB strings.Builder
var hb HighlightedStringBuilder

var inComment, inStr1, inStr2 bool
inStr1 = inStrX

for _, c := range s {
if inComment {
hb.WriteRune(c)
Expand All @@ -1154,8 +1173,9 @@ func RyeHighlight(s string) string {
} else if c == '"' {
hb.WriteRune(c)
if inStr1 {
// trace2(".")
fullB.WriteString(hb.ColoredString(inStr1))
inStr1 = false
fullB.WriteString(hb.ColoredString())
hb.Reset()
} else {
inStr1 = true
Expand All @@ -1164,21 +1184,21 @@ func RyeHighlight(s string) string {
hb.WriteRune(c)
if inStr2 {
inStr2 = false
fullB.WriteString(hb.ColoredString())
fullB.WriteString(hb.ColoredString(inStr1))
hb.Reset()
} else {
inStr2 = true
}
} else if unicode.IsSpace(c) && !inComment && !inStr1 && !inStr2 {
fullB.WriteString(hb.ColoredString())
fullB.WriteString(hb.ColoredString(inStr1))
hb.Reset()

fullB.WriteRune(c)
} else {
hb.WriteRune(c)
}
}
fullB.WriteString(hb.ColoredString())
fullB.WriteString(hb.ColoredString(inStr1))
hb.Reset()
return fullB.String()
return fullB.String(), inStr1
}

0 comments on commit 838f66c

Please sign in to comment.