Skip to content

Commit

Permalink
Merge pull request #50 from guptarohit/add-colors-for-series-cli
Browse files Browse the repository at this point in the history
CLI: Updated `-sc` option to specify colors for multiple series
  • Loading branch information
guptarohit authored Mar 30, 2024
2 parents 28aa5ed + 5c21a9b commit 0992b36
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ Options:
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
-sc series colors
comma-separated series colors corresponding to each series
-sn number of series
number of series (columns) in the input data (default 1)
-ub upper bound
Expand Down
40 changes: 30 additions & 10 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
enableRealTime bool
realTimeDataBuffer int
fps float64 = 24
seriesColor asciigraph.AnsiColor
seriesColors []asciigraph.AnsiColor
captionColor asciigraph.AnsiColor
axisColor asciigraph.AnsiColor
labelColor asciigraph.AnsiColor
Expand All @@ -50,17 +50,17 @@ 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("sc", "`series color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
flag.Func("sc", "comma-separated `series colors` corresponding to each series", func(str string) error {
if colors, ok := parseColors(str); !ok {
return errors.New("unrecognized color, check available color names at https://www.w3.org/TR/SVG11/types.html#ColorKeywords")
} else {
seriesColor = c
seriesColors = colors
return nil
}
})

flag.Func("cc", "`caption color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
if c, ok := parseColor(str); !ok {
return errors.New("unrecognized color, check available color names at https://www.w3.org/TR/SVG11/types.html#ColorKeywords")
} else {
captionColor = c
Expand All @@ -69,7 +69,7 @@ func main() {
})

flag.Func("ac", "y-`axis color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
if c, ok := parseColor(str); !ok {
return errors.New("unrecognized color, check available color names at https://www.w3.org/TR/SVG11/types.html#ColorKeywords")
} else {
axisColor = c
Expand All @@ -78,7 +78,7 @@ func main() {
})

flag.Func("lc", "y-axis `label color` of the plot", func(str string) error {
if c, ok := asciigraph.ColorNames[str]; !ok {
if c, ok := parseColor(str); !ok {
return errors.New("unrecognized color, check available color names at https://www.w3.org/TR/SVG11/types.html#ColorKeywords")
} else {
labelColor = c
Expand Down Expand Up @@ -122,8 +122,8 @@ func main() {
p = math.NaN()
}
series[i] = append(series[i], p)

}

if enableRealTime {
if realTimeDataBuffer > 0 && len(series[0]) > realTimeDataBuffer {
for i := range series {
Expand All @@ -140,7 +140,7 @@ func main() {
asciigraph.Offset(int(offset)),
asciigraph.Precision(precision),
asciigraph.Caption(caption),
asciigraph.SeriesColors(seriesColor),
asciigraph.SeriesColors(seriesColors...),
asciigraph.CaptionColor(captionColor),
asciigraph.AxisColor(axisColor),
asciigraph.LabelColor(labelColor),
Expand Down Expand Up @@ -168,7 +168,7 @@ func main() {
asciigraph.Offset(int(offset)),
asciigraph.Precision(precision),
asciigraph.Caption(caption),
asciigraph.SeriesColors(seriesColor),
asciigraph.SeriesColors(seriesColors...),
asciigraph.CaptionColor(captionColor),
asciigraph.AxisColor(axisColor),
asciigraph.LabelColor(labelColor),
Expand All @@ -179,3 +179,23 @@ func main() {
fmt.Println(plot)
}
}

func parseColors(colors string) ([]asciigraph.AnsiColor, bool) {
colorList := strings.Split(colors, ",")
parsedColors := make([]asciigraph.AnsiColor, len(colorList))

for i, color := range colorList {
parsedColor, ok := parseColor(strings.TrimSpace(color))
if !ok {
return parsedColors, ok
}
parsedColors[i] = parsedColor
}

return parsedColors, true
}

func parseColor(color string) (asciigraph.AnsiColor, bool) {
parsedColor, ok := asciigraph.ColorNames[color]
return parsedColor, ok
}

0 comments on commit 0992b36

Please sign in to comment.