Skip to content

Commit

Permalink
Show full log in modal on Enter
Browse files Browse the repository at this point in the history
  • Loading branch information
ymtdzzz committed Dec 22, 2024
1 parent 1948626 commit b572d51
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 48 deletions.
5 changes: 5 additions & 0 deletions tuiexporter/internal/tui/component/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions tuiexporter/internal/tui/component/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
95 changes: 95 additions & 0 deletions tuiexporter/internal/tui/component/modal.go
Original file line number Diff line number Diff line change
@@ -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
}
})
}
4 changes: 3 additions & 1 deletion tuiexporter/internal/tui/component/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
47 changes: 0 additions & 47 deletions tuiexporter/internal/tui/component/tree.go

This file was deleted.

0 comments on commit b572d51

Please sign in to comment.