ymtdzzz / otel-tui

A terminal OpenTelemetry viewer inspired by otel-desktop-viewer
Apache License 2.0
214 stars 3 forks source link

feat: Display keymaps corresponding to focused pane, not page #92

Closed ymtdzzz closed 1 month ago

ymtdzzz commented 3 months ago

We need this feature because of the growing of the number of keymaps.

image

ymtdzzz commented 1 month ago

There seems to be two way to implement this feature:

(1) Top down approach

Define the key mapping on the root component.

Code example ```go func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex { // ... page := attatchCommandList2(basePage, []*KeyMaps2{ { c: table, maps: []*KeyMap{ { key: tcell.NewEventKey(tcell.KeyRune, '/', tcell.ModNone), description: "Search traces", }, }, }, { c: search, maps: []*KeyMap{ { key: tcell.NewEventKey(tcell.KeyEsc, ' ', tcell.ModNone), description: "(search) Cancel", }, { key: tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone), description: "(search) Confirm", }, }, }, { c: details, maps: []*KeyMap{ { key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), description: "(detail) Resize side col", }, { key: tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl), description: "(detail) Resize side col", }, }, }, }) // ... func attatchCommandList2(p tview.Primitive, keys []*KeyMaps2) *tview.Flex { command := tview.NewTextView(). SetDynamicColors(true) for _, k := range keys { keytexts := []string{} for _, v := range k.maps { keytexts = append(keytexts, fmt.Sprintf("[yellow]%s[white]: %s", keyMapRegex.ReplaceAllString(v.key.Name(), ""), v.description, )) } k.c.SetFocusFunc(func() { command.SetText(strings.Join(keytexts, " | ")) }) } base := tview.NewFlex().SetDirection(tview.FlexRow) base.AddItem(p, 0, 1, true). AddItem(command, 1, 1, false) return base } ```

(2) Bottom up approach

Pass the command's SetText() from the root component to the constructor of child components and SetFocusFunc() is performed in it.


I think (2) is better because (1) overrides SetFocusFunc() implicitly and that causes unexpected behaviors.

ymtdzzz commented 1 month ago

WIP implementation of (2): https://github.com/ymtdzzz/otel-tui/commit/1e1b90348da0922231c97972cd4246a73c7efdab

focused on tables pane image

focused on search form image

focused on details pane image