1
1
mirror of https://github.com/walles/moar.git synced 2024-11-30 02:34:13 +03:00

NewReaderFromStream(): Make name mandatory

This makes the API somewhat simpler to use. To support not providing any
name we still allow the empty string as the name, and document that
thoroughly.
This commit is contained in:
Johan Walles 2020-12-30 19:00:03 +01:00
parent f0dead13ad
commit 570d780bc2
5 changed files with 23 additions and 16 deletions

View File

@ -92,8 +92,7 @@ func main() {
fmt.Fprintln(&buf, "March")
fmt.Fprintln(&buf, "April")
name := "Months"
err := m.Page(m.NewReaderFromStream(&name, &buf))
err := m.Page(m.NewReaderFromStream("Months", &buf))
if err != nil {
// Handle paging problems
panic(err)

View File

@ -12,7 +12,7 @@ import (
)
func TestUnicodeRendering(t *testing.T) {
reader := NewReaderFromStream(nil, strings.NewReader("åäö"))
reader := NewReaderFromStream("", strings.NewReader("åäö"))
if err := reader._Wait(); err != nil {
panic(err)
}
@ -47,7 +47,7 @@ func createExpectedCell(Rune rune, Style tcell.Style) Token {
}
func TestFgColorRendering(t *testing.T) {
reader := NewReaderFromStream(nil, strings.NewReader(
reader := NewReaderFromStream("", strings.NewReader(
"\x1b[30ma\x1b[31mb\x1b[32mc\x1b[33md\x1b[34me\x1b[35mf\x1b[36mg\x1b[37mh\x1b[0mi"))
if err := reader._Wait(); err != nil {
panic(err)
@ -73,7 +73,7 @@ func TestFgColorRendering(t *testing.T) {
func TestBrokenUtf8(t *testing.T) {
// The broken UTF8 character in the middle is based on "©" = 0xc2a9
reader := NewReaderFromStream(nil, strings.NewReader("abc\xc2def"))
reader := NewReaderFromStream("", strings.NewReader("abc\xc2def"))
if err := reader._Wait(); err != nil {
panic(err)
}
@ -113,7 +113,7 @@ func startPaging(t *testing.T, reader *Reader) []tcell.SimCell {
// assertIndexOfFirstX verifies the (zero-based) index of the first 'x'
func assertIndexOfFirstX(t *testing.T, s string, expectedIndex int) {
reader := NewReaderFromStream(nil, strings.NewReader(s))
reader := NewReaderFromStream("", strings.NewReader(s))
if err := reader._Wait(); err != nil {
panic(err)
}
@ -192,7 +192,7 @@ func TestCodeHighlighting(t *testing.T) {
}
func testManPageFormatting(t *testing.T, input string, expected Token) {
reader := NewReaderFromStream(nil, strings.NewReader(input))
reader := NewReaderFromStream("", strings.NewReader(input))
if err := reader._Wait(); err != nil {
panic(err)
}
@ -342,7 +342,7 @@ func TestCreateScreenLineScrolled2Utf8SearchHit(t *testing.T) {
}
func TestFindFirstLineOneBasedSimple(t *testing.T) {
reader := NewReaderFromStream(nil, strings.NewReader("AB"))
reader := NewReaderFromStream("", strings.NewReader("AB"))
pager := NewPager(reader)
// Wait for reader to finish reading
@ -356,7 +356,7 @@ func TestFindFirstLineOneBasedSimple(t *testing.T) {
}
func TestFindFirstLineOneBasedAnsi(t *testing.T) {
reader := NewReaderFromStream(nil, strings.NewReader("A\x1b[30mB"))
reader := NewReaderFromStream("", strings.NewReader("A\x1b[30mB"))
pager := NewPager(reader)
// Wait for reader to finish reading

View File

@ -146,11 +146,19 @@ func readStream(stream io.Reader, reader *Reader, fromFilter *exec.Cmd) {
}
// NewReaderFromStream creates a new stream reader
func NewReaderFromStream(name *string, reader io.Reader) *Reader {
//
// The name can be an empty string ("").
//
// If non-empty, the name will be displayed by the pager in the bottom left
// corner to help the user keep track of what is being paged.
func NewReaderFromStream(name string, reader io.Reader) *Reader {
mReader := newReaderFromStream(reader, nil)
mReader.lock.Lock()
mReader.name = name
mReader.lock.Unlock()
if len(name) > 0 {
mReader.lock.Lock()
mReader.name = &name
mReader.lock.Unlock()
}
return mReader
}
@ -342,7 +350,7 @@ func NewReaderFromFilename(filename string) (*Reader, error) {
return nil, err
}
reader := NewReaderFromStream(&filename, stream)
reader := NewReaderFromStream(filename, stream)
return reader, nil
}

View File

@ -172,7 +172,7 @@ func TestGetLongLine(t *testing.T) {
}
func getReaderWithLineCount(totalLines int) *Reader {
reader := NewReaderFromStream(nil, strings.NewReader(strings.Repeat("x\n", totalLines)))
reader := NewReaderFromStream("", strings.NewReader(strings.Repeat("x\n", totalLines)))
if err := reader._Wait(); err != nil {
panic(err)
}

View File

@ -117,7 +117,7 @@ func main() {
if stdinIsRedirected && !stdoutIsRedirected {
// Display input pipe contents
reader := m.NewReaderFromStream(nil, os.Stdin)
reader := m.NewReaderFromStream("", os.Stdin)
startPaging(reader)
return
}