2021-03-01 19:05:26 -05:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
2021-03-04 20:09:51 -05:00
|
|
|
"bufio"
|
2021-03-01 19:05:26 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-05-15 23:48:47 -04:00
|
|
|
"regexp"
|
2021-03-01 19:05:26 -05:00
|
|
|
|
2021-05-14 18:49:36 -04:00
|
|
|
"code.rocketnine.space/tslocum/cview"
|
2021-03-01 19:05:26 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Renderer renderers network bytes into something that can be displayed on a
|
|
|
|
// cview.TextView.
|
2021-03-04 20:09:51 -05:00
|
|
|
//
|
2021-05-16 23:08:53 -04:00
|
|
|
// Calling Close when all writing is done is not a no-op, it will stop the the
|
|
|
|
// goroutine that runs for each Renderer.
|
2021-03-04 20:09:51 -05:00
|
|
|
//
|
2021-05-16 23:08:53 -04:00
|
|
|
// Write calls may block if the Lines channel buffer is full.
|
2021-03-01 19:05:26 -05:00
|
|
|
type Renderer interface {
|
2021-05-16 23:08:53 -04:00
|
|
|
io.ReadWriteCloser
|
2021-03-01 19:05:26 -05:00
|
|
|
|
2021-03-07 22:37:10 -05:00
|
|
|
// Links returns a channel that yields link URLs as they are parsed.
|
|
|
|
// It is buffered. The channel will be closed when there won't be anymore links.
|
2021-03-01 19:05:26 -05:00
|
|
|
Links() <-chan string
|
|
|
|
}
|
|
|
|
|
2021-03-04 20:09:51 -05:00
|
|
|
// ScanLines is copied from bufio.ScanLines and is used with bufio.Scanner.
|
|
|
|
// The only difference is that this func doesn't get rid of the end-of-line marker.
|
|
|
|
// This is so that the number of read bytes can be counted correctly in ReadFrom.
|
|
|
|
//
|
|
|
|
// It also simplifes code by no longer having to append a newline character.
|
|
|
|
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
if atEOF && len(data) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
if i := bytes.IndexByte(data, '\n'); i >= 0 {
|
|
|
|
// We have a full newline-terminated line.
|
|
|
|
return i + 1, data[0 : i+1], nil
|
|
|
|
}
|
|
|
|
// If we're at EOF, we have a final, non-terminated line. Return it.
|
|
|
|
if atEOF {
|
|
|
|
return len(data), data, nil
|
|
|
|
}
|
|
|
|
// Request more data.
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlaintextRenderer escapes text for cview usage and does nothing else.
|
2021-03-01 19:05:26 -05:00
|
|
|
type PlaintextRenderer struct {
|
2021-05-16 23:08:53 -04:00
|
|
|
readOut *io.PipeReader
|
|
|
|
readIn *io.PipeWriter
|
|
|
|
writeIn *io.PipeWriter
|
|
|
|
writeOut *io.PipeReader
|
2021-03-01 19:05:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlaintextRenderer() *PlaintextRenderer {
|
2021-05-16 23:08:53 -04:00
|
|
|
pr1, pw1 := io.Pipe()
|
|
|
|
pr2, pw2 := io.Pipe()
|
|
|
|
ren := PlaintextRenderer{
|
|
|
|
readOut: pr1,
|
|
|
|
readIn: pw1,
|
|
|
|
writeIn: pw2,
|
|
|
|
writeOut: pr2,
|
|
|
|
}
|
|
|
|
go ren.handler()
|
|
|
|
return &ren
|
2021-03-01 19:05:26 -05:00
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:53 -04:00
|
|
|
// handler is supposed to run in a goroutine as soon as the renderer is created.
|
|
|
|
// It handles the buffering and parsing in the background.
|
|
|
|
func (ren *PlaintextRenderer) handler() {
|
|
|
|
scanner := bufio.NewScanner(ren.writeOut)
|
2021-03-04 20:09:51 -05:00
|
|
|
scanner.Split(ScanLines)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
//nolint:errcheck
|
2021-05-16 23:08:53 -04:00
|
|
|
ren.readIn.Write(cview.EscapeBytes(scanner.Bytes()))
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
// Close the ends this func touches, shouldn't matter really
|
|
|
|
ren.writeOut.CloseWithError(err)
|
|
|
|
ren.readIn.CloseWithError(err)
|
2021-03-04 20:09:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 20:14:56 -05:00
|
|
|
func (ren *PlaintextRenderer) Write(p []byte) (n int, err error) {
|
2021-05-16 23:08:53 -04:00
|
|
|
return ren.writeIn.Write(p)
|
2021-03-04 20:09:51 -05:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:14:56 -05:00
|
|
|
func (ren *PlaintextRenderer) Read(p []byte) (n int, err error) {
|
2021-05-16 23:08:53 -04:00
|
|
|
return ren.readOut.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ren *PlaintextRenderer) Close() error {
|
|
|
|
// Close user-facing ends of the pipes. Shouldn't matter which ends though
|
|
|
|
ren.writeIn.Close()
|
|
|
|
ren.readOut.Close()
|
|
|
|
return nil
|
2021-03-01 19:05:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:14:56 -05:00
|
|
|
func (ren *PlaintextRenderer) Links() <-chan string {
|
2021-03-01 19:05:26 -05:00
|
|
|
ch := make(chan string)
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2021-03-04 20:09:51 -05:00
|
|
|
// ANSIRenderer escapes text for cview usage, as well as converting ANSI codes
|
|
|
|
// into cview tags if the config allows it.
|
2021-03-01 19:05:26 -05:00
|
|
|
type ANSIRenderer struct {
|
2021-05-16 23:08:53 -04:00
|
|
|
readOut *io.PipeReader
|
|
|
|
readIn *io.PipeWriter
|
|
|
|
writeIn *io.PipeWriter
|
|
|
|
writeOut *io.PipeReader
|
|
|
|
ansiWriter io.Writer // cview.ANSIWriter
|
|
|
|
buf *bytes.Buffer // Where ansiWriter writes to
|
2021-03-01 19:05:26 -05:00
|
|
|
}
|
|
|
|
|
2021-05-15 23:48:47 -04:00
|
|
|
// Regex for identifying ANSI color codes
|
|
|
|
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
|
|
|
|
2021-03-01 19:05:26 -05:00
|
|
|
func NewANSIRenderer() *ANSIRenderer {
|
2021-05-16 23:08:53 -04:00
|
|
|
pr1, pw1 := io.Pipe()
|
|
|
|
pr2, pw2 := io.Pipe()
|
2021-03-01 19:05:26 -05:00
|
|
|
var ansiWriter io.Writer = nil // When ANSI is disabled
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
|
|
|
|
// ANSI enabled
|
|
|
|
ansiWriter = cview.ANSIWriter(&buf)
|
|
|
|
}
|
2021-05-16 23:08:53 -04:00
|
|
|
ren := ANSIRenderer{
|
|
|
|
readOut: pr1,
|
|
|
|
readIn: pw1,
|
|
|
|
writeIn: pw2,
|
|
|
|
writeOut: pr2,
|
|
|
|
ansiWriter: ansiWriter,
|
|
|
|
buf: &buf,
|
|
|
|
}
|
|
|
|
go ren.handler()
|
|
|
|
return &ren
|
2021-03-04 20:09:51 -05:00
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:53 -04:00
|
|
|
// handler is supposed to run in a goroutine as soon as the renderer is created.
|
|
|
|
// It handles the buffering and parsing in the background.
|
|
|
|
func (ren *ANSIRenderer) handler() {
|
2021-03-04 20:09:51 -05:00
|
|
|
// Go through lines, render, and write each line
|
|
|
|
|
2021-05-16 23:08:53 -04:00
|
|
|
scanner := bufio.NewScanner(ren.writeOut)
|
2021-03-04 20:09:51 -05:00
|
|
|
scanner.Split(ScanLines)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
line = cview.EscapeBytes(line)
|
|
|
|
|
|
|
|
if ren.ansiWriter == nil {
|
|
|
|
// ANSI disabled
|
|
|
|
line = ansiRegex.ReplaceAll(scanner.Bytes(), nil)
|
|
|
|
} else {
|
|
|
|
// ANSI enabled
|
|
|
|
|
|
|
|
ren.buf.Reset()
|
|
|
|
|
|
|
|
// Shouldn't error because everything it writes to are all bytes.Buffer
|
|
|
|
ren.ansiWriter.Write(line) //nolint:errcheck
|
|
|
|
|
|
|
|
// The ANSIWriter injects tags like [-:-:-]
|
|
|
|
// but this will reset the background to use the user's terminal color.
|
|
|
|
// These tags need to be replaced with resets that use the theme color.
|
|
|
|
line = bytes.ReplaceAll(
|
|
|
|
ren.buf.Bytes(),
|
|
|
|
[]byte("[-:-:-]"),
|
|
|
|
[]byte(fmt.Sprintf("[-:%s:-]", config.GetColorString("bg"))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-16 23:08:53 -04:00
|
|
|
ren.readIn.Write(line) //nolint:errcheck
|
2021-03-01 19:05:26 -05:00
|
|
|
}
|
2021-03-04 20:09:51 -05:00
|
|
|
|
2021-05-16 23:08:53 -04:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
// Close the ends this func touches, shouldn't matter really
|
|
|
|
ren.writeOut.CloseWithError(err)
|
|
|
|
ren.readIn.CloseWithError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ren *ANSIRenderer) Write(p []byte) (n int, err error) {
|
|
|
|
return ren.writeIn.Write(p)
|
2021-03-04 20:09:51 -05:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:14:56 -05:00
|
|
|
func (ren *ANSIRenderer) Read(p []byte) (n int, err error) {
|
2021-05-16 23:08:53 -04:00
|
|
|
return ren.readOut.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ren *ANSIRenderer) Close() error {
|
|
|
|
// Close user-facing ends of the pipes. Shouldn't matter which ends though
|
|
|
|
ren.writeIn.Close()
|
|
|
|
ren.readOut.Close()
|
|
|
|
return nil
|
2021-03-01 19:05:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:14:56 -05:00
|
|
|
func (ren *ANSIRenderer) Links() <-chan string {
|
2021-03-01 19:05:26 -05:00
|
|
|
ch := make(chan string)
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|