From 1e1b90348da0922231c97972cd4246a73c7efdab Mon Sep 17 00:00:00 2001 From: "Y.Matsuda" Date: Tue, 20 Aug 2024 23:27:43 +0900 Subject: [PATCH] tmp --- .../internal/tui/component/commands.go | 78 ++++++++++++ tuiexporter/internal/tui/component/page.go | 117 ++++++++---------- tuiexporter/internal/tui/component/trace.go | 13 +- .../internal/tui/component/trace_test.go | 4 +- 4 files changed, 147 insertions(+), 65 deletions(-) create mode 100644 tuiexporter/internal/tui/component/commands.go diff --git a/tuiexporter/internal/tui/component/commands.go b/tuiexporter/internal/tui/component/commands.go new file mode 100644 index 0000000..61f8b61 --- /dev/null +++ b/tuiexporter/internal/tui/component/commands.go @@ -0,0 +1,78 @@ +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 attatchCommandList(p tview.Primitive, keys KeyMaps) *tview.Flex { + command := tview.NewTextView(). + SetDynamicColors(true). + SetText(keys.keyTexts()) + + base := tview.NewFlex().SetDirection(tview.FlexRow) + base.AddItem(p, 0, 1, true). + AddItem(command, 1, 1, false) + + return base +} + +func newCommandList() *tview.TextView { + return tview.NewTextView(). + SetDynamicColors(true) +} + +func attatchCommandList2(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() + } + }) +} diff --git a/tuiexporter/internal/tui/component/page.go b/tuiexporter/internal/tui/component/page.go index 14ce115..d272f0c 100644 --- a/tuiexporter/internal/tui/component/page.go +++ b/tuiexporter/internal/tui/component/page.go @@ -1,9 +1,7 @@ package component import ( - "fmt" "log" - "regexp" "strings" "github.com/gdamore/tcell/v2" @@ -27,15 +25,6 @@ const ( DEFAULT_PROPORTION_LOG_TABLE = 30 ) -var keyMapRegex = regexp.MustCompile(`Rune|\[|\]`) - -type KeyMap struct { - key *tcell.EventKey - description string -} - -type KeyMaps []*KeyMap - type TUIPages struct { pages *tview.Pages traces *tview.Flex @@ -119,6 +108,7 @@ func (p *TUIPages) registerPages(store *telemetry.Store) { } func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex { + commands := newCommandList() basePage := tview.NewFlex().SetDirection(tview.FlexColumn) tableContainer := tview.NewFlex().SetDirection(tview.FlexRow) @@ -172,6 +162,16 @@ func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex { } return event }) + registerCommandList(commands, table, nil, KeyMaps{ + { + key: tcell.NewEventKey(tcell.KeyRune, '/', tcell.ModNone), + description: "Search traces", + }, + { + key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), + description: "Clear all data", + }, + }) input := "" inputConfirmed := "" @@ -196,13 +196,23 @@ func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex { } p.setFocusFn(table) }) + registerCommandList(commands, search, nil, KeyMaps{ + { + key: tcell.NewEventKey(tcell.KeyEsc, ' ', tcell.ModNone), + description: "Cancel", + }, + { + key: tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone), + description: "Confirm", + }, + }) table.SetSelectionChangedFunc(func(row, _ int) { if row == 0 { return } details.Clear() - details.AddItem(getTraceInfoTree(store.GetFilteredServiceSpansByIdx(row-1)), 0, 1, true) + details.AddItem(getTraceInfoTree(commands, store.GetFilteredServiceSpansByIdx(row-1)), 0, 1, true) log.Printf("selected row(original): %d", row) }) tableContainer. @@ -235,36 +245,39 @@ func (p *TUIPages) createTracePage(store *telemetry.Store) *tview.Flex { return event }) - page := attatchCommandList(basePage, KeyMaps{ - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), - description: "(traces) Clear all data", - }, - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyRune, '/', tcell.ModNone), - description: "Search traces", - }, - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyEsc, ' ', tcell.ModNone), - description: "(search) Cancel", - }, - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone), - description: "(search) Confirm", - }, - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), - description: "(detail) Resize side col", - }, - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl), - description: "(detail) Resize side col", - }, - &KeyMap{ - key: tcell.NewEventKey(tcell.KeyF12, ' ', tcell.ModNone), - description: "(debug) Toggle Log", - }, - }) + page := attatchCommandList2(commands, basePage) + /* + page := attatchCommandList(basePage, KeyMaps{ + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), + description: "(traces) Clear all data", + }, + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyRune, '/', tcell.ModNone), + description: "Search traces", + }, + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyEsc, ' ', tcell.ModNone), + description: "(search) Cancel", + }, + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyEnter, ' ', tcell.ModNone), + description: "(search) Confirm", + }, + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), + description: "(detail) Resize side col", + }, + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl), + description: "(detail) Resize side col", + }, + &KeyMap{ + key: tcell.NewEventKey(tcell.KeyF12, ' ', tcell.ModNone), + description: "(debug) Toggle Log", + }, + }) + */ page = attatchTab(page, PAGE_TRACES) return page @@ -703,23 +716,3 @@ func attatchTab(p tview.Primitive, name string) *tview.Flex { return base } - -func attatchCommandList(p tview.Primitive, keys KeyMaps) *tview.Flex { - keytexts := []string{} - for _, v := range keys { - keytexts = append(keytexts, fmt.Sprintf("[yellow]%s[white]: %s", - keyMapRegex.ReplaceAllString(v.key.Name(), ""), - v.description, - )) - } - - command := tview.NewTextView(). - SetDynamicColors(true). - SetText(strings.Join(keytexts, " | ")) - - base := tview.NewFlex().SetDirection(tview.FlexRow) - base.AddItem(p, 0, 1, true). - AddItem(command, 1, 1, false) - - return base -} diff --git a/tuiexporter/internal/tui/component/trace.go b/tuiexporter/internal/tui/component/trace.go index f4019e9..d36d9ec 100644 --- a/tuiexporter/internal/tui/component/trace.go +++ b/tuiexporter/internal/tui/component/trace.go @@ -82,7 +82,7 @@ func getCellFromSpan(span *telemetry.SpanData, column int) *tview.TableCell { return tview.NewTableCell(text) } -func getTraceInfoTree(spans []*telemetry.SpanData) *tview.TreeView { +func getTraceInfoTree(commands *tview.TextView, spans []*telemetry.SpanData) *tview.TreeView { if len(spans) == 0 { return tview.NewTreeView() } @@ -136,6 +136,17 @@ func getTraceInfoTree(spans []*telemetry.SpanData) *tview.TreeView { node.SetExpanded(!node.IsExpanded()) }) + registerCommandList(commands, tree, nil, KeyMaps{ + { + key: tcell.NewEventKey(tcell.KeyRune, 'L', tcell.ModCtrl), + description: "Resize side col", + }, + { + key: tcell.NewEventKey(tcell.KeyRune, 'H', tcell.ModCtrl), + description: "Resize side col", + }, + }) + return tree } diff --git a/tuiexporter/internal/tui/component/trace_test.go b/tuiexporter/internal/tui/component/trace_test.go index 5a7dfcd..aaba760 100644 --- a/tuiexporter/internal/tui/component/trace_test.go +++ b/tuiexporter/internal/tui/component/trace_test.go @@ -148,7 +148,7 @@ func TestGetTraceInfoTree(t *testing.T) { screen.Init() screen.SetSize(sw, sh) - gottree := getTraceInfoTree(spans) + gottree := getTraceInfoTree(nil, spans) gottree.SetRect(0, 0, sw, sh) gottree.Draw(screen) screen.Sync() @@ -192,5 +192,5 @@ func TestGetTraceInfoTree(t *testing.T) { } func TestGetTraceInfoTreeNoSpans(t *testing.T) { - assert.Nil(t, getTraceInfoTree(nil).GetRoot()) + assert.Nil(t, getTraceInfoTree(nil, nil).GetRoot()) }