1
0
mirror of https://github.com/makew0rld/amfora.git synced 2025-01-03 14:56:27 -05:00

Close links chan at end; remove TODOs

This commit is contained in:
makeworld 2021-03-07 22:37:10 -05:00
parent 38674eebbd
commit f3f8a63c38
2 changed files with 9 additions and 8 deletions

View File

@ -48,7 +48,6 @@ type GemtextRenderer struct {
// If it's not a gemini:// page, set this to true. // If it's not a gemini:// page, set this to true.
func NewGemtextRenderer(width int, proxied bool) *GemtextRenderer { func NewGemtextRenderer(width int, proxied bool) *GemtextRenderer {
pr, pw := io.Pipe() pr, pw := io.Pipe()
links := make(chan string, 10)
ansiEnabled := false ansiEnabled := false
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") { if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
@ -62,7 +61,7 @@ func NewGemtextRenderer(width int, proxied bool) *GemtextRenderer {
return &GemtextRenderer{ return &GemtextRenderer{
r: pr, r: pr,
w: pw, w: pw,
links: links, links: make(chan string, 10),
width: width, width: width,
proxied: proxied, proxied: proxied,
ansiEnabled: ansiEnabled, ansiEnabled: ansiEnabled,
@ -272,7 +271,6 @@ func (ren *GemtextRenderer) renderLine(line string) string {
func (ren *GemtextRenderer) ReadFrom(r io.Reader) (int64, error) { func (ren *GemtextRenderer) ReadFrom(r io.Reader) (int64, error) {
// Go through lines, render, and write each line // Go through lines, render, and write each line
// TODO: Should writes be buffered?
var n int64 var n int64
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
@ -282,6 +280,8 @@ func (ren *GemtextRenderer) ReadFrom(r io.Reader) (int64, error) {
n += int64(len(scanner.Bytes())) n += int64(len(scanner.Bytes()))
line := scanner.Text() line := scanner.Text()
// Process the one possibly invisible line
if strings.HasPrefix(line, "```") { if strings.HasPrefix(line, "```") {
ren.pre = !ren.pre ren.pre = !ren.pre
continue continue
@ -295,6 +295,10 @@ func (ren *GemtextRenderer) ReadFrom(r io.Reader) (int64, error) {
)) ))
} }
// Everything has been read, no more links
close(ren.links)
return n, scanner.Err() return n, scanner.Err()
} }

View File

@ -22,9 +22,8 @@ type Renderer interface {
io.ReadWriter io.ReadWriter
io.ReaderFrom io.ReaderFrom
// Links returns a channel that yields Link URLs as they are parsed. // Links returns a channel that yields link URLs as they are parsed.
// It is buffered. The channel might be closed to indicate links aren't supported // It is buffered. The channel will be closed when there won't be anymore links.
// for this renderer.
Links() <-chan string Links() <-chan string
} }
@ -62,7 +61,6 @@ func NewPlaintextRenderer() *PlaintextRenderer {
func (ren *PlaintextRenderer) ReadFrom(r io.Reader) (int64, error) { func (ren *PlaintextRenderer) ReadFrom(r io.Reader) (int64, error) {
// Go through lines and escape bytes and write each line // Go through lines and escape bytes and write each line
// TODO: Should writes be buffered?
var n int64 var n int64
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
@ -127,7 +125,6 @@ func (ren *ANSIRenderer) Write(p []byte) (n int, err error) {
func (ren *ANSIRenderer) ReadFrom(r io.Reader) (int64, error) { func (ren *ANSIRenderer) ReadFrom(r io.Reader) (int64, error) {
// Go through lines, render, and write each line // Go through lines, render, and write each line
// TODO: Should writes be buffered?
var n int64 var n int64
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)