11package main
22
33import (
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 ("\r command 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 \n Output:\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
4974func 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}
0 commit comments