Skip to content

Commit

Permalink
Revised layouts, status widgets (#46)
Browse files Browse the repository at this point in the history
* Revised tview layout

* Clean-up

* Fix some sizing issues and bugs with termdash
  • Loading branch information
spacez320 authored Feb 27, 2024
1 parent ea01952 commit ae9593a
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 148 deletions.
108 changes: 36 additions & 72 deletions internal/lib/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import (

"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/sparkline"
"github.com/mum4k/termdash/widgets/text"
"github.com/rivo/tview"
)

// Represents the display driver.
type DisplayDriver int

// Represents the display mode.
type DisplayMode int

// Display driver constants. Each display mode uses a specific display driver.
const (
DISPLAY_RAW DisplayDriver = iota + 1 // Used for direct output.
Expand Down Expand Up @@ -50,12 +56,6 @@ var (
interruptChan = make(chan bool) // Channel for interrupting displays.
)

// Represents the display driver.
type DisplayDriver int

// Represents the display mode.
type DisplayMode int

// Starts the display. Applies contextual logic depending on the provided display driver. Expects a
// function to execute within a goroutine to update the display.
func display(driver DisplayDriver, displayUpdateFunc func()) {
Expand All @@ -78,15 +78,6 @@ func displayQuit() {
close(interruptChan)
}

// Creates help text for any display.
func helpText() string {
return HELP_TEXT + fmt.Sprintf(
"\nQuery: %v | Labels: %v | Filters: %v",
currentCtx.Value("query"),
store.GetLabels(currentCtx.Value("query").(string)),
currentCtx.Value("filters"))
}

// Presents raw output.
func RawDisplay(query string) {
var (
Expand All @@ -109,38 +100,30 @@ func RawDisplay(query string) {
}

// Update the results pane with new results as they are generated.
func StreamDisplay(query string, showHelp, showLogs bool) {
func StreamDisplay(query string, filters, labels []string, showHelp, showLogs bool) {
var (
reader = readerIndexes[query] // Reader index for the query.
)

// Wait for the first result to appear to synchronize storage.
// ait for the first result to appear to synchronize storage.
GetResultWait(query)
reader.Dec()

// Initialize the display.
resultsView, helpView, logsView, flexBox := initDisplayTviewText(helpText())

// Hide displays we don't want to show.
if !showHelp {
flexBox.RemoveItem(helpView)
}
if !showLogs {
flexBox.RemoveItem(logsView)
}
widgets := initDisplayTviewText(query, filters, labels, showHelp, showLogs)

// Start the display.
display(
DISPLAY_TVIEW,
func() {
// Print labels as the first line.
appTview.QueueUpdateDraw(func() {
fmt.Fprintln(resultsView, store.GetLabels(query))
fmt.Fprintln(widgets.resultsWidget.(*tview.TextView), labels)
})

// Print all previous results.
for _, result := range store.GetToIndex(query, reader) {
fmt.Fprintln(resultsView, result.Value)
fmt.Fprintln(widgets.resultsWidget.(*tview.TextView), result.Value)
}

// Print results.
Expand All @@ -155,16 +138,18 @@ func StreamDisplay(query string, showHelp, showLogs bool) {
<-pauseDisplayChan
default:
// We can display the next result.
fmt.Fprintln(resultsView, (GetResult(query)).Value)
fmt.Fprintln(widgets.resultsWidget.(*tview.TextView), (GetResult(query)).Value)
}
}
},
)
}

// Creates a table of results for the results pane.
func TableDisplay(query string, filters []string, showHelp, showLogs bool) {
func TableDisplay(query string, filters, labels []string, showHelp, showLogs bool) {
var (
widgets tviewWidgets // Widgets produced by tview.

reader = readerIndexes[query] // Reader index for the query.
tableCellPadding = strings.Repeat(" ", TABLE_PADDING) // Padding to add to table cell content.
valueIndexes = []int{} // Indexes of the result values to add to the table.
Expand All @@ -175,23 +160,16 @@ func TableDisplay(query string, filters []string, showHelp, showLogs bool) {
reader.Dec()

// Initialize the display.
resultsView, helpView, logsView, flexBox := initDisplayTviewTable(helpText())

// Hide displays we don't want to show.
if !showHelp {
flexBox.RemoveItem(helpView)
}
if !showLogs {
flexBox.RemoveItem(logsView)
}
widgets = initDisplayTviewTable(query, filters, labels, showHelp, showLogs)

// Start the display.
display(
DISPLAY_TVIEW,
func() {
var (
nextCellContent string // Next cell to add to the table.
i = 0 // Used to determine the next row index.

i = 0 // Used to determine the next row index.
)

// Determine the value indexes to populate into the graph. If no filter is provided, the index
Expand All @@ -204,9 +182,9 @@ func TableDisplay(query string, filters []string, showHelp, showLogs bool) {

appTview.QueueUpdateDraw(func() {
// Row to contain the labels.
headerRow := resultsView.InsertRow(i)
headerRow := widgets.resultsWidget.(*tview.Table).InsertRow(i)

for j, label := range FilterSlice(store.GetLabels(query), valueIndexes) {
for j, label := range FilterSlice(labels, valueIndexes) {
headerRow.SetCellSimple(i, j, tableCellPadding+label+tableCellPadding)
}
})
Expand All @@ -216,7 +194,7 @@ func TableDisplay(query string, filters []string, showHelp, showLogs bool) {
for _, result := range store.GetToIndex(query, reader) {
appTview.QueueUpdateDraw(func() {
var (
row = resultsView.InsertRow(i) // Row to contain the result.
row = widgets.resultsWidget.(*tview.Table).InsertRow(i) // Row to contain the result.
)

for j, value := range FilterSlice(result.Values, valueIndexes) {
Expand Down Expand Up @@ -249,7 +227,7 @@ func TableDisplay(query string, filters []string, showHelp, showLogs bool) {
// We can display the next result.
appTview.QueueUpdateDraw(func() {
var (
row = resultsView.InsertRow(i) // Row to contain the result.
row = widgets.resultsWidget.(*tview.Table).InsertRow(i) // Row to contain the result.
)

for j, value := range FilterSlice((GetResult(query)).Values, valueIndexes) {
Expand All @@ -273,13 +251,13 @@ func TableDisplay(query string, filters []string, showHelp, showLogs bool) {
}

// Creates a graph of results for the results pane.
func GraphDisplay(query string, filters []string, showHelp, showLogs bool) {
func GraphDisplay(query string, filters, labels []string, showHelp, showLogs bool) {
var (
helpWidget, logsWidget *text.Text // Accessory widgets to display.
resultsWidget *sparkline.SparkLine // Results widget.
err error // General error holder.

reader = readerIndexes[query] // Reader index for the query.
valueIndex = 0 // Index of the result value to graph.
widgets = termdashWidgets{} // Widgets for displaying.
)

// Wait for the first result to appear to synchronize storage.
Expand All @@ -292,25 +270,15 @@ func GraphDisplay(query string, filters []string, showHelp, showLogs bool) {
}

// Initialize the results view.
resultsWidget, err := sparkline.New(
sparkline.Label(store.GetLabels(query)[valueIndex]),
//
// XXX This should probably moved into `display_termdash.go` once termdash is managing more types
// of result displays.
widgets.resultsWidget, err = sparkline.New(
sparkline.Label(labels[valueIndex]),
sparkline.Color(cell.ColorGreen),
)
e(err)

// Initialize the help view.
if showHelp {
helpWidget, err = text.New()
e(err)
helpWidget.Write(helpText())
}

// Initialize the logs view.
if showLogs {
logsWidget, err = text.New()
e(err)
}

// Start the display.
display(
DISPLAY_TERMDASH,
Expand All @@ -322,9 +290,9 @@ func GraphDisplay(query string, filters []string, showHelp, showLogs bool) {

switch value.(type) {
case int64:
resultsWidget.Add([]int{int(value.(int64))})
widgets.resultsWidget.(*sparkline.SparkLine).Add([]int{int(value.(int64))})
case float64:
resultsWidget.Add([]int{int(value.(float64))})
widgets.resultsWidget.(*sparkline.SparkLine).Add([]int{int(value.(float64))})
}
}

Expand All @@ -343,9 +311,9 @@ func GraphDisplay(query string, filters []string, showHelp, showLogs bool) {

switch value.(type) {
case int64:
resultsWidget.Add([]int{int(value.(int64))})
widgets.resultsWidget.(*sparkline.SparkLine).Add([]int{int(value.(int64))})
case float64:
resultsWidget.Add([]int{int(value.(float64))})
widgets.resultsWidget.(*sparkline.SparkLine).Add([]int{int(value.(float64))})
}
}
}
Expand All @@ -354,9 +322,5 @@ func GraphDisplay(query string, filters []string, showHelp, showLogs bool) {

// Initialize the display. This must happen after the display function is invoked, otherwise data
// will never appear.
initDisplayTermdash(termDashWidgets{
resultsWidget: resultsWidget,
helpWidget: helpWidget,
logsWidget: logsWidget,
})
initDisplayTermdash(widgets, query, filters, labels, showHelp, showLogs)
}
Loading

0 comments on commit ae9593a

Please sign in to comment.