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