From 6d8b20c78524e4fc1282c421f0443b1df12e7e7d Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 19 Aug 2018 14:07:34 -0700 Subject: [PATCH] Formatting text in the textview is now optional --- textfile/widget.go | 78 ++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/textfile/widget.go b/textfile/widget.go index 8b93eac3..30d6ba9d 100644 --- a/textfile/widget.go +++ b/textfile/widget.go @@ -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,35 +51,14 @@ 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 { - lexer := lexers.Match(filePath) - if lexer == nil { - lexer = lexers.Fallback - } - - style := styles.Get(wtf.Config.UString("wtf.mods.textfile.formatStyle", "vim")) - if style == nil { - style = styles.Fallback - } - formatter := formatters.Get("terminal256") - if formatter == nil { - formatter = formatters.Fallback - } - - contents, _ := ioutil.ReadAll(file) - iterator, _ := lexer.Tokenise(nil, string(contents)) - - var buf bytes.Buffer - formatter.Format(&buf, style, iterator) - - formatted := tview.TranslateANSI(buf.String()) - widget.View.SetText(formatted) + text = widget.plainText() } + + widget.View.SetText(text) } /* -------------------- Unexported Functions -------------------- */ @@ -92,6 +67,47 @@ 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 + } + + style := styles.Get(wtf.Config.UString("wtf.mods.textfile.formatStyle", "vim")) + if style == nil { + style = styles.Fallback + } + formatter := formatters.Get("terminal256") + if formatter == nil { + formatter = formatters.Fallback + } + + contents, _ := ioutil.ReadAll(file) + iterator, _ := lexer.Tokenise(nil, string(contents)) + + var buf bytes.Buffer + formatter.Format(&buf, style, iterator) + + return tview.TranslateANSI(buf.String()) +} + +func (widget *Widget) plainText() string { + filePath, _ := wtf.ExpandHomeDir(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 { switch string(event.Rune()) { case "/":