fixed color bug
This commit is contained in:
+1
-1
@@ -42,7 +42,7 @@ func (m *MenuComponent) Render() string {
|
|||||||
if !m.Data.IsActive {
|
if !m.Data.IsActive {
|
||||||
cursor = m.Style.CursorInactive
|
cursor = m.Style.CursorInactive
|
||||||
}
|
}
|
||||||
d, _ := osui.CompressString(m.Style.Cursor, "")
|
d, _ := osui.CompressString(m.Style.Cursor)
|
||||||
empty := strings.Repeat(" ", len(d))
|
empty := strings.Repeat(" ", len(d))
|
||||||
|
|
||||||
for i, item := range m.Items {
|
for i, item := range m.Items {
|
||||||
|
|||||||
@@ -14,66 +14,81 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RenderLine(frameLine, line_ string, x int, fm *map[int]string, lm map[int]string) string {
|
func LogToFile(s string, a ...any) {
|
||||||
frame := []rune(frameLine)
|
existingData, _ := os.ReadFile("log.log")
|
||||||
|
os.WriteFile("log.log", []byte(string(existingData)+"\n"+fmt.Sprintf(s, a...)), 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderLine(frame_, line_ string, x int, fm map[int]string, lm map[int]string) string {
|
||||||
|
var res strings.Builder
|
||||||
|
frame := []rune(frame_)
|
||||||
line := []rune(line_)
|
line := []rune(line_)
|
||||||
|
|
||||||
for i := range frame {
|
for i := range frame {
|
||||||
if i >= x && i-x < len(line) {
|
if i >= x && i-x < len(line) {
|
||||||
frame[i] = line[i-x]
|
// Render character from 'line'
|
||||||
delete(*fm, i)
|
res.WriteRune(line[i-x])
|
||||||
|
|
||||||
|
// Add escape sequence from line's map (lm), if any
|
||||||
if v, ok := lm[i-x]; ok {
|
if v, ok := lm[i-x]; ok {
|
||||||
(*fm)[i] = v
|
res.WriteString(v)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Render character from 'frame'
|
||||||
|
res.WriteRune(frame[i])
|
||||||
|
|
||||||
|
// Add escape sequence from frame's map (fm), if any
|
||||||
|
if v, ok := fm[i]; ok {
|
||||||
|
res.WriteString(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(frame)
|
// Log the result string for debugging
|
||||||
|
// LogToFile("line_: %#v", line_)
|
||||||
|
// LogToFile("lm: %#v", lm)
|
||||||
|
// LogToFile("result: %#v\n", res.String())
|
||||||
|
|
||||||
|
return res.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func CompressString(input string) (string, map[int]string) {
|
||||||
|
pattern := `(\x1b\[([0-9;]*)[a-zA-Z])+`
|
||||||
|
re := regexp.MustCompile(pattern)
|
||||||
|
matchesMap := make(map[int]string)
|
||||||
|
res := []rune{}
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for i < len([]rune(input)) {
|
||||||
|
loc := re.FindStringIndex(string([]rune(input)[i:]))
|
||||||
|
|
||||||
|
if loc != nil && loc[0] == 0 {
|
||||||
|
ansiSeq := re.FindString(string([]rune(input)[i:]))
|
||||||
|
matchesMap[len(res)] = ansiSeq
|
||||||
|
i += len([]rune(ansiSeq))
|
||||||
|
} else {
|
||||||
|
res = append(res, []rune(input)[i])
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(res), matchesMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderOnFrame(c Component, frame *[]string) {
|
func RenderOnFrame(c Component, frame *[]string) {
|
||||||
componentData := c.GetComponentData()
|
componentData := c.GetComponentData()
|
||||||
for i, line := range strings.Split(c.Render(), "\n") {
|
for i, line := range strings.Split(c.Render(), "\n") {
|
||||||
if int(componentData.Y)+i < len(*frame) {
|
if int(componentData.Y)+i < len(*frame) {
|
||||||
fo, fm := CompressString((*frame)[int(componentData.Y)+i], "")
|
fo, fm := CompressString((*frame)[int(componentData.Y)+i])
|
||||||
lo, lm := CompressString(line, "")
|
lo, lm := CompressString(line)
|
||||||
r := RenderLine(fo, lo, int(componentData.X), &fm, lm)
|
LogToFile("line: %#v", line)
|
||||||
// if strings.Contains(line, "info") {
|
LogToFile("Line Map (lm): %#v", lm)
|
||||||
// fmt.Printf("%#v\n", lm)
|
LogToFile("Compressed Line: %#v\n", lo)
|
||||||
// for {
|
(*frame)[int(componentData.Y)+i] = RenderLine(fo, lo, componentData.X, fm, lm)
|
||||||
// }
|
|
||||||
// }
|
|
||||||
(*frame)[int(componentData.Y)+i] = DecompressString(r, fm)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompressString(input, repl string) (string, map[int]string) {
|
|
||||||
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
|
|
||||||
re := regexp.MustCompile(pattern)
|
|
||||||
matches := re.FindAllStringIndex(input, -1)
|
|
||||||
matchesMap := make(map[int]string)
|
|
||||||
n := 0
|
|
||||||
for _, match := range matches {
|
|
||||||
start := match[0]
|
|
||||||
end := match[1]
|
|
||||||
matchesMap[start-n] = input[start:end]
|
|
||||||
n += len(input[start:end])
|
|
||||||
}
|
|
||||||
return re.ReplaceAllString(input, repl), matchesMap
|
|
||||||
}
|
|
||||||
|
|
||||||
func DecompressString(modified string, fm map[int]string) string {
|
|
||||||
res := ""
|
|
||||||
for i, c := range modified {
|
|
||||||
res += string(c)
|
|
||||||
if v, ok := fm[i]; ok {
|
|
||||||
res += v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadKey() (string, error) {
|
func ReadKey() (string, error) {
|
||||||
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user