Compare commits
3
Commits
f77c04c908
...
v0.1.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e714abddf | ||
|
|
3ceaa13b3f | ||
|
|
8165e80b05 |
@@ -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,67 +14,61 @@ 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 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_)
|
||||||
|
|
||||||
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]
|
if v, ok := lm[i-x]; ok {
|
||||||
delete(*fm, i)
|
res.WriteString(v)
|
||||||
}
|
|
||||||
if v, ok := (*lm)[i-x]; ok {
|
|
||||||
if i-x > 0 {
|
|
||||||
delete(*lm, i-x)
|
|
||||||
(*lm)[i] = 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
|
||||||
|
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) {
|
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)
|
(*frame)[int(componentData.Y)+i] = RenderLine(fo, lo, componentData.X, fm, lm)
|
||||||
(*frame)[int(componentData.Y)+i] = DecompressString(r, fm, lm)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, lm map[int]string) string {
|
|
||||||
res := ""
|
|
||||||
for i, c := range modified {
|
|
||||||
if v, ok := fm[i]; ok {
|
|
||||||
res += v
|
|
||||||
}
|
|
||||||
if v, ok := lm[i]; ok {
|
|
||||||
res += v
|
|
||||||
}
|
|
||||||
res += string(c)
|
|
||||||
}
|
|
||||||
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