Skip to content

Commit

Permalink
Display stderr text
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Sep 6, 2021
1 parent fc6f763 commit 51a8a80
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
38 changes: 27 additions & 11 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Snapshot struct {
start time.Time
end time.Time

exitCode int
errorResult []byte

completed bool
err error

Expand Down Expand Up @@ -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...)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 != "" {
Expand All @@ -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
}

Expand Down
12 changes: 10 additions & 2 deletions viddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type HistoryRow struct {

addition *tview.TableCell
deletion *tview.TableCell
exitCode *tview.TableCell
}

type Viddy struct {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 51a8a80

Please sign in to comment.