Skip to content

Commit

Permalink
Add CLI flag(s) for colored parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
guptarohit committed May 3, 2022
1 parent db79f40 commit f44f963
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 9 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ Running this example would render the following graph:
0.00 ┼╯ ╰
```

### Colored graphs

``` go
package main

import (
"fmt"
"github.com/guptarohit/asciigraph"
)

func main() {
data := make([][]float64, 4)

for i := 0; i < 4; i++ {
for x := -20; x <= 20; x++ {
v := math.NaN()
if r := 20 - i; x >= -r && x <= r {
v = math.Sqrt(math.Pow(float64(r), 2)-math.Pow(float64(x), 2)) / 2
}
data[i] = append(data[i], v)
}
}
graph := asciigraph.PlotMany(data, asciigraph.Precision(0), asciigraph.SeriesColors(
asciigraph.Red,
asciigraph.Yellow,
asciigraph.Green,
asciigraph.Blue,
))

fmt.Println(graph)
}
```

Running this example would render the following graph:

![colored_graph_image][]

## Command line interface

This package also brings a small utility for command line usage.
Expand All @@ -81,20 +118,28 @@ This package also brings a small utility for command line usage.
Usage of asciigraph:
asciigraph [options]
Options:
-ac axis color
y-axis color of the plot
-b buffer
data points buffer when realtime graph enabled, default equal to `width`
-c caption
caption for the graph
-cc caption color
caption color of the plot
-f fps
set fps to control how frequently graph to be rendered when realtime graph enabled (default 24)
-h height
height in text rows, 0 for auto-scaling
-lc label color
y-axis label color of the plot
-o offset
offset in columns, for the label (default 3)
-p precision
precision of data point labels along the y-axis (default 2)
-r realtime
enables realtime graph for data stream
-sc series color
series color of the plot
-w width
width in columns, 0 for auto-scaling
asciigraph expects data points from stdin. Invalid values are logged to stderr.
Expand Down Expand Up @@ -174,6 +219,7 @@ Feel free to make a pull request! :octocat:
[Mentioned in Awesome Go]: https://awesome.re/mentioned-badge-flat.svg
[6]: https://github.com/avelino/awesome-go#advanced-console-uis
[image]: https://user-images.githubusercontent.com/7895001/41509956-b1b2b3d0-7279-11e8-9d19-d7dea17d5e44.png
[colored_graph_image]: https://user-images.githubusercontent.com/7895001/166443444-40ad8113-2c0f-46d7-9c75-1cf08435ce15.png
[releases]: https://github.com/guptarohit/asciigraph/releases
[asciichart]: https://github.com/kroitor/asciichart
[asciinema]: https://asciinema.org/a/382383.svg
Expand Down
2 changes: 1 addition & 1 deletion asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func PlotMany(data [][]float64, options ...Option) string {
if !math.IsNaN(series[0]) {
y0 = int(round(series[0]*ratio) - min2)
plot[rows-y0][config.Offset-1].Text = "┼" // first value
plot[rows-y0][config.Offset-1].Color = color
plot[rows-y0][config.Offset-1].Color = config.AxisColor
}

for x := 0; x < len(series)-1; x++ { // plot the line
Expand Down
4 changes: 2 additions & 2 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func TestPlotMany(t *testing.T) {
{
[][]float64{{0, 0}, {math.NaN(), 0}},
[]Option{SeriesColors(Red)},
" 0.00 \x1b[91m┼\x1b[0m╶"},
" 0.00 ╶"},
{
[][]float64{{0, 0}, {math.NaN(), 0}},
[]Option{SeriesColors(Default, Red)},
Expand All @@ -319,7 +319,7 @@ func TestPlotMany(t *testing.T) {
`
2.00 ┤\x1b[91m╭╭\x1b[0m
1.00 ┤\x1b[91m││\x1b[0m
0.00 \x1b[91m╯╯\x1b[0m`},
0.00 \x1b[91m╯╯\x1b[0m`},
}

for i := range cases {
Expand Down
49 changes: 43 additions & 6 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ var (
enableRealTime bool
realTimeDataBuffer int
fps float64 = 24
color asciigraph.AnsiColor
seriesColor asciigraph.AnsiColor
captionColor asciigraph.AnsiColor
axisColor asciigraph.AnsiColor
labelColor asciigraph.AnsiColor
)

func main() {
Expand All @@ -41,14 +44,42 @@ func main() {
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.Float64Var(&fps, "f", fps, "set `fps` to control how frequently graph to be rendered when realtime graph enabled")
flag.Func("l", "`color` of the data line", func(str string) error {
flag.Func("sc", "`series color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
return errors.New("unrecognized color")
return errors.New("unrecognized seriesColor")
} else {
color = c
seriesColor = c
return nil
}
})

flag.Func("cc", "`caption color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
return errors.New("unrecognized seriesColor")
} else {
captionColor = c
return nil
}
})

flag.Func("ac", "y-`axis color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
return errors.New("unrecognized seriesColor")
} else {
axisColor = c
return nil
}
})

flag.Func("lc", "y-axis `label color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
return errors.New("unrecognized seriesColor")
} else {
labelColor = c
return nil
}
})

flag.Parse()

data := make([]float64, 0, 64)
Expand Down Expand Up @@ -84,7 +115,10 @@ func main() {
asciigraph.Offset(int(offset)),
asciigraph.Precision(precision),
asciigraph.Caption(caption),
asciigraph.SeriesColors(color),
asciigraph.SeriesColors(seriesColor),
asciigraph.CaptionColor(captionColor),
asciigraph.AxisColor(axisColor),
asciigraph.LabelColor(labelColor),
)
asciigraph.Clear()
fmt.Println(plot)
Expand All @@ -107,7 +141,10 @@ func main() {
asciigraph.Offset(int(offset)),
asciigraph.Precision(precision),
asciigraph.Caption(caption),
asciigraph.SeriesColors(color),
asciigraph.SeriesColors(seriesColor),
asciigraph.CaptionColor(captionColor),
asciigraph.AxisColor(axisColor),
asciigraph.LabelColor(labelColor),
)

fmt.Println(plot)
Expand Down

0 comments on commit f44f963

Please sign in to comment.