1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-427 Use new keyboard implementation for widgets. Closes #427

This commit is contained in:
Chris Cummer
2019-05-06 08:56:01 -07:00
parent a9c5dc3be8
commit 2d0706c40b
45 changed files with 589 additions and 745 deletions

View File

@@ -0,0 +1,21 @@
package textfile
import (
"github.com/gdamore/tcell"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) initializeKeyboardControls() {
widget.SetKeyboardChar("/", widget.ShowHelp)
widget.SetKeyboardChar("h", widget.Prev)
widget.SetKeyboardChar("l", widget.Next)
widget.SetKeyboardChar("o", widget.openFile)
widget.SetKeyboardKey(tcell.KeyLeft, widget.Prev)
widget.SetKeyboardKey(tcell.KeyRight, widget.Next)
}
func (widget *Widget) openFile() {
src := widget.CurrentSource()
wtf.OpenFile(src)
}

View File

@@ -12,7 +12,6 @@ import (
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/gdamore/tcell"
"github.com/radovskyb/watcher"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
@@ -32,6 +31,7 @@ const HelpText = `
type Widget struct {
wtf.HelpfulWidget
wtf.KeyboardWidget
wtf.MultiSourceWidget
wtf.TextWidget
@@ -39,9 +39,11 @@ type Widget struct {
settings *Settings
}
// NewWidget creates a new instance of a widget
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
KeyboardWidget: wtf.NewKeyboardWidget(),
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "filePath", "filePaths"),
TextWidget: wtf.NewTextWidget(app, settings.common, true),
@@ -52,11 +54,14 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
// Don't use a timer for this widget, watch for filesystem changes instead
widget.settings.common.RefreshInterval = 0
widget.HelpfulWidget.SetView(widget.View)
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.SetDisplayFunction(widget.display)
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
widget.View.SetInputCapture(widget.keyboardIntercept)
widget.View.SetWrap(true)
widget.HelpfulWidget.SetView(widget.View)
go widget.watchForFileChanges()
@@ -139,36 +144,6 @@ func (widget *Widget) plainText() string {
return string(text)
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case "/":
widget.ShowHelp()
return nil
case "h":
widget.Prev()
return nil
case "l":
widget.Next()
return nil
case "o":
wtf.OpenFile(widget.CurrentSource())
return nil
}
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
}
return event
}
func (widget *Widget) watchForFileChanges() {
watch := watcher.New()
watch.FilterOps(watcher.Write)