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

Formatting text in the textview is now optional

This commit is contained in:
Chris Cummer 2018-08-19 14:07:34 -07:00
parent 976e24b431
commit 6d8b20c785

View File

@ -1,18 +1,14 @@
package textfile
import (
//"bufio"
"bytes"
"io/ioutil"
"os"
"path/filepath"
//"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
//"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
@ -55,12 +51,30 @@ func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.View.SetTitle(widget.ContextualTitle(widget.fileName()))
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
file, err := os.Open(filePath)
if err != nil {
widget.View.SetText(err.Error())
var text string
if wtf.Config.UBool("wtf.mods.textfile.format", false) {
text = widget.formattedText()
} else {
text = widget.plainText()
}
widget.View.SetText(text)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) fileName() string {
return filepath.Base(widget.filePath)
}
func (widget *Widget) formattedText() string {
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
file, err := os.Open(filePath)
if err != nil {
return err.Error()
}
lexer := lexers.Match(filePath)
if lexer == nil {
lexer = lexers.Fallback
@ -81,15 +95,17 @@ func (widget *Widget) Refresh() {
var buf bytes.Buffer
formatter.Format(&buf, style, iterator)
formatted := tview.TranslateANSI(buf.String())
widget.View.SetText(formatted)
}
return tview.TranslateANSI(buf.String())
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) plainText() string {
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
func (widget *Widget) fileName() string {
return filepath.Base(widget.filePath)
text, err := ioutil.ReadFile(filePath) // just pass the file name
if err != nil {
return err.Error()
}
return string(text)
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {