add content and externalInputHandlers

This commit is contained in:
aditya-K2
2023-04-09 02:18:36 +05:30
parent a4313360a7
commit 5cc0d44f2b
2 changed files with 32 additions and 18 deletions

View File

@@ -2,7 +2,6 @@ package ui
import (
"errors"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@@ -12,21 +11,28 @@ var (
berr = errors.New("Couldn't Get Base Selection in Interactive View")
defaultfg = tcell.ColorGreen
defaultbg = tcell.ColorDefault
defaultstyle = tcell.StyleDefault.
Defaultstyle = tcell.StyleDefault.
Foreground(defaultfg).
Background(defaultbg)
)
type Content struct {
Content string
Style tcell.Style
}
type _range struct {
Start int
End int
}
type interactiveView struct {
visual bool
vrange *_range
baseSel int
View *tview.Table
visual bool
vrange *_range
baseSel int
content func() [][]Content
externalCapture func(e *tcell.EventKey) *tcell.EventKey
View *tview.Table
}
func NewInteractiveView() *interactiveView {
@@ -49,6 +55,14 @@ func NewInteractiveView() *interactiveView {
return i
}
func (i *interactiveView) SetContentFunc(f func() [][]Content) {
i.content = f
}
func (i *interactiveView) SetExternalCapture(f func(e *tcell.EventKey) *tcell.EventKey) {
i.externalCapture = f
}
func (i *interactiveView) exitVisualMode() {
if i.vrange.Start < i.baseSel {
i.View.Select(i.vrange.Start, -1)
@@ -185,6 +199,8 @@ func (i *interactiveView) capture(e *tcell.EventKey) *tcell.EventKey {
{
if e.Key() == tcell.KeyEscape {
return i.getHandler("exitvisual")(e)
} else if i.externalCapture != nil {
return i.externalCapture(e)
}
return e
}
@@ -198,20 +214,18 @@ func GetCell(text string, st tcell.Style) *tview.TableCell {
}
func (i *interactiveView) update() {
s := strings.Split("orem ipsum dolor sit amet, consectetur adipiscing elit. Nunc nec leo a tellus gravida convallis. Curabitur tempus purus nisi. Proin non enim convallis augue porta aliquet.", " ")
i.View.Clear()
for j := range s {
s := i.content()
for x := range s {
b := ""
if i.visual && (j >= i.vrange.Start && j <= i.vrange.End) {
if i.visual && (x >= i.vrange.Start && x <= i.vrange.End) {
b = "[blue::]█[::]"
}
i.View.SetCell(j, 0,
GetCell(b, defaultstyle))
i.View.SetCell(j, 1,
GetCell(s[j], defaultstyle))
i.View.SetCell(j, 2,
GetCell(s[j], defaultstyle.Foreground(tcell.ColorBlue)))
i.View.SetCell(j, 3,
GetCell(s[j], defaultstyle.Foreground(tcell.ColorYellow)))
i.View.SetCell(x, 0,
GetCell(b, Defaultstyle))
for y := range s[x] {
i.View.SetCell(x, y+1,
GetCell(s[x][y].Content, s[x][y].Style))
}
}
}

View File

@@ -40,7 +40,7 @@ func (c *menu) ContentHandler() {
}
for k := range c.content {
c.Menu.SetCell(k+1, 0,
GetCell(c.content[k], defaultstyle))
GetCell(c.content[k], Defaultstyle))
}
}