diff options
author | Sergey Grebenshchikov <sgreben@gmail.com> | 2018-03-28 17:27:59 +0200 |
---|---|---|
committer | Sergey Grebenshchikov <sgreben@gmail.com> | 2018-03-28 17:46:08 +0200 |
commit | c76ad10b1dc63beeee3cf4fa8f4f4f4b02222289 (patch) | |
tree | e5735d201e3ab6267a75dec79ed5d53b0bcdf635 | |
parent | 7e3d9839123783e5c367522953e494789ce3618d (diff) |
Add braille & quarter-block canvas. Add scatter plot type. Re-write linechart + barchart.
33 files changed, 12841 insertions, 502 deletions
@@ -1,4 +1,4 @@ -VERSION = 1.0.1 +VERSION = 1.1.0 APP := jp PACKAGES := $(shell go list -f {{.Dir}} ./...) @@ -14,9 +14,10 @@ Dead simple terminal plots from JSON data. Bar charts and line charts are suppor - [Y values only (X=index)](#y-values-only-xindex) - [Array data](#array-data) - [Line chart](#line-chart) - - [Y values only (X=index)](#y-values-only-xindex-1) - [Array data, separate X and Y values](#array-data-separate-x-and-y-values) - [Array data, XY pairs](#array-data-xy-pairs) + - [Y values only (X=index)](#y-values-only-xindex-1) + - [Scatter plot](#scatter-plot) <!-- /TOC --> @@ -30,16 +31,16 @@ Or [download the binary](https://github.com/sgreben/jp/releases/latest) from the ```bash # Linux -curl -LO https://github.com/sgreben/jp/releases/download/1.0.1/jp_1.0.1_linux_x86_64.zip -unzip jp_1.0.1_linux_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.0/jp_1.1.0_linux_x86_64.zip +unzip jp_1.1.0_linux_x86_64.zip # OS X -curl -LO https://github.com/sgreben/jp/releases/download/1.0.1/jp_1.0.1_osx_x86_64.zip -unzip jp_1.0.1_osx_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.0/jp_1.1.0_osx_x86_64.zip +unzip jp_1.1.0_osx_x86_64.zip # Windows -curl -LO https://github.com/sgreben/jp/releases/download/1.0.1/jp_1.0.1_windows_x86_64.zip -unzip jp_1.0.1_windows_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.0/jp_1.1.0_windows_x86_64.zip +unzip jp_1.1.0_windows_x86_64.zip ``` ## Use it @@ -49,7 +50,7 @@ unzip jp_1.0.1_windows_x86_64.zip ```text Usage of jp: -type value - Plot type. One of [line bar] (default line) + Plot type. One of [line bar scatter] (default line) -x string x values (JSONPath expression) -y string @@ -60,6 +61,8 @@ Usage of jp: Plot height (default 0 (auto)) -width int Plot width (default 0 (auto)) + -canvas value + Canvas type. One of [full quarter braille auto] (default auto) ``` ## Examples @@ -68,6 +71,7 @@ Usage of jp: In case you're on mobile, here's some PNGs of what `jp` output looks like: +- ![Scatter plot](docs/scatter_plot.png) - ![Bar chart](docs/bar_chart.png) - ![Line chart](docs/line_chart.png) @@ -77,16 +81,18 @@ In case you're on mobile, here's some PNGs of what `jp` output looks like: ```bash $ cat examples/tcp-time.json | jp -x ..Label -y ..Count -type bar - 69.0 - █████████████ - █████████████ - █████████████ - █████████████ - █████████████ 21.0 - █████████████ █████████████ 7.0 2.0 1.0 - █████████████ █████████████ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ - - 46.85267ms 48.38578ms 49.91889ms 51.452ms 52.98511ms + + 69 + █████████████ + █████████████ + █████████████ + █████████████ + █████████████ + █████████████ 21 + █████████████ █████████████ 7 2 1 + █████████████ █████████████ ▄▄▄▄▄▄▄▄▄▄▄▄▄ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ + + 46.85267ms 48.38578ms 49.91889ms 51.452ms 52.98511ms ``` @@ -94,16 +100,18 @@ $ cat examples/tcp-time.json | jp -x ..Label -y ..Count -type bar ```bash $ cat examples/tcp-time.json | jp -xy "..[Label,Count]" -type bar - 69.0 - █████████████ - █████████████ - █████████████ - █████████████ - █████████████ 21.0 - █████████████ █████████████ 7.0 2.0 1.0 - █████████████ █████████████ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ - - 46.85267ms 48.38578ms 49.91889ms 51.452ms 52.98511ms + + 69 + █████████████ + █████████████ + █████████████ + █████████████ + █████████████ + █████████████ 21 + █████████████ █████████████ 7 2 1 + █████████████ █████████████ ▄▄▄▄▄▄▄▄▄▄▄▄▄ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ + + 46.85267ms 48.38578ms 49.91889ms 51.452ms 52.98511ms ``` #### Y values only (X=index) @@ -111,16 +119,17 @@ $ cat examples/tcp-time.json | jp -xy "..[Label,Count]" -type bar ```bash $ cat examples/tcp-time.json | jp -y ..Count -type bar - 69.0 - █████████████ - █████████████ - █████████████ - █████████████ - █████████████ 21.0 - █████████████ █████████████ 7.0 2.0 1.0 - █████████████ █████████████ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ - - 0 1 2 3 4 + 69 + █████████████ + █████████████ + █████████████ + █████████████ + █████████████ + █████████████ 21 + █████████████ █████████████ 7 2 1 + █████████████ █████████████ ▄▄▄▄▄▄▄▄▄▄▄▄▄ ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁ + + 0 1 2 3 4 ``` #### Array data @@ -128,83 +137,145 @@ $ cat examples/tcp-time.json | jp -y ..Count -type bar ```bash $ echo '[[-3, 5], [-2, 0], [-1, 0.1], [0, 1], [1, 2], [2, 3]]' | jp -xy '[*][0, 1]' -type bar - 5.0 - ███████████ - ███████████ - ███████████ 3.0 - ███████████ ███████████ - ███████████ 2.0 ███████████ - ███████████ 0.0 0.1 1.0 ███████████ ███████████ - ███████████ ▁▁▁▁▁▁▁▁▁▁▁ ███████████ ███████████ ███████████ - - -3 -2 -1 0 1 2 + 5 + ███████████ + ███████████ + ███████████ 3 + ███████████ ▄▄▄▄▄▄▄▄▄▄▄ + ███████████ 2 ███████████ + ███████████ 1 ███████████ ███████████ + ███████████ 0.1 ▄▄▄▄▄▄▄▄▄▄▄ ███████████ ███████████ + ███████████ 0 ▁▁▁▁▁▁▁▁▁▁▁ ███████████ ███████████ ███████████ + + -3 -2 -1 0 1 2 ``` ### Line chart -#### Y values only (X=index) +#### Array data, separate X and Y values ```bash -$ cat examples/tcp-time.json | jp -y ..Duration - -59970040.9│ - │ - │ - │ █ - │██ - │██ █ - │█ █ █ █ - y█ █ ██ ██ █ █ ███ ███ █ █ █ - │ ██ ██ ███ ████ ███ ██ █ ██ █ ███ █ ██ █ █ ██ ██ ██ ██ █ ██ █ █ █ █ - │ █ █ ██ █ ██ ████ ██ █████ ██████ █ ██ ████████ █ ██ █ - │ - │ - │ -42167403.0└───────────────────────────────────────x───────────────────────────────────────────────── - 0.0 99.0 +$ jq -n '[range(200)/20 | [., sin]]' | jp -x '[*][0]' -y '[*][1]' + 1.059955│ ▄▄▄▄▖ ▄▄▄▄▖ + │ ▗▀ ▝▚▖ ▗▛▘ ▝▚▖ + │ ▟▘ ▀▖ ▄▘ ▝▖ + │ ▟ ▝▖ ▞▘ ▝▖ + │ ▟ ▝▖ ▞▘ ▝▖ + │ ▐ ▐ ▗▘ ▐▖ + │ ▐▘ ▚ ▗▘ ▐ + │▗▘ ▚ ▞ ▚ + │▞ ▝▖ ▞ ▌ + │ ▝▖ ▐ ▝▖ + │ ▐ ▗▘ ▀▖ + │ ▚ ▗▘ ▚ + │ ▚ ▗▞ ▙ + │ ▝▚ ▞ ▘ + │ ▝▄ ▗▞ + │ ▝▙ ▞ + │ ▚▖ ▄▛ + │ ▝▀▀▀▀▘ + -1.059955└───────────────────────────────────────────────────────────────────── + 0 10.547 ``` -#### Array data, separate X and Y values +#### Array data, XY pairs ```bash -$ jq -n '[range(200)/20 | [., sin]]' | jp -x '[*][0]' -y '[*][1]' +$ jq -n '[range(200)/20 | [., sin]]' | jp -xy '[*][0, 1]' -1.1 │ ███████ ██████ - │ ███ ███ ███ ███ - │ ███ ██ ██ ███ - │ ██ ███ ██ ██ - │ █ ██ ██ ███ - │██ ██ ██ ██ - │█ █ ██ ██ - y █ ██ █ - │ ██ ██ ██ - │ ██ ██ ██ - │ ██ ██ - │ ██ ████ - │ ██████ --1.1└───────────────────────────────────x─────────────────────────────────────── - 0.0 9.9 + 1.059955│ ▄▄▄▄▖ ▄▄▄▄▖ + │ ▗▀ ▝▚▖ ▗▛▘ ▝▚▖ + │ ▟▘ ▀▖ ▄▘ ▝▖ + │ ▟ ▝▖ ▞▘ ▝▖ + │ ▟ ▝▖ ▞▘ ▝▖ + │ ▐ ▐ ▗▘ ▐▖ + │ ▐▘ ▚ ▗▘ ▐ + │▗▘ ▚ ▞ ▚ + │▞ ▝▖ ▞ ▌ + │ ▝▖ ▐ ▝▖ + │ ▐ ▗▘ ▀▖ + │ ▚ ▗▘ ▚ + │ ▚ ▗▞ ▙ + │ ▝▚ ▞ ▘ + │ ▝▄ ▗▞ + │ ▝▙ ▞ + │ ▚▖ ▄▛ + │ ▝▀▀▀▀▘ + -1.059955└───────────────────────────────────────────────────────────────────── + 0 10.547 ``` -#### Array data, XY pairs +#### Y values only (X=index) ```bash -$ jq -n '[range(200)/20 | [., sin]]' | jp -xy '[*][0, 1]' +$ cat examples/tcp-time.json | jp -y ..Duration -1.1 │ ███████ ██████ - │ ███ ███ ███ ███ - │ ███ ██ ██ ███ - │ ██ ███ ██ ██ - │ █ ██ ██ ███ - │██ ██ ██ ██ - │█ █ ██ ██ - y █ ██ █ - │ ██ ██ ██ - │ ██ ██ ██ - │ ██ ██ - │ ██ ████ - │ ██████ --1.1└───────────────────────────────────x─────────────────────────────────────── - 0.0 9.9 + 5.342888e+07│▐▌ + │▐▌ + │▐▐ + │▐▐ ▐ + │▐▐ ▐ + │▐▐ ▐ + │▐▐ ▐ + │▞▐ ▐▖ + │▌▐ ▐▌ + │▌▐ ▐▌ + │▌▐ ▞▌ ▌ + │▌▐ ▗ ▄ ▌▌ ▌ + │▘▐ ▗ ▐ ▞▐ ▌▌ ▌ + │ ▐ ▗ ▟ █ ▌▐ ▌▌ ▗▌ + │ ▐ ▙ ▐▌ █ █ ▌▐ ▟ ▌▌ ▐▌ + │ ▌ ▛▖ ▐▐ ▗▜ █ ▌ ▌▗ █ ▌▌ ▐▌ + │ ▌ ▟ ▚ ▌▐▗ ▐▝▟▐ ▌ ▌▟▐ ▌ ▌▚ ▐▌ ▖ + │ ▌ █ ▐ ▌▐▐▌▐ █ ▌▗ ▐ ▌▛▟ ▌▗ ▌▐ ▐▐ ▌ + │ ▌▗▀ ▝▄▘ ▛▐▌ ▛ ▌▟ ▐ ▀ ▌ ▌▐ ▌▐ ▐▐ ▗▚ + │ ▌▐ ▘ ▌ ▘ ▌ ▌█ ▐ ▘ ▐▛▖▌▐ ▐▐ ▗▌ ▖ ▖ ▐▐ + │ ▙▐ ▘ ▘ █▐ ▐ ▝▌▌▌▐▄ ▞▐ ▌▌▗▜ ▐▌ ▐▐ ▗▖ + │ ▜▐ ▌ ▌▟ █ ▌▌▐ ▄▘▚▐▐ ▗▄ ▌▐▖ ▗ ▞▐▐ ▞▚ ▗ ▖▗▌▐▄ + │ ▝▞ ▙▘ ▜ ▐▌▐▐▜ ▐▞ ▌▐▝▟ ▝▄▀▄▘▝▞▀▘▝▖ ▞▄▌▌▌▌ + │ ▌ ▛ ▐ ▌▐▌▝ ▝▌ ▚▌ ▌ ▚▞▘▜▐ ▐▘ + │ ▘ ▘ ▝ ▝▌ ▘ ▝ ▝ + │ + │ + │ + 4.591664e+07└────────────────────────────────────────────────────────────────── + 0 97.02 ``` + +### Scatter plot + +```bash +$ cat examples/mvrnorm.json | jp -xy '..[x,y]' -type scatter + + 3.535344│ ⠄ ⠄ + │ ⠈⠂ ⠂ ⡀ ⠂ + │ ⠐⡀⡀⡂ ⠁ ⢄ ⠁ ⠠ + │ ⡀ ⠆ ⠈ ⠄⡀ ⠂ + │ ⡀ ⠠ ⡀ ⡀ ⠄ ⡀⠐⠄⠁⠐ ⠠⢆⠠⠂⠂⠄⣀⢈ ⡀⠈ ⡀ + │ ⡀⠂⠂⠄ ⡀⠂⢔⠠ ⢤⢀⠌⣡⠁⠦⠄⠐⡐⠂⣀⠅⠁⠈ ⠂ ⠈⠁⠁ ⡀ ⠄ + │ ⡀ ⢀ ⠄ ⠈⠠ ⠡⠑⠈⠈⢢⡁⡄⢈⠂⢡⠈⡄⡀⠈⠰⢉⡠⠘⢄⢃⠉⢀⣄⠢⠠⠄ ⠠ ⡀⠁ ⡀ ⠂ + │ ⠈ ⡂⠈⡁⠈⠄⢂⡹⡐⡡⡆⡥⣙⡶⡼⠱⣅⣅⣼⢗⡱⢐⣈⠑⢁⠂ ⢐⢁⠭⠘⡀ ⠈ + │ ⠁ ⢀⠄⢈⠈⡰⢀⡥⠋⣧⣓⣚⡛⢲⣽⣝⣭⢙⣟⢲⡽⣋⡠⣿⣜⣵⠙⡦⠗ ⣡⠁⠁⠁⠄⠠ ⠄⡂ + │ ⠄⠌⠌⠡⠉⡐⢯⣵⡏⢵⡞⠂⢰⣽⣷⢛⣯⡣⣷⢭⣞⣏⠤⣾⢡⡻⠢⢊⢠⡠⠸⢄⣃⡀⢁⠐ ⠐⡀ ⠂ ⠄ + │ ⠨ ⡈⠂ ⢀⢑⠄⣜⡾⣴⢨⠶⣪⣧⢿⣷⣷⡱⣿⣞⣲⣮⣮⣯⢾⡷⡬⡷⣺⠤⢏⡼⣨⢌⡬⠠⢂⢠⠒⠱⠆⡈ + │ ⠈ ⠃ ⠄⡐⠂⠐⢀⢈⣂⡈⣳⣷⣜⢺⣿⣹⣷⣼⣯⡿⣃⣽⣿⢾⣟⣾⢵⣻⠯⡼⡃⣼⣗⢲⠪⠇⣉⠺ ⢱⠠⠙⡀⢐⠌ + │ ⠄ ⠈⠊⠐⠑⠨⠚⢁⡊⢾⡶⢩⢿⣏⣽⢞⣼⣇⣵⣿⣿⣽⣿⢽⣭⠺⣿⣽⣳⢚⣾⣻⣾⣜⠩⡒⣃⠈⢢⠕⢂⢰⡀ ⡔⢀⢀ ⡀ + │ ⢀⠂⠁⠂⠇ ⠂⠊⢀⠐⡘⡍⡇⣚⢸⢟⣯⢿⣳⡪⣫⣵⣿⣯⣿⢿⣷⣻⣖⣗⣻⣚⢥⡷⣕⣏⠶⠊⠄⣠⠰⠂⡄⠂ ⠄⠁ + │ ⠠⢂ ⠘⠐⣀ ⣀⢡⢐⠔⢫⠯⢕⠫⠿⣹⢶⣾⡻⣭⣽⢗⠿⣹⣛⣺⣿⠯⢲⡼⣵⢉⣭⢐⣟⡍⠄⠈⠥⠄ ⠁ ⠄ + │ ⠈⠂ ⠊⡀⡈⠢⡌⡠⠖⢤⠥⡑⣯⣾⣴⣯⡿⣯⣝⣯⣿⠧⣽⣒⢾⣼⣻⣛⣗⡹⡽⢪⠯⠒⡨⠈ ⠈⡐⢄ ⠂⠘⠠ ⠂ + │ ⠰ ⡀⠃⠁⠠ ⠉⡈⡨⡱⢍⠌⠷⣯⠫⠬⡙⣴⣯⡣⡟⡮⠩⣫⠿⢞⢵⡰⠞⡂⠴ ⠕⢀⡂⠁ ⢀ ⠤ ⠈ + │ ⢩⠂ ⠁⡄ ⢀⠲⢂⠑⢁⡘⠄⠵⣣⢑⢻⠨⡩⣌⠕⢮⣮⣋⢹⡁⣊⡃⠈⡕⡘⡠⠨⠄⡘⠨ ⠊⠁ ⠂ + │ ⠐ ⢀ ⠈⠐⠔⠈ ⠁⢀⣀⡃⣊⢁⡘⠁⠛⠨ ⠒⡑⡀⠵⢙⠄⠡⠢⠃⠄⠋⠅ ⠥⠁⠠⢀ ⠄ + │ ⢀⢁ ⠆ ⠉⠁⠐ ⠄⠁⢑⡀⢀⠠⠑⢡⢊⠂⠑⠌⡅⠊⠄⠉⢈⡐ ⡀ ⠠ ⠂ + │ ⢀ ⠑ ⠂ ⡁ ⠌⢠⠈⠂⠄⠉⡃⠈⠄ ⠂⠠⠁ ⠄ ⢀ ⠠ + │ ⠐ ⠐ ⢁⠂⠂⠢⠠⠄⠔⠐ ⠁ + │ ⠢ ⠁ ⠂⠐ ⠐ ⠐ ⠈ + │ ⢀ ⠄⠈ ⠈ + │ ⠐ + │ + │ + │ ⠈ +-4.271874└───────────────────────────────────────────────────────────────────── + -4.08815 3.79083 +```
\ No newline at end of file diff --git a/cmd/jp/bar.go b/cmd/jp/bar.go index e1a622b..15dd196 100644 --- a/cmd/jp/bar.go +++ b/cmd/jp/bar.go @@ -4,8 +4,8 @@ import ( "fmt" "reflect" - "github.com/sgreben/jp/pkg/jp" - "github.com/sgreben/jp/pkg/jp/primitives" + "github.com/sgreben/jp/pkg/draw" + "github.com/sgreben/jp/pkg/plot" ) func barPlotData(xvv, yvv [][]reflect.Value) (x []string, y []float64) { @@ -29,10 +29,10 @@ func barPlotData(xvv, yvv [][]reflect.Value) (x []string, y []float64) { return } -func barPlot(xvv, yvv [][]reflect.Value, box primitives.Box) string { +func barPlot(xvv, yvv [][]reflect.Value, c draw.Canvas) string { groups, y := barPlotData(xvv, yvv) - chart := jp.NewBarChart(box.Width, box.Height) - data := new(jp.DataTable) + chart := plot.NewBarChart(c) + data := new(plot.DataTable) if len(groups) != len(y) { for i := range y { data.AddColumn(fmt.Sprint(i)) diff --git a/cmd/jp/canvas_other.go b/cmd/jp/canvas_other.go new file mode 100644 index 0000000..053cef7 --- /dev/null +++ b/cmd/jp/canvas_other.go @@ -0,0 +1,10 @@ +// +build !windows + +package main + +var autoCanvas = map[string]string{ + plotTypeBar: canvasTypeQuarter, + plotTypeLine: canvasTypeQuarter, + plotTypeScatter: canvasTypeBraille, + plotTypeHist: canvasTypeQuarter, +} diff --git a/cmd/jp/canvas_windows.go b/cmd/jp/canvas_windows.go new file mode 100644 index 0000000..de22e5b --- /dev/null +++ b/cmd/jp/canvas_windows.go @@ -0,0 +1,10 @@ +// +build windows + +package main + +var autoCanvas = map[string]string{ + plotTypeBar: canvasTypeFull, + plotTypeLine: canvasTypeFull, + plotTypeScatter: canvasTypeFull, + plotTypeHist: canvasTypeFull, +} diff --git a/cmd/jp/flag.go b/cmd/jp/flag.go index 533b35d..eeec98c 100644 --- a/cmd/jp/flag.go +++ b/cmd/jp/flag.go @@ -6,8 +6,8 @@ import ( ) type enumVar struct { - Choices []string // The acceptable choices the user may pass to the flag - Value string // the current value of the flag + Choices []string + Value string } // Set implements the flag.Value interface. @@ -18,7 +18,7 @@ func (so *enumVar) Set(v string) error { return nil } } - return fmt.Errorf("invalid choice; must be one of %s", strings.Join(so.Choices, ",")) + return fmt.Errorf("must be one of [%s]", strings.Join(so.Choices, " ")) } func (so *enumVar) String() string { diff --git a/cmd/jp/line.go b/cmd/jp/line.go index b47b38c..caca49f 100644 --- a/cmd/jp/line.go +++ b/cmd/jp/line.go @@ -3,8 +3,9 @@ package main import ( "reflect" - "github.com/sgreben/jp/pkg/jp" - "github.com/sgreben/jp/pkg/jp/primitives" + "github.com/sgreben/jp/pkg/draw" + + "github.com/sgreben/jp/pkg/plot" ) func linePlotData(xvv, yvv [][]reflect.Value) (x, y []float64) { @@ -31,10 +32,10 @@ func linePlotData(xvv, yvv [][]reflect.Value) (x, y []float64) { return } -func linePlot(xvv, yvv [][]reflect.Value, box primitives.Box) string { +func linePlot(xvv, yvv [][]reflect.Value, c draw.Canvas) string { x, y := linePlotData(xvv, yvv) - chart := jp.NewLineChart(box.Width, box.Height) - data := new(jp.DataTable) + chart := plot.NewLineChart(c) + data := new(plot.DataTable) data.AddColumn("x") data.AddColumn("y") n := len(x) @@ -51,6 +52,5 @@ func linePlot(xvv, yvv [][]reflect.Value, box primitives.Box) string { for i := 0; i < n; i++ { data.AddRow(x[i%len(x)], y[i%len(y)]) } - chart.Symbol = "█" return chart.Draw(data) } diff --git a/cmd/jp/main.go b/cmd/jp/main.go index c603f68..07de252 100644 --- a/cmd/jp/main.go +++ b/cmd/jp/main.go @@ -8,24 +8,34 @@ import ( "os" "reflect" - "github.com/sgreben/jp/pkg/jp/primitives" + "github.com/sgreben/jp/pkg/draw" "github.com/sgreben/jp/pkg/terminal" "github.com/sgreben/jp/pkg/jsonpath" ) type configuration struct { - Box primitives.Box - X string - Y string - XY string - PlotType enumVar + Box draw.Box + X string + Y string + XY string + PlotType enumVar + CanvasType enumVar } -const plotTypeLine = "line" -const plotTypeBar = "bar" -const plotTypeScatter = "scatter" -const plotTypeHist = "hist" +const ( + plotTypeLine = "line" + plotTypeBar = "bar" + plotTypeScatter = "scatter" + plotTypeHist = "hist" +) + +const ( + canvasTypeFull = "full" + canvasTypeQuarter = "quarter" + canvasTypeBraille = "braille" + canvasTypeAuto = "auto" +) var config = configuration{ PlotType: enumVar{ @@ -33,16 +43,29 @@ var config = configuration{ Choices: []string{ plotTypeLine, plotTypeBar, + plotTypeScatter, + }, + }, + CanvasType: enumVar{ + Value: canvasTypeAuto, + Choices: []string{ + canvasTypeFull, + canvasTypeQuarter, + canvasTypeBraille, + canvasTypeAuto, }, }, } -var xPattern *jsonpath.JSONPath -var yPattern *jsonpath.JSONPath -var xyPattern *jsonpath.JSONPath +var ( + xPattern *jsonpath.JSONPath + yPattern *jsonpath.JSONPath + xyPattern *jsonpath.JSONPath +) func init() { flag.Var(&config.PlotType, "type", fmt.Sprintf("Plot type. One of %v", config.PlotType.Choices)) + flag.Var(&config.CanvasType, "canvas", fmt.Sprintf("Canvas type. One of %v", config.CanvasType.Choices)) flag.StringVar(&config.X, "x", "", "x values (JSONPath expression)") flag.StringVar(&config.Y, "y", "", "y values (JSONPath expression)") flag.StringVar(&config.XY, "xy", "", "x,y value pairs (JSONPath expression). Overrides -x and -y if given.") @@ -75,6 +98,9 @@ func init() { if config.Box.Height == 0 { config.Box.Height = terminal.Height() - 1 } + if config.CanvasType.Value == canvasTypeAuto { + config.CanvasType.Value = autoCanvas[config.PlotType.Value] + } } func match(in interface{}, p *jsonpath.JSONPath) [][]reflect.Value { @@ -100,11 +126,24 @@ func main() { x = match(in, xPattern) y = match(in, yPattern) } + buffer := draw.NewBuffer(config.Box) + var p draw.Pixels + switch config.CanvasType.Value { + case canvasTypeBraille: + p = &draw.Braille{Buffer: buffer} |