Skip to content

Removed need for ctrl key for navigation #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ in an easy to review format.
* `up arrow`, `j`: move line selection down
* `down arrow`, `k`: move line selection up
* `enter`: view detail of selected line
* `ctrl+s`: open search box
* `ctrl+g`: open go to line box
* `ctrl+q`, `ctrl+s`: quit the application
* `s`: open search box
* `g`: open go to line box
* `q`, `ctrl+c`: quit the application
+ Line detail view:
* up/down navigation is same as lines view
* `esc`: return to lines view
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/goto_line_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func (t *TUI) initGotoLineModal() {
0,
)
t.linesTable.Select(int(lineNum)-1, 0)
t.pages.HidePage(PAGE_GOTO_LINE)
t.hideModal(PAGE_GOTO_LINE)
})

form.AddButton("Cancel", func() {
t.pages.HidePage(PAGE_GOTO_LINE)
t.hideModal(PAGE_GOTO_LINE)
})

t.pages.AddPage(PAGE_GOTO_LINE, modal(form, 30, 7), true, false)
Expand Down
10 changes: 5 additions & 5 deletions internal/tui/lines_table_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func (t *TUI) linesTableInputHandler(event *tcell.EventKey) *tcell.EventKey {
t.logger.Trace("received key event in lines table view", "key", event.Name(), "rune", event.Rune())

// TODO: modals are retaining state between invocations, they shouldn't
switch event.Key() {
case tcell.KeyCtrlG:
switch event.Rune() {
case 'g':
t.logger.Trace("showing go to line modal")
t.pages.ShowPage(PAGE_GOTO_LINE).SendToFront(PAGE_GOTO_LINE)
t.showModal(PAGE_GOTO_LINE)
return nil

case tcell.KeyCtrlS:
case 's':
t.logger.Trace("showing search modal")
t.pages.ShowPage(PAGE_SEARCH_FORM).SendToFront(PAGE_SEARCH_FORM)
t.showModal(PAGE_SEARCH_FORM)
return nil
}
return event
Expand Down
34 changes: 29 additions & 5 deletions internal/tui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type TUI struct {
db *database.LogsDatabase
logger *log.Logger

// captureGlobalInput will be true when we are on a "main" view, e.g. the
// "lines table" view. It will be false when there is some view showing that
// needs to capture all key presses, e.g. a search modal. The idea being, if
// this is false, then we can ignore any global keys, e.g. the quit key, until
// that view is closed.
captureGlobalInput bool

// root is the overall application frame.
root *tview.Grid
// pages is the primary top view of the application, i.e. everything
Expand Down Expand Up @@ -43,11 +50,12 @@ type TUI struct {

func NewTUI(logLines []common.Envelope, db *database.LogsDatabase, logger *log.Logger) TUI {
tui := TUI{
App: tview.NewApplication(),
db: db,
logger: logger,
lines: logLines,
pages: tview.NewPages(),
App: tview.NewApplication(),
db: db,
logger: logger,
lines: logLines,
pages: tview.NewPages(),
captureGlobalInput: true,
}

tui.initLineDetailView()
Expand All @@ -64,6 +72,11 @@ func NewTUI(logLines []common.Envelope, db *database.LogsDatabase, logger *log.L
return tui
}

func (t *TUI) hidePage(name string) {
t.pages.HidePage(name)
t.captureGlobalInput = !t.captureGlobalInput
}

// showPage hides the current page, caches the text of the left status
// indicator, updates the left status, and shows the new page.
func (t *TUI) showPage(name string, status string) {
Expand All @@ -74,7 +87,18 @@ func (t *TUI) showPage(name string, status string) {
t.prevPageStatus = ""
}

t.captureGlobalInput = t.pageShouldCaptureGlobalInput(name)
t.pages.HidePage(currentPageName)
t.pages.ShowPage(name)
t.leftStatus.SetText(status)
}

func (t *TUI) hideModal(name string) {
t.pages.HidePage(name)
t.captureGlobalInput = !t.captureGlobalInput
}

func (t *TUI) showModal(name string) {
t.captureGlobalInput = t.pageShouldCaptureGlobalInput(name)
t.pages.ShowPage(name).SendToFront(name)
}
14 changes: 14 additions & 0 deletions internal/tui/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ const (
PAGE_LINE_DETAIL = "line_detail"
PAGE_SEARCH_FORM = "search_form"
)

func (t *TUI) pageShouldCaptureGlobalInput(pageName string) bool {
switch pageName {
case PAGE_GOTO_LINE:
return false
case PAGE_LINE_DETAIL:
return false
case PAGE_LINES_TABLE:
return true
case PAGE_SEARCH_FORM:
return false
}
return false
}
13 changes: 10 additions & 3 deletions internal/tui/root_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ func (t *TUI) initRootView() {
//
// The majority of event handlers should be located on primitives.
func (t *TUI) rootInputHandler(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlH:
if t.captureGlobalInput == false {
// When captureGlobalInput is false, the app should be showing a view that
// requires full input control, i.e. one that should not recognize global
// keyboard shortcuts.
return event
}

switch event.Rune() {
case 'h':
t.leftStatus.SetText("help invoked")
return nil

case tcell.KeyCtrlQ:
case 'q':
t.App.Stop()
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/tui/search_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (t *TUI) initSearchModal() {
)

form.AddButton("Search", func() { t.handleSearch(form) })
form.AddButton("Cancel", func() { t.pages.HidePage(PAGE_SEARCH_FORM) })
form.AddButton("Cancel", func() { t.hideModal(PAGE_SEARCH_FORM) })

t.pages.AddPage(PAGE_SEARCH_FORM, modal(form, 50, 8), true, false)
}
Expand All @@ -28,7 +28,7 @@ func (t *TUI) handleSearch(form *tview.Form) {
searchResult, err := t.db.Search(searchTerm)
if err != nil {
t.logger.Error("search failed", "error", err)
t.pages.HidePage(PAGE_SEARCH_FORM)
t.hideModal(PAGE_SEARCH_FORM)
// TODO: show error modal
return
}
Expand All @@ -37,7 +37,7 @@ func (t *TUI) handleSearch(form *tview.Form) {
content := NewLinesTableContent(lines)
t.lines = lines
t.linesTable.SetContent(content)
t.pages.HidePage(PAGE_SEARCH_FORM)
t.hideModal(PAGE_SEARCH_FORM)
t.linesScrollStatus(0, 0)
t.linesTable.Select(0, 0)
}
Loading