Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e714abddf | ||
|
|
3ceaa13b3f | ||
|
|
8165e80b05 | ||
|
|
f77c04c908 | ||
|
|
27530fe8cc |
@@ -34,7 +34,6 @@ func NewScreen(c Component) *Screen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Render() {
|
func (s *Screen) Render() {
|
||||||
Clear()
|
|
||||||
width, height := GetTerminalSize()
|
width, height := GetTerminalSize()
|
||||||
frame := NewFrame(width, height)
|
frame := NewFrame(width, height)
|
||||||
data := s.component.GetComponentData()
|
data := s.component.GetComponentData()
|
||||||
@@ -48,6 +47,7 @@ func (s *Screen) Render() {
|
|||||||
data.IsActive = true
|
data.IsActive = true
|
||||||
data.DefaultColor = colors.Reset
|
data.DefaultColor = colors.Reset
|
||||||
RenderOnFrame(s.component, &frame)
|
RenderOnFrame(s.component, &frame)
|
||||||
|
Clear()
|
||||||
fmt.Print(strings.Join(frame, "\n"))
|
fmt.Print(strings.Join(frame, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+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,60 +14,61 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
func renderLine(frameLine, line_ string, x int) string {
|
func RenderLine(frame_, line_ string, x int, fm map[int]string, lm map[int]string) string {
|
||||||
frame := []rune(frameLine)
|
var res strings.Builder
|
||||||
|
frame := []rune(frame_)
|
||||||
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 {
|
if v, ok := lm[i-x]; ok {
|
||||||
if i+j >= x && v < len(line) {
|
res.WriteString(v)
|
||||||
if c != '\b' && line[v] == '\t' {
|
|
||||||
frame = append(frame[:i+j], append([]rune{' '}, frame[i+j:]...)...)
|
|
||||||
}
|
}
|
||||||
frame[i+j] = line[v]
|
res.WriteRune(line[i-x])
|
||||||
v++
|
} else {
|
||||||
|
if v, ok := fm[i]; ok {
|
||||||
|
res.WriteString(v)
|
||||||
|
}
|
||||||
|
res.WriteRune(frame[i])
|
||||||
}
|
}
|
||||||
if c == '\b' {
|
}
|
||||||
|
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++
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(frame)
|
return string(res), matchesMap
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
(*frame)[int(componentData.Y)+i] = RenderLine(fo, lo, componentData.X, fm, lm)
|
||||||
r := renderLine(f, line, int(componentData.X))
|
|
||||||
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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 {
|
||||||
@@ -174,7 +175,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