Skip to content

Commit

Permalink
Process final line if it doesn't have a \n
Browse files Browse the repository at this point in the history
  • Loading branch information
keyneston committed Apr 30, 2021
1 parent 5cd89fb commit cfaf85a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (c *Config) Register(f *flag.FlagSet) *Config {
c.fset.Var(&c.Format, "format", "Alias for -f")
c.fset.Var(&c.Alignments, "a", "Set column alignments; Can be called multiple times and/or comma separated. Arrow indicates direction '<' left, '>' right, '=' center; Columns are zero indexed; e.g. -a '0<,1>,2='")
c.fset.BoolVar(&c.Version, "v", false, "Print version info")
c.fset.BoolVar(&c.Version, "version", false, "Alias for -v")

return c
}
Expand Down
9 changes: 9 additions & 0 deletions table/read_mk.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func (t *Table) readFormatMK(r io.Reader) error {
}
}

trimmedData := bytes.TrimSpace(data)
if atEOF && len(trimmedData) > 0 {
return len(data) + 1, trimmedData, nil
}

return 0, nil, nil
})

Expand Down Expand Up @@ -75,5 +80,9 @@ func (t *Table) readFormatMK(r io.Reader) error {
return err
}

if len(current) > 0 {
t.data = append(t.data, current)
}

return nil
}
4 changes: 4 additions & 0 deletions table/read_mk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestReadFormatMK(t *testing.T) {
name: "trailing pipe", input: "| a | b | \\| |\n",
expected: [][]string{{"a", "b", `\|`}},
},
{
name: "missing final newline", input: "| a | b | a |\n|1 | 2 |3|",
expected: [][]string{{"a", "b", "a"}, {"1", "2", "3"}},
},
}

for _, c := range cases {
Expand Down
16 changes: 11 additions & 5 deletions table/read_re.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (

func (t *Table) readFormatRE(r io.Reader) error {
reader := bufio.NewReader(r)
for {

done := false
for !done {
line, err := reader.ReadSlice(byte(t.NewLine))
if err != nil {
if errors.Is(err, io.EOF) {
return nil
if !errors.Is(err, io.EOF) {
return err
}
return err

// If we receive io.EOF then we need to finish processing the final
// line, then stop looping.
done = true
}
if line == nil {
return nil
Expand All @@ -31,6 +36,7 @@ func (t *Table) readFormatRE(r io.Reader) error {
res[i] = prepareContent(res[i])
}
t.data = append(t.data, res)

}

return nil
}
5 changes: 5 additions & 0 deletions table/read_re_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func TestReadFormatRE(t *testing.T) {
sep: `\t+`,
expected: [][]string{{"a", "1"}, {"b", "2"}, {"c", "3"}},
},
{
name: "missing final newline", input: "a\tb\ta\t\n1\t2\t3",
sep: `\t+`,
expected: [][]string{{"a", "b", "a"}, {"1", "2", "3"}},
},
}

for _, c := range cases {
Expand Down

0 comments on commit cfaf85a

Please sign in to comment.