|
|
|
@@ -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()
|
|
|
|
|