Skip to content

Commit

Permalink
Merge pull request #20 from nineinchnick/ignore-broken-pipe
Browse files Browse the repository at this point in the history
Detect and ignore broken pipe when pager was enabled
  • Loading branch information
nineinchnick authored Mar 22, 2021
2 parents c2858ae + 95ec403 commit d546289
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 17 additions & 13 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"os/exec"
"strings"
"syscall"

runewidth "github.com/mattn/go-runewidth"
)
Expand Down Expand Up @@ -214,7 +216,7 @@ func (enc *TableEncoder) Encode(w io.Writer) error {
}

if err := exp.encodeVals(vals); err != nil {
return nil
return checkErr(err, cmd)
}
continue
}
Expand All @@ -236,7 +238,7 @@ func (enc *TableEncoder) Encode(w io.Writer) error {
}

if err := enc.encodeVals(vals); err != nil {
return err
return checkErr(err, cmd)
}

// draw end border
Expand All @@ -248,10 +250,8 @@ func (enc *TableEncoder) Encode(w io.Writer) error {
// add summary
enc.summarize(w)

// flush will return the error code
err = enc.w.Flush()
if err != nil {
return err
if err := enc.w.Flush(); err != nil {
return checkErr(err, cmd)
}
if cmd != nil {
cmdBuf.Close()
Expand All @@ -268,11 +268,17 @@ func startPager(pagerCmd string, w io.Writer) (*exec.Cmd, io.WriteCloser, error)
if err != nil {
return nil, nil, err
}
// TODO when pager exits early, writes and flushes
// will start producing "broken pipe" errors, how to detect them?
return cmd, cmdBuf, cmd.Start()
}

func checkErr(err error, cmd *exec.Cmd) error {
if cmd != nil && errors.Is(err, syscall.EPIPE) {
// broken pipe means pager quit before consuming all data, which might be expected
return nil
}
return err
}

func (enc *TableEncoder) encodeVals(vals [][]*Value) error {
rs := enc.rowStyle(enc.lineStyle.Row)
// print buffered vals
Expand Down Expand Up @@ -762,17 +768,15 @@ func (enc *ExpandedEncoder) Encode(w io.Writer) error {
}

if err := enc.encodeVals(vals); err != nil {
return err
return checkErr(err, cmd)
}
}

// add summary
enc.summarize(w)

// flush will return the error code
err = enc.w.Flush()
if err != nil {
return err
if err := enc.w.Flush(); err != nil {
return checkErr(err, cmd)
}
if cmd != nil {
cmdBuf.Close()
Expand Down
4 changes: 1 addition & 3 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ func FromMap(opts map[string]string) (Builder, []Option) {
}
}
}
builder := NewTableEncoder
// TODO detect "pager" option, on means auto, always means on
// TODO pager should check both width and height in both TableEncoder and ExpandedEncoder
pager := opts["pager"]
pagerCmd := opts["pager_cmd"]
if pager != "" && pagerCmd != "" {
Expand All @@ -97,6 +94,7 @@ func FromMap(opts map[string]string) (Builder, []Option) {
tableOpts = append(tableOpts, WithMinPagerWidth(-1), WithMinPagerHeight(-1))
}
}
builder := NewTableEncoder
if e, ok := opts["expanded"]; ok {
switch e {
case "auto":
Expand Down

0 comments on commit d546289

Please sign in to comment.