ervy

Bring charts to terminal.
git clone https://noulin.net/git/ervy.git
Log | Files | Refs | README | LICENSE

commit 00bc3eccb63fa8d9d6991043a964af8571f79c77
parent c14bc2fa1daf4c1254ae9669a76d0a0ca7a921d9
Author: Remy Noulin <loader2x@gmail.com>
Date:   Mon, 11 Feb 2019 21:27:42 +0100

add ribbon

README.md       |   3 ++
bullet.c        |   2 +-
ervy.h          |   1 +
example.c       |   9 ++++++
imgs/ribbon.png | Bin 0 -> 438 bytes
package.yml     |   2 +-
ribbon.c        |  89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ribbon.h        |   5 ++++
8 files changed, 109 insertions(+), 2 deletions(-)

Diffstat:
MREADME.md | 3+++
Mbullet.c | 2+-
Mervy.h | 1+
Mexample.c | 9+++++++++
Aimgs/ribbon.png | 0
Mpackage.yml | 2+-
Aribbon.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aribbon.h | 5+++++
8 files changed, 109 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md @@ -20,3 +20,6 @@ See `example.c` for an example. ### Bullet ![](/imgs/bullet.png) + +### Ribbon +![](/imgs/ribbon.png) diff --git a/bullet.c b/bullet.c @@ -1,5 +1,5 @@ -#include "bar.h" +#include "bullet.h" #include "math.h" #include "limits.h" diff --git a/ervy.h b/ervy.h @@ -2,3 +2,4 @@ #include "bar.h" #include "bullet.h" +#include "ribbon.h" diff --git a/example.c b/example.c @@ -55,5 +55,14 @@ int main(int ARGC, char** ARGV) { puts(bullet(bulletData, NULL)); + // Ribbon + char *ribbonData = "[\ + { key: 'A', value: 5, style: '"BGWHT " "RST"' },\ + { key: 'B', value: 10, style: '"BGYLW " "RST"' },\ + { key: 'C', value: 10, style: '"BGMGT " "RST"' },\ + { key: 'D', value: 10, style: '"BGRED " "RST"' }\ + ]"; + + puts(ribbon(ribbonData, NULL)); } // vim: set expandtab ts=2 sw=2: diff --git a/imgs/ribbon.png b/imgs/ribbon.png Binary files differ. diff --git a/package.yml b/package.yml @@ -1,6 +1,6 @@ --- name: ervy - version: 0.0.5 + version: 0.0.6 description: "bring chart to terminal" bin: ./ervy.c #cflags: -DA -ggdb -std=gnu11 -fPIC -pipe diff --git a/ribbon.c b/ribbon.c @@ -0,0 +1,89 @@ + +#include "ribbon.h" +#include "math.h" +#include "limits.h" + +char *ribbon(char *data, char *opts) { + var dat = verifyData(data); + if (!dat) ret NULL; + + createSmallJson(newOpts); + parseG(&newOpts, "{\ + left: 1,\ + width: 60,\ + padding: 1,\ + style: '*'\ + }"); + + createAllocateSmallJson(oopts); + if (opts) { + parseG(oopts, opts); + } + + mergeNSmashG(&newOpts, oopts); + + var barWidth = getG(&newOpts, rtI64, "barWidth"); + var left = getG(&newOpts, rtI64, "left"); + var width = getG(&newOpts, rtI64, "width"); + var padding = getG(&newOpts, rtI64, "padding"); + var style = getG(&newOpts, rtChar, "style"); + + var result = repeatS(PAD, left); + + + createSmallArray(values); + + iter(dat, V) { + cast(smallDictt*, v, V); + pushG(&values, getNumG(v, "value")); + } + + f64 sum = 0; + + range(i, lenG(&values)) { + sum += getNumG(&values, i); + } + var maxKeyLength = maxKeyLen(dat); + var valLength = lenG(dat); + + range(i, valLength) { + var tmp = getG(dat, rtSmallDictt, i); + var ratioLength = round(width * getNumG(tmp, "value") / sum); + + var padChar = style; + if (hasG(tmp, "style")) padChar = getG(tmp, rtChar, "style"); + + var pads = repeatS(padChar, ratioLength); + + iAppendS(&result, pads); + + free(pads); + + finishG(tmp); + } + + var eol = repeatS(EOL, padding); + var pad = repeatS(PAD, left); + iAppendManyS(&result, eol, pad); + freeManyS(eol,pad); + + range(i, valLength) { + var tmp = getG(dat, rtSmallDictt, i); + var ratioLength = round(width * getNumG(tmp, "value") / sum); + + var k = catS(PAD, getG(tmp, rtChar, "key")); + iPadEndS(&k, ratioLength, PAD); + + iAppendS(&result, k); + + free(k); + + finishG(tmp); + } + + freeManyG(&newOpts, &values); + terminateG(dat); + + ret result; +} +// vim: set expandtab ts=2 sw=2: diff --git a/ribbon.h b/ribbon.h @@ -0,0 +1,5 @@ +#pragma once + +#include "utils.h" + +char *ribbon(char *data, char *opts);