fixed ui when horizontal
This commit is contained in:
@@ -14,22 +14,18 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
func renderLine(frameLine, line_ string, x int) string {
|
func RenderLine(frameLine, line_ string, x int, fm *map[int]string, lm *map[int]string) string {
|
||||||
frame := []rune(frameLine)
|
frame := []rune(frameLine)
|
||||||
line := []rune(line_)
|
line := []rune(line_)
|
||||||
|
|
||||||
i := 0
|
for i := range frame {
|
||||||
v := 0
|
if i >= x && i-x < len(line) {
|
||||||
for j, c := range frame {
|
frame[i] = line[i-x]
|
||||||
if i+j >= x && v < len(line) {
|
delete(*fm, i)
|
||||||
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' {
|
if v, ok := (*lm)[i-x]; ok {
|
||||||
i++
|
delete(*lm, i-x)
|
||||||
|
(*lm)[i] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,34 +34,43 @@ func renderLine(frameLine, line_ string, x int) string {
|
|||||||
|
|
||||||
func RenderOnFrame(c Component, frame *[]string) {
|
func RenderOnFrame(c Component, frame *[]string) {
|
||||||
componentData := c.GetComponentData()
|
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) {
|
if int(componentData.Y)+i < len(*frame) {
|
||||||
fo := (*frame)[int(componentData.Y)+i]
|
fo, fm := CompressString((*frame)[int(componentData.Y)+i], "")
|
||||||
f, fa := CompressString(fo, "\b")
|
lo, lm := CompressString(line, "")
|
||||||
line, la := CompressString(lo, "\t")
|
r := RenderLine(fo, lo, int(componentData.X), &fm, &lm)
|
||||||
r := renderLine(f, line, int(componentData.X))
|
(*frame)[int(componentData.Y)+i] = DecompressString(r, fm, lm)
|
||||||
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompressString(input, repl string) (string, []string) {
|
func CompressString(input, repl string) (string, map[int]string) {
|
||||||
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
|
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
|
||||||
re := regexp.MustCompile(pattern)
|
re := regexp.MustCompile(pattern)
|
||||||
matches := re.FindAllString(input, -1)
|
matches := re.FindAllStringIndex(input, -1)
|
||||||
return re.ReplaceAllString(input, repl), matches
|
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, outputArray []string, c string) string {
|
func DecompressString(modified string, fm map[int]string, lm map[int]string) string {
|
||||||
parts := strings.Split(modified, c)
|
res := ""
|
||||||
reconstructed := ""
|
for i, c := range modified {
|
||||||
for i, part := range parts {
|
if v, ok := fm[i]; ok {
|
||||||
reconstructed += part
|
res += v
|
||||||
if i < len(outputArray) {
|
|
||||||
reconstructed += outputArray[i]
|
|
||||||
}
|
}
|
||||||
|
if v, ok := lm[i]; ok {
|
||||||
|
res += v
|
||||||
|
}
|
||||||
|
res += string(c)
|
||||||
}
|
}
|
||||||
return reconstructed
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadKey() (string, error) {
|
func ReadKey() (string, error) {
|
||||||
@@ -174,7 +179,6 @@ func LogicValueInt(b bool, _if, _else int) int {
|
|||||||
return _else
|
return _else
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get the terminal size
|
// Get the terminal size
|
||||||
func GetTerminalSize() (int, int) {
|
func GetTerminalSize() (int, int) {
|
||||||
return consolesize.GetConsoleSize()
|
return consolesize.GetConsoleSize()
|
||||||
|
|||||||
Reference in New Issue
Block a user