Skip to content

Commit

Permalink
Proper keystroke handling
Browse files Browse the repository at this point in the history
  • Loading branch information
spacez320 committed Jan 14, 2024
1 parent c3c14cf commit ca41253
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
25 changes: 12 additions & 13 deletions internal/lib/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
)

const (
HELP_TEXT = "(ESC) Quit | (ENTER) Next Display | (TAB) Next Query"
HELP_TEXT = "(ESC) Quit | (Tab) Next Display | (n) Next Query"
HELP_SIZE = 10 // Proportional size of the logs widget.
LOGS_SIZE = 15 // Proportional size of the logs widget.
OUTER_PADDING_LEFT = 10 // Left padding for the full display.
Expand Down Expand Up @@ -75,6 +75,11 @@ func display(driver DisplayDriver, f func()) {
}
}

// Creates help text for any display.
func helpText(query string) string {
return HELP_TEXT + fmt.Sprintf("\nQuery: %v", query) // Text to display in the help pane.
}

// Presents raw output.
func RawDisplay(query string) {
go func() {
Expand All @@ -86,12 +91,8 @@ func RawDisplay(query string) {

// Update the results pane with new results as they are generated.
func StreamDisplay(query string) {
var (
helpText = HELP_TEXT + fmt.Sprintf("\nQuery: %v", query) // Text to display in the help pane.
)

// Initialize the display.
resultsView, _, _ := initDisplayTviewText(helpText)
resultsView, _, _ := initDisplayTviewText(helpText(query))

// Start the display.
display(
Expand Down Expand Up @@ -129,13 +130,12 @@ func StreamDisplay(query string) {
// Creates a table of results for the results pane.
func TableDisplay(query string, filters []string) {
var (
helpText = HELP_TEXT + fmt.Sprintf("\nQuery: %v", query) // Text to display in the help pane.
tableCellPadding = strings.Repeat(" ", TABLE_PADDING) // Padding to add to table cell content.
valueIndexes = []int{} // Indexes of the result values to add to the table.
tableCellPadding = strings.Repeat(" ", TABLE_PADDING) // Padding to add to table cell content.
valueIndexes = []int{} // Indexes of the result values to add to the table.
)

// Initialize the display.
resultsView, _, _ := initDisplayTviewTable(helpText)
resultsView, _, _ := initDisplayTviewTable(helpText(query))

// Start the display.
display(
Expand Down Expand Up @@ -228,8 +228,7 @@ func TableDisplay(query string, filters []string) {
// Creates a graph of results for the results pane.
func GraphDisplay(query string, filters []string) {
var (
helpText = HELP_TEXT + fmt.Sprintf("\nQuery: %v", query) // Text to display in the help pane.
valueIndex = 0 // Index of the result value to graph.
valueIndex = 0 // Index of the result value to graph.
)

// Determine the values to populate into the graph. If no filter is provided,
Expand All @@ -248,7 +247,7 @@ func GraphDisplay(query string, filters []string) {
// Initialize the help view.
helpWidget, err := text.New()
e(err)
helpWidget.Write(helpText)
helpWidget.Write(helpText(query))

// Initialize the logs view.
logsWidget, err := text.New()
Expand Down
13 changes: 8 additions & 5 deletions internal/lib/display_termdash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lib

import (
"context"
"fmt"

"github.com/mum4k/termdash"
"github.com/mum4k/termdash/container"
Expand Down Expand Up @@ -42,17 +43,19 @@ func keyboardTermdashHandler(key *terminalapi.Keyboard) {
cancel()
appTermdash.Close()
case keyboard.KeyTab:
// When a user presses Tab, stop the display but continue running.
// When a user presses Enter, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
currentCtx = context.WithValue(currentCtx, "advanceDisplayMode", true)
cancel()
appTermdash.Close()
case keyboard.KeyEnter:
// When a user presses Enter, stop the display but continue running.
case 'n':
// When a user presses Tab, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceDisplayMode", true)
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
cancel()
appTermdash.Close()
default:
slog.Debug(fmt.Sprintf("Pressed key %v", key.Key))
}
}

Expand Down
35 changes: 21 additions & 14 deletions internal/lib/display_tview.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ var (
)

// Function to call on keyboard events.
func keyboardTviewHandler(key tcell.Key) {
switch key {
func keyboardTviewHandler(event *tcell.EventKey) *tcell.EventKey {
// slog.Debug(fmt.Sprintf("Event is: %v", event))
switch key := event.Key(); key {
case tcell.KeyEscape:
// When a user presses Esc, close the application.
currentCtx = context.WithValue(currentCtx, "quit", true)
appTview.Stop()
case tcell.KeyRune:
switch event.Rune() {
case 'n':
// This is wrapped in a goroutine to avoid deadlocks with tview.
//
// See: https://github.com/rivo/tview/issues/784
go func() {
// When a user presses Tab, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
appTview.Stop()
}()
}
case tcell.KeyTab:
// This is wrapped in a goroutine to avoid deadlocks with tview.
//
// See: https://github.com/rivo/tview/issues/784
go func() {
// When a user presses Tab, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
appTview.Stop()
}()
case tcell.KeyEnter:
// This is wrapped in a goroutine to avoid deadlocks with tview.
//
// See: https://github.com/rivo/tview/issues/784
Expand All @@ -44,6 +48,8 @@ func keyboardTviewHandler(key tcell.Key) {
appTview.Stop()
}()
}

return event
}

// Display init function specific to table results.
Expand All @@ -56,7 +62,7 @@ func initDisplayTviewTable(helpText string) (
logsView = tview.NewTextView()

// Initialize the results view.
resultsView.SetBorders(true).SetDoneFunc(keyboardTviewHandler)
resultsView.SetBorders(true)
resultsView.SetBorder(true).SetTitle("Results")

initDisplayTview(resultsView, helpView, logsView, helpText)
Expand All @@ -74,7 +80,7 @@ func initDisplayTviewText(helpText string) (resultsView, helpView, logsView *tvi
resultsView.SetChangedFunc(
func() {
appTview.Draw()
}).SetDoneFunc(keyboardTviewHandler)
})
resultsView.SetBorder(true).SetTitle("Results")

initDisplayTview(resultsView, helpView, logsView, helpText)
Expand Down Expand Up @@ -110,6 +116,7 @@ func initDisplayTview(
OUTER_PADDING_LEFT,
OUTER_PADDING_RIGHT,
)
flexBox.SetInputCapture(keyboardTviewHandler)
appTview.SetRoot(flexBox, true).SetFocus(resultsView)

// Initialize the help view.
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ type Storage map[string]*Results
///////////////////////////////////////////////////////////////////////////////////////////////////

const (
PUT_EVENT_CHANNEL_SIZE = 128 // Size of Put channels.
// Size of Put channels. This is the amount of results that may accumulate if
// not being actively consumed.
PUT_EVENT_CHANNEL_SIZE = 128
)

// Channels for broadcasting Put calls.
Expand Down

0 comments on commit ca41253

Please sign in to comment.