From b572d517efcc1bb0714d18f9b69345f034cf7b68 Mon Sep 17 00:00:00 2001 From: "Y.Matsuda" Date: Sun, 22 Dec 2024 15:50:30 +0900 Subject: [PATCH] Show full log in modal on Enter --- tuiexporter/internal/tui/component/log.go | 5 + .../internal/tui/component/log_test.go | 4 + tuiexporter/internal/tui/component/modal.go | 95 +++++++++++++++++++ .../internal/tui/component/timeline.go | 4 +- tuiexporter/internal/tui/component/tree.go | 47 --------- 5 files changed, 107 insertions(+), 48 deletions(-) create mode 100644 tuiexporter/internal/tui/component/modal.go delete mode 100644 tuiexporter/internal/tui/component/tree.go diff --git a/tuiexporter/internal/tui/component/log.go b/tuiexporter/internal/tui/component/log.go index 664a734..5d70eb7 100644 --- a/tuiexporter/internal/tui/component/log.go +++ b/tuiexporter/internal/tui/component/log.go @@ -30,6 +30,11 @@ func NewLogDataForTable(logs *[]*telemetry.LogData) LogDataForTable { } } +// implementation for tableModalMapper interface +func (l *LogDataForTable) GetColumnIdx() int { + return len(logTableHeader) - 1 +} + // implementations for tview Virtual Table // see: https://github.com/rivo/tview/wiki/VirtualTable func (l LogDataForTable) GetCell(row, column int) *tview.TableCell { diff --git a/tuiexporter/internal/tui/component/log_test.go b/tuiexporter/internal/tui/component/log_test.go index a6e4b94..19338b7 100644 --- a/tuiexporter/internal/tui/component/log_test.go +++ b/tuiexporter/internal/tui/component/log_test.go @@ -160,6 +160,10 @@ func TestLogDataForTable(t *testing.T) { }) } }) + + t.Run("tableModalMapper GetColumnIdx", func(t *testing.T) { + assert.Equal(t, "RawData", logTableHeader[ldftable.GetColumnIdx()]) + }) } func TestGetLogInfoTree(t *testing.T) { diff --git a/tuiexporter/internal/tui/component/modal.go b/tuiexporter/internal/tui/component/modal.go new file mode 100644 index 0000000..e006ea0 --- /dev/null +++ b/tuiexporter/internal/tui/component/modal.go @@ -0,0 +1,95 @@ +package component + +import ( + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +type showModalFunc func(tview.Primitive, string) *tview.TextView + +type hideModalFunc func(tview.Primitive) + +const MODAL_TITLE = "Scroll (Ctrl+J, Ctrl+K)" + +func attachModalForTreeAttributes(tree *tview.TreeView, showFn showModalFunc, hideFn hideModalFunc) { + var currentModalNode *tview.TreeNode = nil + tree.SetSelectedFunc(func(node *tview.TreeNode) { + if len(node.GetChildren()) > 0 { + node.SetExpanded(!node.IsExpanded()) + return + } + if currentModalNode == node { + hideFn(tree) + currentModalNode = nil + return + } + textView := showFn(tree, node.GetText()) + textView.SetTitle(MODAL_TITLE) + currentModalNode = node + tree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyCtrlJ: + row, col := textView.GetScrollOffset() + textView.ScrollTo(row+1, col) + return nil + case tcell.KeyCtrlK: + row, col := textView.GetScrollOffset() + textView.ScrollTo(row-1, col) + return nil + } + return event + }) + }) + tree.SetChangedFunc(func(node *tview.TreeNode) { + if currentModalNode != nil { + hideFn(tree) + currentModalNode = nil + } + }) +} + +type tableModalMapper interface { + // GetColumnIdx returns the column index for getting the content to be shown + // in the modal + GetColumnIdx() int +} + +func attachModalForTableRows(table *tview.Table, mapper tableModalMapper, showFn showModalFunc, hideFn hideModalFunc) { + if mapper == nil { + return + } + + var currentRow = -1 + + table.SetSelectedFunc(func(row, column int) { + if currentRow == row { + hideFn(table) + currentRow = -1 + return + } + currentRow = row + if cell := table.GetCell(row, mapper.GetColumnIdx()); cell != nil { + textView := showFn(table, cell.Text) + textView.SetTitle(MODAL_TITLE) + table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyCtrlJ: + row, col := textView.GetScrollOffset() + textView.ScrollTo(row+1, col) + return nil + case tcell.KeyCtrlK: + row, col := textView.GetScrollOffset() + textView.ScrollTo(row-1, col) + return nil + } + return event + }) + } + }) + table.SetSelectionChangedFunc(func(row, column int) { + if currentRow != -1 { + hideFn(table) + currentRow = -1 + } + }) +} diff --git a/tuiexporter/internal/tui/component/timeline.go b/tuiexporter/internal/tui/component/timeline.go index a2d532c..f1e81dd 100644 --- a/tuiexporter/internal/tui/component/timeline.go +++ b/tuiexporter/internal/tui/component/timeline.go @@ -194,7 +194,9 @@ func DrawTimeline(commands *tview.TextView, showModalFn showModalFunc, hideModal logCount := 0 if lds, ok := lcache.GetLogsByTraceID(traceID); ok { logCount = len(lds) - logs.SetContent(NewLogDataForTable(&lds)) + logData := NewLogDataForTable(&lds) + logs.SetContent(&logData) + attachModalForTableRows(logs, &logData, showModalFn, hideModalFn) } logs.SetBorder(true).SetTitle(fmt.Sprintf("Logs (l) -- %d logs found (L to toggle collapse)", logCount)) registerCommandList(commands, logs, nil, KeyMaps{ diff --git a/tuiexporter/internal/tui/component/tree.go b/tuiexporter/internal/tui/component/tree.go deleted file mode 100644 index e116683..0000000 --- a/tuiexporter/internal/tui/component/tree.go +++ /dev/null @@ -1,47 +0,0 @@ -package component - -import ( - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" -) - -type showModalFunc func(tview.Primitive, string) *tview.TextView - -type hideModalFunc func(tview.Primitive) - -func attachModalForTreeAttributes(tree *tview.TreeView, showFn showModalFunc, hideFn hideModalFunc) { - var currentModalNode *tview.TreeNode = nil - tree.SetSelectedFunc(func(node *tview.TreeNode) { - if len(node.GetChildren()) > 0 { - node.SetExpanded(!node.IsExpanded()) - return - } - if currentModalNode == node { - hideFn(tree) - currentModalNode = nil - return - } - textView := showFn(tree, node.GetText()) - textView.SetTitle("Scroll (Ctrl+J, Ctrl+K)") - currentModalNode = node - tree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { - switch event.Key() { - case tcell.KeyCtrlJ: - row, col := textView.GetScrollOffset() - textView.ScrollTo(row+1, col) - return nil - case tcell.KeyCtrlK: - row, col := textView.GetScrollOffset() - textView.ScrollTo(row-1, col) - return nil - } - return event - }) - }) - tree.SetChangedFunc(func(node *tview.TreeNode) { - if currentModalNode != nil { - hideFn(tree) - currentModalNode = nil - } - }) -}