Skip to content

Commit

Permalink
Add simple command line utility
Browse files Browse the repository at this point in the history
  • Loading branch information
dmke committed Jun 26, 2018
1 parent 655e935 commit 9537a7c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ Running this example would render the following graph:
2.00 ┤ ╰╯ ╰╯╰╯
..
Command line interface
----------------------

This package also brings a small utility for command line usage. Assuming
`$GOPATH/bin` is in yout `$PATH`, simply `go get` it, and feed it data
points via stdin:

::

$ go install github.com/guptarohit/asciigraph/cmd/asciigraph
$ seq 1 72 | asciigraph -h 10 -c "plot data from stdin"
72.00 ┼
65.55 ┤ ╭────
59.09 ┤ ╭──────╯
52.64 ┤ ╭──────╯
46.18 ┤ ╭──────╯
39.73 ┤ ╭──────╯
33.27 ┤ ╭───────╯
26.82 ┤ ╭──────╯
20.36 ┤ ╭──────╯
13.91 ┤ ╭──────╯
7.45 ┤ ╭──────╯
1.00 ┼──╯
plot data from stdin
..

Acknowledgement
----------------
This package is golang port of library `asciichart <https://github.com/kroitor/asciichart>`_ written by `@kroitor <https://github.com/kroitor>`_.
Expand Down
61 changes: 61 additions & 0 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"

"github.com/guptarohit/asciigraph"
)

var (
height uint
width uint
offset uint = 3
caption string
)

func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [options]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "%s expects data points from stdin. Invalid values are logged to stderr.", os.Args[0])
}
flag.UintVar(&height, "h", height, "`height` in text rows, 0 for auto-scaling")
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.Parse()

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

s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanWords)
for s.Scan() {
p, err := strconv.ParseFloat(s.Text(), 64)
if err != nil {
continue
}
data = append(data, p)
}
if err := s.Err(); err != nil {
log.Fatal(err)
}

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))

fmt.Println(plot)
}

0 comments on commit 9537a7c

Please sign in to comment.