Skip to content

Commit

Permalink
Variable clean-up, starting query switching for termdash
Browse files Browse the repository at this point in the history
  • Loading branch information
spacez320 committed Jan 5, 2024
1 parent 366d01f commit 14856b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
6 changes: 3 additions & 3 deletions internal/lib/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func display(driver DisplayDriver, f func()) {
switch driver {
case DISPLAY_TVIEW:
// Start the tview-specific display.
err := app.Run()
err := appTview.Run()
e(err)
case DISPLAY_TERMDASH:
// Start the termdash-specific display.
Expand Down Expand Up @@ -179,7 +179,7 @@ func TableDisplay(query string, filters []string) {
headerRow.SetCellSimple(i, j, tableCellPadding+label+tableCellPadding)
}

app.Draw()
appTview.Draw()
i += 1
}

Expand All @@ -205,7 +205,7 @@ func TableDisplay(query string, filters []string) {
row.SetCellSimple(i, j, tableCellPadding+nextCellContent+tableCellPadding)
}

app.Draw()
appTview.Draw()
i += 1
}
},
Expand Down
47 changes: 23 additions & 24 deletions internal/lib/display_termdash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"github.com/mum4k/termdash/widgets/text"
)

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Types
//
///////////////////////////////////////////////////////////////////////////////////////////////////

// Used to provide an io.Writer implementation of termdash text widgets.
type termdashTextWriter struct {
text text.Text
Expand All @@ -34,25 +28,39 @@ func (t *termdashTextWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Private
//
///////////////////////////////////////////////////////////////////////////////////////////////////
var (
appTermdash *tcell.Terminal // Termdash display.
cancel context.CancelFunc // Cancel function for the termdash display.
)

func keyboardTermdashHandler(key *terminalapi.Keyboard) {
switch key.Key {
case keyboard.KeyEsc:
// When a user presses Esc, close the application.
cancel()
appTermdash.Close()
os.Exit(0)
}
}

// Sets-up the termdash container, which defines the overall layout, and begins
// running the display.
func initDisplayTermdash(resultsWidget, helpWidget, logsWidget widgetapi.Widget) {
var (
ctx context.Context // Termdash specific context.
err error // General error holder.
)

// Set-up the context and enable it to close on key-press.
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel = context.WithCancel(context.Background())

// Set-up the layout.
t, err := tcell.New()
appTermdash, err = tcell.New()
e(err)

// Render the widget.
c, err := container.New(
t,
appTermdash,
container.PaddingBottom(OUTER_PADDING_BOTTOM),
container.PaddingLeft(OUTER_PADDING_LEFT),
container.PaddingTop(OUTER_PADDING_TOP),
Expand Down Expand Up @@ -87,14 +95,5 @@ func initDisplayTermdash(resultsWidget, helpWidget, logsWidget widgetapi.Widget)
e(err)

// Run the display.
termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(
func(k *terminalapi.Keyboard) {
// When a user presses Esc, close the application.
if k.Key == keyboard.KeyEsc {
cancel()
t.Close()
os.Exit(0)
}
},
))
termdash.Run(ctx, appTermdash, c, termdash.KeyboardSubscriber(keyboardTermdashHandler))
}
16 changes: 8 additions & 8 deletions internal/lib/display_tview.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
)

var (
app = tview.NewApplication() // Tview application.
appTview = tview.NewApplication() // Tview application.
)

// Function to call on 'done' events.
func doneTview(key tcell.Key) {
func keyboardTviewHandler(key tcell.Key) {
switch key {
case tcell.KeyEscape:
// When a user presses Esc, close the application.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "quit", true)
app.Stop()
appTview.Stop()
case tcell.KeyTab:
// When a user presses Tab, stop the display but continue running.
interruptChan <- true
currentCtx = context.WithValue(currentCtx, "advanceQuery", true)
app.Stop()
appTview.Stop()
}
}

Expand All @@ -42,7 +42,7 @@ func initDisplayTviewTable(helpText string) (
logsView = tview.NewTextView()

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

initDisplayTview(resultsView, helpView, logsView, helpText)
Expand All @@ -59,8 +59,8 @@ func initDisplayTviewText(helpText string) (resultsView, helpView, logsView *tvi
// Initialize the results view.
resultsView.SetChangedFunc(
func() {
app.Draw()
}).SetDoneFunc(doneTview)
appTview.Draw()
}).SetDoneFunc(keyboardTviewHandler)
resultsView.SetBorder(true).SetTitle("Results")

initDisplayTview(resultsView, helpView, logsView, helpText)
Expand Down Expand Up @@ -96,7 +96,7 @@ func initDisplayTview(
OUTER_PADDING_LEFT,
OUTER_PADDING_RIGHT,
)
app.SetRoot(flexBox, true).SetFocus(resultsView)
appTview.SetRoot(flexBox, true).SetFocus(resultsView)

// Initialize the help view.
helpView.SetBorder(true).SetTitle("Help")
Expand Down

0 comments on commit 14856b5

Please sign in to comment.