From 51a8a801fac1117a65100ccbcb20dc2a69aaa8b4 Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 6 Sep 2021 19:38:34 +0900 Subject: [PATCH] Display stderr text --- snapshot.go | 38 +++++++++++++++++++++++++++----------- viddy.go | 12 ++++++++++-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/snapshot.go b/snapshot.go index d2d0347..f4b7db1 100644 --- a/snapshot.go +++ b/snapshot.go @@ -31,6 +31,9 @@ type Snapshot struct { start time.Time end time.Time + exitCode int + errorResult []byte + completed bool err error @@ -100,6 +103,7 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error { }() var b bytes.Buffer + var eb bytes.Buffer commands := []string{s.command} commands = append(commands, s.args...) @@ -119,6 +123,7 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error { } command.Stdout = &b + command.Stderr = &eb if err := command.Start(); err != nil { return nil //nolint:nilerr @@ -130,6 +135,8 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error { } s.result = b.Bytes() + s.errorResult = eb.Bytes() + s.exitCode = command.ProcessState.ExitCode() s.completed = true finishedQueue <- s.id close(s.finish) @@ -138,29 +145,39 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error { return nil } +func isWhiteString(str string) bool { + for _, c := range str { + if !unicode.IsSpace(c) { + return false + } + } + return true +} + func (s *Snapshot) render(w io.Writer, isShowDiff bool, query string) error { - var err error + src := string(s.result) - var src string + if isWhiteString(src) { + src = string(s.errorResult) + io.WriteString(w, fmt.Sprintf(`[red]%s[-:-:-]`, src)) + return nil + } //nolint:nestif if isShowDiff { if s.diffPrepared { src = DiffPrettyText(s.diff) } else { - err := s.compareFromBefore() - if err != nil { - src = string(s.result) - } else { + if err := s.compareFromBefore(); err == nil { src = DiffPrettyText(s.diff) } } - } else { - src = string(s.result) } var b bytes.Buffer - _, err = io.Copy(tview.ANSIWriter(&b), strings.NewReader(src)) + if _, err := io.Copy(tview.ANSIWriter(&b), strings.NewReader(src)); err != nil { + return err + } var r io.Reader if query != "" { @@ -169,8 +186,7 @@ func (s *Snapshot) render(w io.Writer, isShowDiff bool, query string) error { r = &b } - _, _ = io.Copy(w, r) - + _, err := io.Copy(w, r) return err } diff --git a/viddy.go b/viddy.go index d2bd476..40f557d 100644 --- a/viddy.go +++ b/viddy.go @@ -18,6 +18,7 @@ type HistoryRow struct { addition *tview.TableCell deletion *tview.TableCell + exitCode *tview.TableCell } type Viddy struct { @@ -213,6 +214,10 @@ func (v *Viddy) queueHandler() { v.diffQueue <- s.id + if s.exitCode > 0 { + r.exitCode.SetText(fmt.Sprintf("E(%d)", s.exitCode)) + } + ls := v.getSnapShot(v.latestFinishedID) if ls == nil || s.start.After(ls.start) { v.latestFinishedID = id @@ -229,19 +234,22 @@ func (v *Viddy) queueHandler() { s := v.getSnapShot(id) idCell := tview.NewTableCell(strconv.FormatInt(s.id, 10)).SetTextColor(tview.Styles.SecondaryTextColor) - additionCell := tview.NewTableCell("+0").SetTextColor(tcell.ColorGreen) - deletionCell := tview.NewTableCell("-0").SetTextColor(tcell.ColorRed) + additionCell := tview.NewTableCell("").SetTextColor(tcell.ColorGreen) + deletionCell := tview.NewTableCell("").SetTextColor(tcell.ColorRed) + exitCodeCell := tview.NewTableCell("").SetTextColor(tcell.ColorYellow) v.historyRows[s.id] = &HistoryRow{ id: idCell, addition: additionCell, deletion: deletionCell, + exitCode: exitCodeCell, } v.historyView.InsertRow(0) v.historyView.SetCell(0, 0, idCell) v.historyView.SetCell(0, 1, additionCell) v.historyView.SetCell(0, 2, deletionCell) + v.historyView.SetCell(0, 3, exitCodeCell) v.Lock() v.idList = append(v.idList, id)