Skip to content

Commit

Permalink
Improved hot-keys navigation. Fixed focus issues after clipboard copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
aurc committed Aug 12, 2022
1 parent 994e9dd commit 3160ec9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/loggo/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Loggo interface {
SetInputCapture(cap func(event *tcell.EventKey) *tcell.EventKey)
Stop()
SetFocus(primitive tview.Primitive)
ShowPopMessage(text string, waitSecs int64)
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()
Expand Down
5 changes: 4 additions & 1 deletion internal/loggo/app_scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (a *appScaffold) PopView() {
a.stackPages = a.stackPages[:len(a.stackPages)-1]
}

func (a *appScaffold) ShowPopMessage(text string, waitSecs int64) {
func (a *appScaffold) ShowPopMessage(text string, waitSecs int64, resetFocusTo tview.Primitive) {
modal := tview.NewFlex().SetDirection(tview.FlexRow)
modal.SetBackgroundColor(tcell.ColorDarkBlue)
countdownText := tview.NewTextView().SetTextAlign(tview.AlignRight)
Expand All @@ -116,6 +116,9 @@ func (a *appScaffold) ShowPopMessage(text string, waitSecs int64) {
}
a.DismissModal()
a.Draw()
if resetFocusTo != nil {
go a.SetFocus(resetFocusTo)
}
}()
}

Expand Down
16 changes: 14 additions & 2 deletions internal/loggo/json_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewJsonView(app Loggo, showQuit bool,
app: app,
indent: " ",
isCopyMode: true,
wordWrap: true,
showQuit: showQuit,
toggleFullScreenCallback: toggleFullScreenCallback,
closeCallback: closeCallback,
Expand Down Expand Up @@ -278,8 +279,19 @@ func (j *JsonView) copyToClipboard() {
} else {
size = fmt.Sprintf(`[yellow::bu]%.0f bytes[-::-]`, l)
}
j.app.ShowPopMessage(fmt.Sprintf(`Copied %s to clipboard`, size), 3)
clipboard.WriteAll(string(j.jText))
j.app.ShowPopMessage(fmt.Sprintf(`Copied %s to clipboard`, size), 2, j.textView)
// Attempt formatting
b := j.jText
m := make(map[string]any)
err := json.Unmarshal(b, &m)
if err == nil {
b2, err := json.MarshalIndent(m, "", " ")
if err == nil {
b = b2
}
}
// Paste to clipboard
_ = clipboard.WriteAll(string(b))
}

func (j *JsonView) prepareCaseInsensitiveSearch() {
Expand Down
24 changes: 19 additions & 5 deletions internal/loggo/log_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (l *LogView) makeUIComponents() {
}
l.jsonView.SetJson(b)
l.makeLayoutsWithJsonView()
l.updateBottomBarMenu()
} else {
l.makeLayouts()
}
Expand Down Expand Up @@ -158,10 +159,12 @@ func (l *LogView) makeUIComponents() {
r, c := l.table.GetOffset()
l.updateLineView()
l.table.SetOffset(r, c)
//go func() {
// selection(row, column)
// l.app.Draw()
//}()
if l.isJsonViewShown() {
go func() {
selection(row, column)
l.app.Draw()
}()
}
}
})

Expand Down Expand Up @@ -252,7 +255,18 @@ func (l *LogView) makeLayoutsWithJsonView() {
AddItem(l.jsonView, 0, 2, false).
AddItem(l.mainMenu, 1, 1, false)

//l.app.SetFocus(l.jsonView.textView)
l.jsonView.textView.SetFocusFunc(func() {
go func() {
time.Sleep(10 * time.Millisecond)
l.updateBottomBarMenu()
}()
})
l.jsonView.textView.SetBlurFunc(func() {
go func() {
time.Sleep(10 * time.Millisecond)
l.updateBottomBarMenu()
}()
})
l.app.SetFocus(l.table)
}

Expand Down
20 changes: 20 additions & 0 deletions internal/loggo/log_view_key_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ THE SOFTWARE.
package loggo

import (
"time"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand All @@ -41,6 +43,24 @@ func (l *LogView) keyEvents() {
case tcell.KeyCtrlSpace:
l.toggledFollowing()
return nil
case tcell.KeyTAB:
if l.isJsonViewShown() {
if l.jsonView.textView.HasFocus() {
l.app.SetFocus(l.table)
go func() {
time.Sleep(time.Millisecond)
l.updateBottomBarMenu()
}()
} else {
l.app.SetFocus(l.jsonView.textView)
go func() {
time.Sleep(time.Millisecond)
l.updateBottomBarMenu()
}()
}
return nil
}
return event
}
prim := l.app.app.GetFocus()
if _, ok := prim.(*tview.InputField); ok {
Expand Down
29 changes: 25 additions & 4 deletions internal/loggo/log_view_nav_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ func (l *LogView) populateMenu() {
AddItem(l.linesView, 1, 1, false)

l.mainMenu = tview.NewFlex().SetDirection(tview.FlexColumn)
l.mainMenu.
l.updateBottomBarMenu()
}

func (l *LogView) updateBottomBarMenu() {
l.mainMenu.Clear().
SetBackgroundColor(color.ColorBackgroundField).SetTitleAlign(tview.AlignCenter)
l.mainMenu.
AddItem(l.textViewMenuControl(tview.NewTextView().
Expand All @@ -135,14 +139,31 @@ func (l *LogView) populateMenu() {
// TODO: Find a reliable way to respond to external closure
} else {
l.makeLayoutsWithTemplateView()
l.updateBottomBarMenu()
}
}), 0, 2, false).
AddItem(l.followingView, 0, 5, false).
}), 0, 3, false).
AddItem(l.followingView, 0, 5, false)
if l.isJsonViewShown() && !l.jsonView.HasFocus() {
l.mainMenu.
AddItem(l.textViewMenuControl(tview.NewTextView().SetRegions(true).
SetDynamicColors(true).
SetText(`[yellow::b](TAB) [-::u]["1"]Focus Log Entry[""]`), func() {
go l.app.SetFocus(l.jsonView.textView)
}), 0, 3, false)
} else if l.isJsonViewShown() && l.jsonView.HasFocus() {
l.mainMenu.
AddItem(l.textViewMenuControl(tview.NewTextView().SetRegions(true).
SetDynamicColors(true).
SetText(`[yellow::b](TAB) [-::u]["1"]Focus Stream Table[""]`), func() {
go l.app.SetFocus(l.table)
}), 0, 3, false)
}
l.mainMenu.
AddItem(l.textViewMenuControl(tview.NewTextView().SetRegions(true).
SetDynamicColors(true).
SetText(`[yellow::b](^C) [-::u]["1"]Quit[""]`), func() {
l.app.Stop()
}), 0, 1, false).
}), 0, 2, false).
AddItem(l.linesView, 0, 3, false)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/uitest/helper/json_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ func JsonGenerator(writer io.Writer) {
jm["timestamp"] = time.Now().Format("2006-01-02T15:04:05-0700")
b, _ = json.Marshal(jm)
_, _ = fmt.Fprintln(writer, string(b))
time.Sleep(time.Millisecond * time.Duration(rand.Intn(25)))
time.Sleep(time.Millisecond * time.Duration(rand.Intn(800)))
}
}

0 comments on commit 3160ec9

Please sign in to comment.