Skip to content

Commit

Permalink
Merge pull request #142 from ymtdzzz/feature/keymaps_for_each_panes
Browse files Browse the repository at this point in the history
Display keymaps corresponding to focused pane, not page
  • Loading branch information
ymtdzzz authored Aug 21, 2024
2 parents e53fd27 + 11232f7 commit ead26ff
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 189 deletions.
66 changes: 66 additions & 0 deletions tuiexporter/internal/tui/component/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package component

import (
"fmt"
"regexp"
"strings"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

var keyMapRegex = regexp.MustCompile(`Rune|\[|\]`)

type KeyMap struct {
key *tcell.EventKey
description string
}

type KeyMaps []*KeyMap

func (m KeyMaps) keyTexts() string {
keytexts := []string{}
for _, v := range m {
keytexts = append(keytexts, fmt.Sprintf(" [yellow]%s[white]: %s",
keyMapRegex.ReplaceAllString(v.key.Name(), ""),
v.description,
))
}
return strings.Join(keytexts, " | ")
}

type Focusable interface {
SetFocusFunc(func()) *tview.Box
}

func newCommandList() *tview.TextView {
return tview.NewTextView().
SetDynamicColors(true)
}

func attatchCommandList(commands *tview.TextView, p tview.Primitive) *tview.Flex {
base := tview.NewFlex().SetDirection(tview.FlexRow)

if commands == nil {
return base
}

base.AddItem(p, 0, 1, true).
AddItem(commands, 1, 1, false)

return base
}

func registerCommandList(commands *tview.TextView, c Focusable, origFocusFn func(), keys KeyMaps) {
if commands == nil {
return
}

c.SetFocusFunc(func() {
commands.SetText(keys.keyTexts())

if origFocusFn != nil {
origFocusFn()
}
})
}
18 changes: 17 additions & 1 deletion tuiexporter/internal/tui/component/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
"fmt"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/ymtdzzz/otel-tui/tuiexporter/internal/telemetry"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func getCellFromLog(log *telemetry.LogData, column int) *tview.TableCell {
return tview.NewTableCell(text)
}

func getLogInfoTree(l *telemetry.LogData, tcache *telemetry.TraceCache, drawTimelineFn func(traceID string)) *tview.TreeView {
func getLogInfoTree(commands *tview.TextView, l *telemetry.LogData, tcache *telemetry.TraceCache, drawTimelineFn func(traceID string)) *tview.TreeView {
if l == nil {
return tview.NewTreeView()
}
Expand Down Expand Up @@ -167,5 +168,20 @@ func getLogInfoTree(l *telemetry.LogData, tcache *telemetry.TraceCache, drawTime
node.SetExpanded(!node.IsExpanded())
})

registerCommandList(commands, tree, nil, KeyMaps{
{
key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl),
description: "Reduce the width",
},
{
key: tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl),
description: "Expand the width",
},
{
key: tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone),
description: "Toggle folding the child nodes",
},
})

return tree
}
2 changes: 1 addition & 1 deletion tuiexporter/internal/tui/component/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestGetLogInfoTree(t *testing.T) {
screen.Init()
screen.SetSize(sw, sh)

gottree := getLogInfoTree(logs[0], nil, nil)
gottree := getLogInfoTree(nil, logs[0], nil, nil)
gottree.SetRect(0, 0, sw, sh)
gottree.Draw(screen)
screen.Sync()
Expand Down
21 changes: 19 additions & 2 deletions tuiexporter/internal/tui/component/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func getCellFromMetrics(metric *telemetry.MetricData, column int) *tview.TableCe
return tview.NewTableCell(text)
}

func getMetricInfoTree(m *telemetry.MetricData) *tview.TreeView {
func getMetricInfoTree(commands *tview.TextView, m *telemetry.MetricData) *tview.TreeView {
if m == nil {
return nil
}
Expand Down Expand Up @@ -369,6 +369,21 @@ func getMetricInfoTree(m *telemetry.MetricData) *tview.TreeView {
node.SetExpanded(!node.IsExpanded())
})

registerCommandList(commands, tree, nil, KeyMaps{
{
key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl),
description: "Reduce the width",
},
{
key: tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl),
description: "Expand the width",
},
{
key: tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone),
description: "Toggle folding the child nodes",
},
})

return tree
}

Expand All @@ -380,7 +395,7 @@ func (a ByTimestamp) Less(i, j int) bool {
return a[i].Timestamp().AsTime().Before(a[j].Timestamp().AsTime())
}

func drawMetricChartByRow(store *telemetry.Store, row int) tview.Primitive {
func drawMetricChartByRow(commands *tview.TextView, store *telemetry.Store, row int) tview.Primitive {
m := store.GetFilteredMetricByIdx(row)
mcache := store.GetMetricCache()
sname := "N/A"
Expand Down Expand Up @@ -542,6 +557,8 @@ func drawMetricChartByRow(store *telemetry.Store, row int) tview.Primitive {

chart.AddItem(ch, 0, 7, true).AddItem(legend, 0, 3, false)

registerCommandList(commands, ch, nil, KeyMaps{})

return chart
}

Expand Down
Loading

0 comments on commit ead26ff

Please sign in to comment.