Skip to content

Commit

Permalink
Merge branch 'realtime-plot-cli'
Browse files Browse the repository at this point in the history
  • Loading branch information
guptarohit committed Jun 28, 2020
2 parents 3bc8416 + aac4cd4 commit db58ab3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 17 deletions.
20 changes: 20 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ Feed it data points via stdin:

..
Realtime graph for data points via stdin:

::

$ ping -i.2 google.com | grep -oP '(?<=time=).*(?=ms)' --line-buffered | asciigraph -r -h 10 -w 40 -c "realtime plot data (google ping in ms) from stdin"
8.26 ┤ ╭╮
7.77 ┤ ││
7.27 ┤ ││
6.78 ┤ ││ ╭╮
6.29 ┤ ╭╮ ╭╮ ││ ││
5.79 ┤ ││ ││ ││ │╰╮
5.30 ┤ ││ ││ ╭╮││ │ │ ╭
4.81 ┤ ││ ││ ││││ │ │ │
4.32 ┤ ╭╮ ││╭╯│ ╭╮ ││││ │ │ ╭╯
3.82 ┼╮│╰─╯╰╯ │╭──╯╰─╯╰╯╰──╮ ╭───╮╭───╯ ╰──╯
3.33 ┤╰╯ ╰╯ ╰─╯ ╰╯
realtime plot data (google ping in ms) from stdin

..

Acknowledgement
----------------
Expand Down
54 changes: 38 additions & 16 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
)

var (
height uint
width uint
offset uint = 3
caption string
height uint
width uint
offset uint = 3
caption string
enableRealTime bool
realTimeDataBuffer int
)

func main() {
Expand All @@ -30,10 +32,16 @@ func main() {
flag.UintVar(&width, "w", width, "`width` in columns, 0 for auto-scaling")
flag.UintVar(&offset, "o", offset, "`offset` in columns, for the label")
flag.StringVar(&caption, "c", caption, "`caption` for the graph")
flag.BoolVar(&enableRealTime, "r", enableRealTime, "enables `realtime` graph for data stream")
flag.IntVar(&realTimeDataBuffer, "b", realTimeDataBuffer, "data points `buffer` when realtime graph enabled, default equal to `width`")
flag.Parse()

data := make([]float64, 0, 64)

if realTimeDataBuffer == 0 {
realTimeDataBuffer = int(width)
}

s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanWords)
for s.Scan() {
Expand All @@ -44,20 +52,34 @@ func main() {
continue
}
data = append(data, p)
if enableRealTime {
if realTimeDataBuffer > 0 && len(data) > realTimeDataBuffer {
data = data[len(data)-realTimeDataBuffer:]
}
plot := asciigraph.Plot(data,
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
asciigraph.Caption(caption))
asciigraph.Clear()
fmt.Println(plot)
}
}
if err := s.Err(); err != nil {
log.Fatal(err)
}
if !enableRealTime {
if err := s.Err(); err != nil {
log.Fatal(err)
}

if len(data) == 0 {
log.Fatal("no data")
}
if len(data) == 0 {
log.Fatal("no data")
}

plot := asciigraph.Plot(data,
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
asciigraph.Caption(caption))
plot := asciigraph.Plot(data,
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
asciigraph.Caption(caption))

fmt.Println(plot)
fmt.Println(plot)
}
}
27 changes: 26 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package asciigraph

import "math"
import (
"fmt"
"math"
"os"
"os/exec"
"runtime"
)

func minMaxFloat64Slice(v []float64) (min, max float64) {
min = math.Inf(1)
Expand Down Expand Up @@ -61,3 +67,22 @@ func interpolateArray(data []float64, fitCount int) []float64 {
interpolatedData = append(interpolatedData, data[len(data)-1])
return interpolatedData
}

// clear terminal screen
var Clear func()

func init() {
platform := runtime.GOOS

if platform == "windows" {
Clear = func() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}
} else {
Clear = func() {
fmt.Print("\033[2J\033[H")
}
}
}

0 comments on commit db58ab3

Please sign in to comment.