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

Textfile module now supports multiple text files

This commit is contained in:
Chris Cummer
2018-08-30 07:52:59 -07:00
parent ed75f9648a
commit 0e797fea3d
12 changed files with 168 additions and 48 deletions

View File

@@ -18,14 +18,20 @@ const HelpText = `
Keyboard commands for Textfile:
/: Show/hide this help window
h: Previous text file
l: Next text file
o: Open the text file in the operating system
arrow left: Previous text file
arrow right: Next text file
`
type Widget struct {
wtf.HelpfulWidget
wtf.TextWidget
filePath string
filePaths []string
idx int
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
@@ -33,9 +39,11 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
filePath: wtf.Config.UString("wtf.mods.textfile.filePath"),
idx: 0,
}
widget.loadFilePaths()
widget.HelpfulWidget.SetView(widget.View)
widget.View.SetWrap(true)
@@ -47,28 +55,56 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Next() {
widget.idx = widget.idx + 1
if widget.idx == len(widget.filePaths) {
widget.idx = 0
}
widget.display()
}
func (widget *Widget) Prev() {
widget.idx = widget.idx - 1
if widget.idx < 0 {
widget.idx = len(widget.filePaths) - 1
}
widget.display()
}
func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) currentFilePath() string {
return widget.filePaths[widget.idx]
}
func (widget *Widget) display() {
widget.View.SetTitle(widget.ContextualTitle(widget.fileName()))
var text string
text := wtf.SigilStr(len(widget.filePaths), widget.idx, widget.View) + "\n"
if wtf.Config.UBool("wtf.mods.textfile.format", false) {
text = widget.formattedText()
text = text + widget.formattedText()
} else {
text = widget.plainText()
text = text + widget.plainText()
}
widget.View.SetText(text)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) fileName() string {
return filepath.Base(widget.filePath)
return filepath.Base(widget.currentFilePath())
}
func (widget *Widget) formattedText() string {
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath())
file, err := os.Open(filePath)
if err != nil {
@@ -98,8 +134,24 @@ func (widget *Widget) formattedText() string {
return tview.TranslateANSI(buf.String())
}
// loadFilePaths parses file paths from the config and stores them in an array
// It is backwards-compatible, supporting both the original, singular filePath and
// the current plural filePaths
func (widget *Widget) loadFilePaths() {
var emptyArray []interface{}
filePath := wtf.Config.UString("wtf.mods.textfile.filePath", "")
filePaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.textfile.filePaths", emptyArray))
if filePath != "" {
filePaths = append(filePaths, filePath)
}
widget.filePaths = filePaths
}
func (widget *Widget) plainText() string {
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath())
text, err := ioutil.ReadFile(filePath) // just pass the file name
if err != nil {
@@ -113,9 +165,26 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
case "/":
widget.ShowHelp()
return nil
case "o":
wtf.OpenFile(widget.filePath)
case "h":
widget.Prev()
return nil
case "l":
widget.Next()
return nil
case "o":
wtf.OpenFile(widget.currentFilePath())
return nil
}
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
}
return event