Skip to content

Commit

Permalink
Improve tk show UI (grafana#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmholmes authored Jan 5, 2021
1 parent 56c3091 commit 99159db
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pkg/term/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"github.com/rivo/tview"
)

// PageItem represents a single item to be viewed
type PageItem struct {
Name string
Content string
}

// Page shows an application viewer allowing the review of specific resources
func Page(items []PageItem) error {
app := tview.NewApplication()

Expand All @@ -22,13 +24,13 @@ func Page(items []PageItem) error {

// right side: text view
text := tview.NewTextView().SetText(items[0].Content).SetDynamicColors(true)
text.Box = text.Box.SetBorder(true)
text.Box = text.Box.SetBorder(true).SetTitle("Resource")

// left side: resource chooser
list := tview.NewList().
ShowSecondaryText(false).
SetHighlightFullLine(true)
list.Box = list.Box.SetBorder(true).SetTitle("Resources")
list.Box = list.Box.SetBorder(true).SetTitle("Resources").SetBorderAttributes(tcell.AttrBold)

selectItem := func(i int) {
text.SetText(items[i].Content)
Expand All @@ -45,17 +47,29 @@ func Page(items []PageItem) error {

// layout container
flex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(list, 0, 1, false).
AddItem(text, 0, 4, true)
AddItem(list, 0, 1, true).
AddItem(text, 0, 4, false)

isListSelected := true

// custom key handler
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyTAB, tcell.KeyBacktab:
list.InputHandler()(event, nil)
selectItem(list.GetCurrentItem())
case tcell.KeyCtrlC:
return event
case tcell.KeyDown, tcell.KeyUp:
if isListSelected {
list.InputHandler()(event, nil)
selectItem(list.GetCurrentItem())
} else {
text.InputHandler()(event, nil)
}
case tcell.KeyTAB, tcell.KeyRight:
isListSelected = false
app.SetFocus(text)
case tcell.KeyBacktab, tcell.KeyLeft:
isListSelected = true
app.SetFocus(list)
case tcell.KeyCtrlC, tcell.KeyEscape:
return tcell.NewEventKey(tcell.KeyCtrlC, event.Rune(), event.Modifiers())
default:
text.InputHandler()(event, nil)
}
Expand Down

0 comments on commit 99159db

Please sign in to comment.