Skip to content

Commit e754499

Browse files
committed
feat(output): print stderr/stdout live
closes #3
1 parent fcb03b5 commit e754499

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
- restore_cache:
1010
keys:
11-
- go-mod-v4-{{ checksum "go.mod" }}
11+
- go-mod-v4-{{ checksum "go.sum" }}
1212
- run:
1313
name: Run unit tests
1414
command: |
@@ -19,7 +19,7 @@ jobs:
1919
- run: make run
2020

2121
- save_cache:
22-
key: go-mod-v4-{{ checksum "go.mod" }}
22+
key: go-mod-v4-{{ checksum "go.sum" }}
2323
paths:
2424
- "/go/pkg/mod"
2525

cmd/duration/duration.go

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"os/exec"
78
"strings"
89
"time"
10+
11+
tm "github.com/buger/goterm"
912
)
1013

1114
// Pads an integer with zeroes to the left
@@ -29,21 +32,43 @@ func getHours(time time.Duration) string {
2932
return padTimePart(hours)
3033
}
3134

32-
// Prints the duration how long a command is already running
33-
func printDuration() {
34-
fmt.Printf("command running since: %s:%s:%s", getHours(0), getMinutes(0), getSeconds(0))
35-
start := time.Now()
35+
func clearTime() {
36+
tm.MoveCursor(tm.Width()-30, tm.Height())
37+
tm.Printf(" ")
38+
}
3639

37-
ticker := time.NewTicker(time.Second)
40+
func printDuration(start time.Time) {
41+
currentTime := time.Since(start)
3842

39-
for range ticker.C {
40-
currentTime := time.Since(start)
41-
fmt.Printf("\rcommand running since: %s:%s:%s", getHours(currentTime), getMinutes(currentTime), getSeconds(currentTime))
42-
}
43+
tm.MoveCursor(tm.Width()-30, tm.Height())
44+
tm.Printf("command running since: %s:%s:%s", getHours(currentTime), getMinutes(currentTime), getSeconds(currentTime))
45+
tm.Flush()
4346
}
4447

45-
func printCmdOutput(output string) {
46-
fmt.Printf("\n\nOutput:\n%s", output)
48+
func printDurationAndOutput(output *bytes.Buffer) {
49+
ticker := time.NewTicker(time.Nanosecond)
50+
51+
outputAccumulator := ""
52+
outputPrint := ""
53+
54+
start := time.Now()
55+
printDuration(start)
56+
57+
for range ticker.C {
58+
currentOutput := output.String()
59+
60+
if strings.Compare(currentOutput, outputAccumulator) == 1 {
61+
outputPrint = strings.Replace(currentOutput, outputAccumulator, "", 1)
62+
outputAccumulator = currentOutput
63+
64+
clearTime()
65+
tm.MoveCursor(0, tm.Height())
66+
tm.Printf("%s", string(outputPrint))
67+
tm.Flush()
68+
}
69+
70+
printDuration(start)
71+
}
4772
}
4873

4974
func main() {
@@ -55,17 +80,19 @@ func main() {
5580
program := strings.Join(os.Args[1:2], "")
5681
args := strings.Join(os.Args[2:], " ")
5782

58-
go printDuration()
5983
cmd := exec.Command(program, args)
6084

61-
// Currently the output is printed at the end of the program
62-
// We can not differntiate between stdout and stderr anymore
63-
// I couldn't find a good solution to print realtime while also
64-
// printing the current duration readable until now.
65-
output, err := cmd.CombinedOutput()
85+
var output bytes.Buffer
86+
87+
cmd.Stdout = &output
88+
cmd.Stderr = &output
89+
90+
go printDurationAndOutput(&output)
91+
92+
err := cmd.Run()
6693
if err != nil {
6794
panic(err)
6895
}
6996

70-
printCmdOutput(string(output))
97+
time.Sleep(time.Second)
7198
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module github.com/mstruebing/duration
22

33
go 1.12
4+
5+
require (
6+
github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37
7+
golang.org/x/sys v0.0.0-20190610081024-1e42afee0f76 // indirect
8+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37 h1:uxxtrnACqI9zK4ENDMf0WpXfUsHP5V8liuq5QdgDISU=
2+
github.com/buger/goterm v0.0.0-20181115115552-c206103e1f37/go.mod h1:u9UyCz2eTrSGy6fbupqJ54eY5c4IC8gREQ1053dK12U=
3+
golang.org/x/sys v0.0.0-20190610081024-1e42afee0f76 h1:QSmW7Q3mFdAGjtAd0byXmFJ55inUydyZ4WQmiuItAIQ=
4+
golang.org/x/sys v0.0.0-20190610081024-1e42afee0f76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

test-script/script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
sleep 1
44
echo hello
5+
sleep 1
6+
echo error 1>&2
57
sleep 2
68
echo world
79
sleep 3

0 commit comments

Comments
 (0)