Skip to content

Commit

Permalink
Extended keyboard navigation to dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
aurc committed Aug 16, 2022
1 parent d71826a commit 31b46cf
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 50 deletions.
6 changes: 3 additions & 3 deletions internal/loggo/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ type Loggo interface {
Stop()
SetFocus(primitive tview.Primitive)
ShowPopMessage(text string, waitSecs int64, resetFocusTo tview.Primitive)
ShowPrefabModal(text string, width, height int, buttons ...*tview.Button)
ShowModal(p tview.Primitive, width, height int, bgColor tcell.Color)
DismissModal()
ShowPrefabModal(text string, width, height int, capture inputCapture, buttons ...*tview.Button)
ShowModal(p tview.Primitive, width, height int, bgColor tcell.Color, capture inputCapture)
DismissModal(resetFocusTo tview.Primitive)
Config() *config.Config
StackView(p tview.Primitive)
PopView()
Expand Down
33 changes: 19 additions & 14 deletions internal/loggo/app_scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ import (
)

type appScaffold struct {
app *tview.Application
config *config.Config
pages *tview.Pages
modal *tview.Flex
stackPages []tview.Primitive
app *tview.Application
config *config.Config
pages *tview.Pages
modal *tview.Flex
stackPages []tview.Primitive
inputCapture inputCapture
}

type inputCapture func(event *tcell.EventKey) *tcell.EventKey

type App interface {
Stop()
Run(p tview.Primitive)
Expand Down Expand Up @@ -106,23 +109,20 @@ func (a *appScaffold) ShowPopMessage(text string, waitSecs int64, resetFocusTo t
mainContent.SetBackgroundColor(tcell.ColorDarkBlue).SetBorderPadding(0, 0, 2, 2)
modal.AddItem(mainContent, 0, 1, false)
modal.AddItem(countdownText, 1, 1, false)
a.ShowModal(modal, len(text)/2, 5, tcell.ColorDarkBlue)
a.ShowModal(modal, len(text)/2, 5, tcell.ColorDarkBlue, nil)
countdownText.SetTextColor(tcell.ColorLightGrey).SetBackgroundColor(tcell.ColorDarkBlue)
go func() {
for i := waitSecs; i >= 0; i-- {
countdownText.SetText(fmt.Sprintf(`(%ds)`, i))
a.Draw()
time.Sleep(time.Second)
}
a.DismissModal()
a.DismissModal(resetFocusTo)
a.Draw()
if resetFocusTo != nil {
go a.SetFocus(resetFocusTo)
}
}()
}

func (a *appScaffold) ShowPrefabModal(text string, width, height int, buttons ...*tview.Button) {
func (a *appScaffold) ShowPrefabModal(text string, width, height int, cap inputCapture, buttons ...*tview.Button) {
modal := tview.NewFlex().SetDirection(tview.FlexRow)
modal.SetBackgroundColor(tcell.ColorDarkBlue)
mainContent := tview.NewTextView().
Expand All @@ -143,10 +143,11 @@ func (a *appScaffold) ShowPrefabModal(text string, width, height int, buttons ..

modal.AddItem(mainContent, 0, 1, false)
modal.AddItem(buts, 1, 1, false)
a.ShowModal(modal, width, height, tcell.ColorDarkBlue)
a.ShowModal(modal, width, height, tcell.ColorDarkBlue, cap)
}

func (a *appScaffold) ShowModal(p tview.Primitive, width, height int, bgColor tcell.Color) {
func (a *appScaffold) ShowModal(p tview.Primitive, width, height int, bgColor tcell.Color, cap inputCapture) {
a.inputCapture = cap
modContainer := tview.NewFlex().AddItem(p, 0, 1, false)
modContainer.SetBorder(true).SetBackgroundColor(bgColor)
a.modal = tview.NewFlex().
Expand All @@ -159,8 +160,12 @@ func (a *appScaffold) ShowModal(p tview.Primitive, width, height int, bgColor tc
a.pages.AddPage("modal", a.modal, true, true)
}

func (a *appScaffold) DismissModal() {
func (a *appScaffold) DismissModal(resetFocusTo tview.Primitive) {
a.inputCapture = nil
a.pages.RemovePage("modal")
if resetFocusTo != nil {
go a.SetFocus(resetFocusTo)
}
}

func (a *appScaffold) Run(p tview.Primitive) {
Expand Down
20 changes: 17 additions & 3 deletions internal/loggo/filter_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ package loggo

import (
"fmt"
"github.com/aurc/loggo/internal/char"
"strings"

"github.com/aurc/loggo/internal/char"

"github.com/aurc/loggo/internal/filter"

"github.com/aurc/loggo/internal/color"
Expand Down Expand Up @@ -124,8 +125,21 @@ func (t *FilterView) search() {
exp, err := filter.ParseFilterExpression(t.expressionField.GetText())
if err != nil {
t.app.ShowPrefabModal(fmt.Sprintf("[yellow::b]Invalid filter expression:[-::-]\n[::i]%v", err), 50, 10,
tview.NewButton("Ok").SetSelectedFunc(func() {
t.app.DismissModal()
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter, tcell.KeyEsc:
t.app.DismissModal(t.expressionField)
return nil
}
switch event.Rune() {
case 'C', 'c':
t.app.DismissModal(t.expressionField)
return nil
}
return event
},
tview.NewButton("[darkred::bu]C[-::-]ancel").SetSelectedFunc(func() {
t.app.DismissModal(t.expressionField)
}))
return
}
Expand Down
34 changes: 24 additions & 10 deletions internal/loggo/log_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewLogReader(app *LoggoApp, reader reader.Reader) *LogView {
chanReader: reader,
filterChannel: make(chan *filter.Expression, 1),
filterLock: sync.RWMutex{},
hideFilter: true,
isFollowing: true,
}
lv.makeUIComponents()
Expand All @@ -85,27 +86,40 @@ func NewLogReader(app *LoggoApp, reader reader.Reader) *LogView {
}()
lv.app.ShowPrefabModal(fmt.Sprintf("An error occurred with the input stream: %v "+
"\nYou can continue browsing the buffered logs or close the app.", err), 50, 20,
tview.NewButton("Quit").SetSelectedFunc(func() {
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case 'q', 'Q':
lv.app.Stop()
return nil
case 'c', 'C':
lv.app.DismissModal(lv.table)
return nil
}
return event
},
tview.NewButton("[darkred::bu]Q[-::-]uit").SetSelectedFunc(func() {
lv.app.Stop()
}),
tview.NewButton("Continue").SetSelectedFunc(func() {
lv.app.DismissModal()
tview.NewButton("[darkred::bu]C[-::-]ancel").SetSelectedFunc(func() {
lv.app.DismissModal(lv.table)
}))
})

go func() {
lv.app.ShowModal(NewSplashScreen(lv.app), 71, 16, tcell.ColorBlack)
lv.app.Draw()
time.Sleep(2 * time.Second)
lv.app.DismissModal()
lv.app.Draw()

}()

lv.read()
lv.filter()
lv.filterChannel <- nil

go func() {
lv.app.ShowModal(NewSplashScreen(lv.app), 71, 16, tcell.ColorBlack, nil)
lv.app.Draw()
time.Sleep(2 * time.Second)
lv.app.DismissModal(lv.table)
lv.app.Draw()

time.Sleep(10 * time.Millisecond)
lv.isFollowing = true
lv.app.SetFocus(lv.table)
Expand Down Expand Up @@ -238,10 +252,10 @@ func (l *LogView) makeLayouts() {
}

func (l *LogView) showAbout() {
l.app.ShowModal(NewSplashScreen(l.app), 71, 16, tcell.ColorBlack)
l.app.ShowModal(NewSplashScreen(l.app), 71, 16, tcell.ColorBlack, nil)
l.app.Draw()
time.Sleep(4 * time.Second)
l.app.DismissModal()
l.app.DismissModal(l.table)
l.app.Draw()
}

Expand Down
3 changes: 3 additions & 0 deletions internal/loggo/log_view_key_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (

func (l *LogView) keyEvents() {
l.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if l.app.inputCapture != nil {
return l.app.inputCapture(event)
}
switch event.Key() {
case tcell.KeyCtrlA:
go func() {
Expand Down
34 changes: 31 additions & 3 deletions internal/loggo/log_view_readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"fmt"
"time"

"github.com/gdamore/tcell/v2"

"github.com/aurc/loggo/internal/filter"

"github.com/aurc/loggo/internal/config"
Expand All @@ -37,7 +39,20 @@ func (l *LogView) read() {
go func() {
if err := l.chanReader.StreamInto(); err != nil {
l.app.ShowPrefabModal(fmt.Sprintf("Unable to start stream: %v", err), 40, 10,
tview.NewButton("Quit").SetSelectedFunc(func() {
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter, tcell.KeyEsc:
l.app.Stop()
return nil
}
switch event.Rune() {
case 'Q', 'q':
l.app.Stop()
return nil
}
return event
},
tview.NewButton("[darkred::bu]Q[-::-]uit").SetSelectedFunc(func() {
l.app.Stop()
}))
} else {
Expand Down Expand Up @@ -137,8 +152,21 @@ func (l *LogView) filterLine(e *filter.Expression, index int) error {
l.app.ShowPrefabModal(fmt.Sprintf("[yellow::b]Error interpreting filter expression:[-::-]\n"+
"Filter stream has reset. Please adjust the filter expression"+
"\n[::i]%v", err), 50, 12,
tview.NewButton("Ok").SetSelectedFunc(func() {
l.app.DismissModal()
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter, tcell.KeyEsc:
l.app.DismissModal(l.table)
return nil
}
switch event.Rune() {
case 'C', 'c':
l.app.DismissModal(l.table)
return nil
}
return event
},
tview.NewButton("[darkred::bu]C[-::-]ancel").SetSelectedFunc(func() {
l.app.DismissModal(l.table)
}))
l.filterChannel <- nil
return err
Expand Down
80 changes: 63 additions & 17 deletions internal/loggo/template_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,40 @@ func (t *TemplateView) save(fileName string) {
if err := t.config.Save(fileName); err != nil {
t.app.ShowPrefabModal(
fmt.Sprintf(`Failed to save! Error: %v`, err), 40, 10,
tview.NewButton("Ok").SetSelectedFunc(func() {
t.app.DismissModal()
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter, tcell.KeyEsc:
t.app.DismissModal(t.table)
return nil
}
switch event.Rune() {
case 'C', 'c':
t.app.DismissModal(t.table)
return nil
}
return event
},
tview.NewButton("[darkred::bu]C[-::-]ancel").SetSelectedFunc(func() {
t.app.DismissModal(t.table)
}))
} else {
t.app.ShowPrefabModal(
fmt.Sprintf(`File %v saved successfully!`, fileName), 40, 10,
tview.NewButton("Ok").SetSelectedFunc(func() {
t.app.DismissModal()
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter, tcell.KeyEsc:
t.app.DismissModal(t.table)
return nil
}
switch event.Rune() {
case 'O', 'o':
t.app.DismissModal(t.table)
return nil
}
return event
},
tview.NewButton("[darkred::bu]O[-::-]k").SetSelectedFunc(func() {
t.app.DismissModal(t.table)
}))
}
}
Expand Down Expand Up @@ -325,26 +351,46 @@ func (t *TemplateView) moveDown() {

func (t *TemplateView) confirmDelete() {
t.app.ShowPrefabModal("Are you sure you want to remove this entry", 40, 10,
tview.NewButton("Delete").
func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter, tcell.KeyEsc:
t.app.DismissModal(t.contextMenu)
return nil
}
switch event.Rune() {
case 'D', 'd':
t.delete()
return nil
case 'C', 'c':
t.app.DismissModal(t.contextMenu)
return nil
}
return event
},
tview.NewButton("[darkred::bu]D[-::-]elete").
SetSelectedFunc(func() {
var newKeys []config.Key
r, _ := t.table.GetSelection()
for i, k := range t.config.Keys {
if i != r-1 {
newKeys = append(newKeys, k)
}
}
t.config.Keys = newKeys
t.makeLayouts()
t.app.DismissModal()
t.delete()
}),
tview.NewButton("Cancel").
tview.NewButton("[darkred::bu]C[-::-]ancel").
SetSelectedFunc(func() {
t.app.DismissModal()
t.app.DismissModal(t.contextMenu)
}),
)
}

func (t *TemplateView) delete() {
var newKeys []config.Key
r, _ := t.table.GetSelection()
for i, k := range t.config.Keys {
if i != r-1 {
newKeys = append(newKeys, k)
}
}
t.config.Keys = newKeys
t.makeLayouts()
t.app.DismissModal(t.contextMenu)
}

type TemplateData struct {
tview.TableContentReadOnly
templateView *TemplateView
Expand Down

0 comments on commit 31b46cf

Please sign in to comment.