From 3ca42b8dca6cc9705f7a783413726422429dc3fa Mon Sep 17 00:00:00 2001 From: Sergey Grebenshchikov Date: Wed, 28 Mar 2018 22:32:57 +0200 Subject: Add CSV support --- Makefile | 2 +- README.md | 38 +++++++++-- cmd/jp/csv.go | 29 ++++++++ cmd/jp/main.go | 35 ++++++++-- examples/sin.csv | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 291 insertions(+), 13 deletions(-) create mode 100644 cmd/jp/csv.go create mode 100644 examples/sin.csv diff --git a/Makefile b/Makefile index 88397cb..27c9814 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 1.1.1 +VERSION = 1.1.2 APP := jp PACKAGES := $(shell go list -f {{.Dir}} ./...) diff --git a/README.md b/README.md index 3713d42..6f8825b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # jp -Dead simple terminal plots from JSON data. Bar charts, line charts, and scatter plots are supported. +Dead simple terminal plots from JSON (or CSV) data. Bar charts, line charts, and scatter plots are supported. @@ -18,6 +18,7 @@ Dead simple terminal plots from JSON data. Bar charts, line charts, and scatter - [Array data, XY pairs](#array-data-xy-pairs) - [Y values only (X=index)](#y-values-only-xindex-1) - [Scatter plot](#scatter-plot) + - [CSV input](#csv-input) @@ -31,16 +32,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.1.1/jp_1.1.1_linux_x86_64.zip -unzip jp_1.1.1_linux_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.2/jp_1.1.2_linux_x86_64.zip +unzip jp_1.1.2_linux_x86_64.zip # OS X -curl -LO https://github.com/sgreben/jp/releases/download/1.1.1/jp_1.1.1_osx_x86_64.zip -unzip jp_1.1.1_osx_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.2/jp_1.1.2_osx_x86_64.zip +unzip jp_1.1.2_osx_x86_64.zip # Windows -curl -LO https://github.com/sgreben/jp/releases/download/1.1.1/jp_1.1.1_windows_x86_64.zip -unzip jp_1.1.1_windows_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.2/jp_1.1.2_windows_x86_64.zip +unzip jp_1.1.2_windows_x86_64.zip ``` ## Use it @@ -63,6 +64,8 @@ Usage of jp: Plot width (default 0 (auto)) -canvas value Canvas type. One of [full quarter braille auto] (default auto) + -input value + Input type. One of [json csv] (default json) ``` ## Examples @@ -278,4 +281,25 @@ $ cat examples/mvrnorm.json | jp -xy '..[x,y]' -type scatter │ ⠈ -4.271874└───────────────────────────────────────────────────────────────────── -4.08815 3.79083 +``` + +### CSV input + +``` +$ cat examples/sin.csv | jp -input csv -xy '[*][0,1]' + + 1.059955│ ▗▄▛▀▀▚▄▖ ▄▄▀▀▀▄▄ + │ ▗▞▘ ▝▚▖ ▄▀ ▝▀▄ + │ ▟▘ ▝▄ ▗▀ ▝▀▖ + │ ▗▛ ▚▖ ▞▘ ▝▙ + │ ▄▘ ▀▖ ▞ ▚ + │▞▘ ▝▌ ▗▛ ▚▖ + │ ▝▚ ▐▘ ▝▄ + │ ▜▖ ▟▘ ▝▄ + │ ▐▄ ▗▞ ▝▚ + │ ▚▖ ▄▀ + │ ▀▙▖ ▄▛ + │ ▀▀▄▄▄▞▀▘ + -1.059955└───────────────────────────────────────────────────────────────────── + 0 9.95 ``` \ No newline at end of file diff --git a/cmd/jp/csv.go b/cmd/jp/csv.go new file mode 100644 index 0000000..08ce65c --- /dev/null +++ b/cmd/jp/csv.go @@ -0,0 +1,29 @@ +package main + +import "strconv" + +func parseCell(cell string) interface{} { + f, err := strconv.ParseFloat(cell, 64) + if err == nil { + return f + } + b, err := strconv.ParseBool(cell) + if err == nil { + if b { + return 1 + } + return 0 + } + return cell +} + +func parseRows(rows [][]string) (out [][]interface{}) { + out = make([][]interface{}, len(rows)) + for i, row := range rows { + out[i] = make([]interface{}, len(row)) + for j, cell := range row { + out[i][j] = parseCell(cell) + } + } + return +} diff --git a/cmd/jp/main.go b/cmd/jp/main.go index 07de252..62f3db0 100644 --- a/cmd/jp/main.go +++ b/cmd/jp/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/csv" "encoding/json" "flag" "fmt" @@ -21,6 +22,7 @@ type configuration struct { XY string PlotType enumVar CanvasType enumVar + InputType enumVar } const ( @@ -37,6 +39,11 @@ const ( canvasTypeAuto = "auto" ) +const ( + inputTypeCSV = "csv" + inputTypeJSON = "json" +) + var config = configuration{ PlotType: enumVar{ Value: plotTypeLine, @@ -55,6 +62,13 @@ var config = configuration{ canvasTypeAuto, }, }, + InputType: enumVar{ + Value: inputTypeJSON, + Choices: []string{ + inputTypeJSON, + inputTypeCSV, + }, + }, } var ( @@ -66,6 +80,7 @@ var ( 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.Var(&config.InputType, "input", fmt.Sprintf("Input type. One of %v", config.InputType.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.") @@ -113,12 +128,21 @@ func match(in interface{}, p *jsonpath.JSONPath) [][]reflect.Value { func main() { var in interface{} - dec := json.NewDecoder(os.Stdin) - err := dec.Decode(&in) - if err != nil { - log.Println(err) + switch config.InputType.Value { + case inputTypeJSON: + dec := json.NewDecoder(os.Stdin) + err := dec.Decode(&in) + if err != nil { + log.Println(err) + } + case inputTypeCSV: + r := csv.NewReader(os.Stdin) + rows, err := r.ReadAll() + if err != nil { + log.Println(err) + } + in = parseRows(rows) } - fmt.Println() var x, y [][]reflect.Value if xyPattern != nil { x, y = split(match(in, xyPattern)) @@ -138,6 +162,7 @@ func main() { } p.Clear() c := draw.Canvas{Pixels: p} + fmt.Println() switch config.PlotType.Value { case plotTypeLine: fmt.Println(linePlot(x, y, c)) diff --git a/examples/sin.csv b/examples/sin.csv new file mode 100644 index 0000000..ac4e21c --- /dev/null +++ b/examples/sin.csv @@ -0,0 +1,200 @@ +0,0 +0.05,0.04997916927067833 +0.1,0.09983341664682815 +0.15,0.14943813247359922 +0.2,0.19866933079506122 +0.25,0.24740395925452294 +0.3,0.29552020666133955 +0.35,0.34289780745545134 +0.4,0.3894183423086505 +0.45,0.43496553411123023 +0.5,0.479425538604203 +0.55,0.5226872289306592 +0.6,0.5646424733950354 +0.65,0.6051864057360395 +0.7,0.644217687237691 +0.75,0.6816387600233342 +0.8,0.7173560908995228 +0.85,0.7512804051402926 +0.9,0.7833269096274833 +0.95,0.8134155047893737 +1,0.8414709848078965 +1.05,0.8674232255940169 +1.1,0.8912073600614354 +1.15,0.912763940260521 +1.2,0.9320390859672263 +1.25,0.9489846193555862 +1.3,0.963558185417193 +1.35,0.9757233578266591 +1.4,0.9854497299884601 +1.45,0.9927129910375885 +1.5,0.9974949866040544 +1.55,0.999783764189357 +1.6,0.9995736030415051 +1.65,0.9968650284539189 +1.7,0.9916648104524686 +1.75,0.9839859468739369 +1.8,0.9738476308781951 +1.85,0.9612752029752999 +1.9,0.9463000876874145 +1.95,0.9289597150038693 +2,0.9092974268256817 +2.05,0.8873623686333755 +2.1,0.8632093666488738 +2.15,0.8368987907984978 +2.2,0.8084964038195901 +2.25,0.7780731968879213 +2.3,0.7457052121767203 +2.35,0.7114733527908443 +2.4,0.675463180551151 +2.45,0.6377647021345036 +2.5,0.5984721441039564 +2.55,0.557683717391417 +2.6,0.5155013718214642 +2.65,0.47203054128988264 +2.7,0.4273798802338298 +2.75,0.38166099205233167 +2.8,0.33498815015590505 +2.85,0.2874780123425444 +2.9,0.2392493292139824 +2.95,0.19042264736102704 +3,0.1411200080598672 +3.05,0.0914646422324372 +3.1,0.04158066243329049 +3.15,-0.008407247367148617 +3.2,-0.058374143427580086 +3.25,-0.10819513453010837 +3.3,-0.1577456941432482 +3.35,-0.20690197167339977 +3.4,-0.2555411020268312 +3.45,-0.30354151270842933 +3.5,-0.35078322768961984 +3.55,-0.3971481672859598 +3.6,-0.44252044329485246 +3.65,-0.4867866486556994 +3.7,-0.5298361409084934 +3.75,-0.5715613187423438 +3.8,-0.6118578909427189 +3.85,-0.6506251370651673 +3.9,-0.6877661591839738 +3.95,-0.7231881240865121 +4,-0.7568024953079282 +4.05,-0.7885252544261949 +4.1,-0.8182771110644103 +4.15,-0.8459837010754465 +4.2,-0.8715757724135881 +4.25,-0.8949893582285835 +4.3,-0.9161659367494549 +4.35,-0.9350525775584491 +4.4,-0.9516020738895161 +4.45,-0.9657730606206388 +4.5,-0.977530117665097 +4.55,-0.9868438585032365 +4.6,-0.9936910036334644 +4.65,-0.9980544387588794 +4.7,-0.9999232575641008 +4.75,-0.999292788975378 +4.8,-0.9961646088358407 +4.85,-0.9905465359667133 +4.9,-0.9824526126243325 +4.95,-0.9719030694018208 +5,-0.9589242746631385 +5.05,-0.9435486686359066 +5.1,-0.9258146823277325 +5.15,-0.9057666414687044 +5.2,-0.8834546557201531 +5.25,-0.8589344934265921 +5.3,-0.8322674422239013 +5.35,-0.8035201558521559 +5.4,-0.7727644875559871 +5.45,-0.7400773104888945 +5.5,-0.7055403255703919 +5.55,-0.669239857276262 +5.6,-0.6312666378723216 +5.65,-0.5917155806310094 +5.7,-0.5506855425976376 +5.75,-0.5082790774992583 +5.8,-0.46460217941375737 +5.85,-0.41976401783985967 +5.9,-0.373876664830236 +5.95,-0.32705481486974064 +6,-0.27941549819892586 +6.05,-0.23107778829939224 +6.1,-0.1821625042720959 +6.15,-0.13279190885251674 +6.2,-0.08308940281749641 +6.25,-0.03317921654755682 +6.3,0.016813900484349713 +6.35,0.06676499152155546 +6.4,0.11654920485049364 +6.45,0.1660421058649572 +6.5,0.21511998808781552 +6.55,0.2636601823727784 +6.6,0.31154136351337786 +6.65,0.35864385349280037 +6.7,0.40484992061659836 +6.75,0.4500440737806176 +6.8,0.4941133511386082 +6.85,0.536947602448011 +6.9,0.5784397643882002 +6.95,0.618486128163024 +7,0.6569865987187891 +7.05,0.6938449449297637 +7.1,0.7289690401258759 +7.15,0.7622710923614112 +7.2,0.7936678638491531 +7.25,0.8230808790115054 +7.3,0.8504366206285644 +7.35,0.8756667135928822 +7.4,0.8987080958116269 +7.45,0.9195031758289707 +7.5,0.9379999767747389 +7.55,0.9541522662795148 +7.6,0.9679196720314863 +7.65,0.9792677826862 +7.7,0.9881682338770004 +7.75,0.9945987791111761 +7.8,0.998543345374605 +7.85,0.9999920733059188 +7.9,0.998941341839772 +7.95,0.9953937772576199 +8,0.9893582466233818 +8.05,0.9808498356203995 +8.1,0.9698898108450863 +8.15,0.9565055666515091 +8.2,0.9407305566797731 +8.25,0.9226042102393402 +8.3,0.9021718337562934 +8.35,0.8794844975308649 +8.4,0.8545989080882805 +8.45,0.827577266441984 +8.5,0.7984871126234903 +8.55,0.7674011568674873 +8.6,0.7343970978741134 +8.65,0.699557428602668 +8.7,0.6629692300821833 +8.75,0.6247239537541924 +8.8,0.5849171928917617 +8.85,0.5436484436660883 +8.9,0.5010208564578846 +8.95,0.45714097803515585 +9,0.4121184852417566 +9.05,0.366065910862411 +9.1,0.3190983623493521 +9.15,0.27133323411363275 +9.2,0.22288991410024764 +9.25,0.17388948538043356 +9.3,0.12445442350706169 +9.35,0.07470829038953478 +9.4,0.024775425453357765 +9.45,-0.02521936514365872 +9.5,-0.07515112046180931 +9.55,-0.12489503711675232 +9.6,-0.17432678122297965 +9.65,-0.2233227991637839 +9.7,-0.2717606264109424 +9.75,-0.3195191936222736 +9.8,-0.3664791292519284 +9.85,-0.41252305791709265 +9.9,-0.45753589377532133 +9.95,-0.5014051281791975 -- cgit v1.2.3