5 Commits
Author SHA1 Message Date
selimaj-dev 7e714abddf fixed color bug 2024-10-03 13:19:43 +02:00
selimaj-dev 3ceaa13b3f fixed color bug 2024-10-03 13:10:43 +02:00
selimaj-dev 8165e80b05 fixed ui when horizontal but color bug 2024-10-01 19:53:19 +02:00
selimaj-dev f77c04c908 fixed ui when horizontal 2024-10-01 16:37:54 +02:00
selimaj-dev 27530fe8cc fixed ui when horizontal 2024-10-01 16:34:50 +02:00
3 changed files with 40 additions and 40 deletions
+1 -1
View File
@@ -34,7 +34,6 @@ func NewScreen(c Component) *Screen {
}
func (s *Screen) Render() {
Clear()
width, height := GetTerminalSize()
frame := NewFrame(width, height)
data := s.component.GetComponentData()
@@ -48,6 +47,7 @@ func (s *Screen) Render() {
data.IsActive = true
data.DefaultColor = colors.Reset
RenderOnFrame(s.component, &frame)
Clear()
fmt.Print(strings.Join(frame, "\n"))
}
+1 -1
View File
@@ -42,7 +42,7 @@ func (m *MenuComponent) Render() string {
if !m.Data.IsActive {
cursor = m.Style.CursorInactive
}
d, _ := osui.CompressString(m.Style.Cursor, "")
d, _ := osui.CompressString(m.Style.Cursor)
empty := strings.Repeat(" ", len(d))
for i, item := range m.Items {
+39 -39
View File
@@ -14,60 +14,61 @@ import (
"golang.org/x/term"
)
func renderLine(frameLine, line_ string, x int) string {
frame := []rune(frameLine)
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_)
for i := range frame {
if i >= x && i-x < len(line) {
if v, ok := lm[i-x]; ok {
res.WriteString(v)
}
res.WriteRune(line[i-x])
} else {
if v, ok := fm[i]; ok {
res.WriteString(v)
}
res.WriteRune(frame[i])
}
}
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
v := 0
for j, c := range frame {
if i+j >= x && v < len(line) {
if c != '\b' && line[v] == '\t' {
frame = append(frame[:i+j], append([]rune{' '}, frame[i+j:]...)...)
}
frame[i+j] = line[v]
v++
}
if c == '\b' {
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(frame)
return string(res), matchesMap
}
func RenderOnFrame(c Component, frame *[]string) {
componentData := c.GetComponentData()
for i, lo := range strings.Split(c.Render(), "\n") {
for i, line := range strings.Split(c.Render(), "\n") {
if int(componentData.Y)+i < len(*frame) {
fo := (*frame)[int(componentData.Y)+i]
f, fa := CompressString(fo, "\b")
line, la := CompressString(lo, "\t")
r := renderLine(f, line, int(componentData.X))
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
fo, fm := CompressString((*frame)[int(componentData.Y)+i])
lo, lm := CompressString(line)
(*frame)[int(componentData.Y)+i] = RenderLine(fo, lo, componentData.X, fm, lm)
}
}
}
func CompressString(input, repl string) (string, []string) {
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
re := regexp.MustCompile(pattern)
matches := re.FindAllString(input, -1)
return re.ReplaceAllString(input, repl), matches
}
func DecompressString(modified string, outputArray []string, c string) string {
parts := strings.Split(modified, c)
reconstructed := ""
for i, part := range parts {
reconstructed += part
if i < len(outputArray) {
reconstructed += outputArray[i]
}
}
return reconstructed
}
func ReadKey() (string, error) {
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
@@ -174,7 +175,6 @@ func LogicValueInt(b bool, _if, _else int) int {
return _else
}
// Get the terminal size
func GetTerminalSize() (int, int) {
return consolesize.GetConsoleSize()