Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
ymtdzzz committed Aug 20, 2024
1 parent e53fd27 commit 1e1b903
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 65 deletions.
78 changes: 78 additions & 0 deletions tuiexporter/internal/tui/component/commands.go
Original file line number Diff line number Diff line change
@@ -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()
}
})
}
117 changes: 55 additions & 62 deletions tuiexporter/internal/tui/component/page.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package component

import (
"fmt"
"log"
"regexp"
"strings"

"github.com/gdamore/tcell/v2"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 := ""
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
13 changes: 12 additions & 1 deletion tuiexporter/internal/tui/component/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions tuiexporter/internal/tui/component/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
}

0 comments on commit 1e1b903

Please sign in to comment.