ploot

plot images or real-time plain text from a .csv file
Log | Files | Refs | README

commit 5d691aee8b20c7148c284636e3e897fc1803a562
parent ecbd21a496446ea895412dc2c98c803173828520
Author: Josuah Demangeon <mail@josuah.net>
Date:   Tue, 26 Jun 2018 18:45:53 +0200

start refactoring the project into a library

Diffstat:
Alibploot/ploot_braille.3 | 45+++++++++++++++++++++++++++++++++++++++++++++
Alibploot/ploot_braille.c | 5+++++
Alibploot/ploot_humanize.c | 43+++++++++++++++++++++++++++++++++++++++++++
3 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/libploot/ploot_braille.3 b/libploot/ploot_braille.3 @@ -0,0 +1,45 @@ +.Dd $Mdocdate: June 25 2018$ +.Dt PLOOT_BRAILLE 3 +.Os +. +. +.Sh NAME +. +.Nm plt_braille +.Nd plot a plain UTF-8 text chart in a buffer +. +. +.Sh SYNOPSIS +. +.In libplt.h +.Ft void +.Fn plt_braille "time_t tt" "double *vv" "size_t n" "char *buf" "size_t w" "size_t h" +. +. +.Sh DESCRIPTION +. +The +.Nm +function draws a text plot +of the +last values contained in the array +.Fa "vv" +of size +.Fa "n" +into buffer +.Fa "buf" +of width +.Fa "w" +and height +.Fa "h" +with a scale on the left. +. +. +.Sh SEE ALSO +.\" .Xr foobar 1 +. +. +.Sh AUTHORS +. +.An Josuah Demangeon +.Aq Mt mail@josuah.net diff --git a/libploot/ploot_braille.c b/libploot/ploot_braille.c @@ -0,0 +1,5 @@ +void +ploot_braille(double *values, int n, char *buf, int width, int height) +{ + memset(buf, ' ', width * height); +} diff --git a/libploot/ploot_humanize.c b/libploot/ploot_humanize.c @@ -0,0 +1,43 @@ +#include "ploot.h" + +#define ABS(x) ((x) > 0 ? (x) : -(x)) + +/* + * Convert a big or small number to a number between 1.0 and 1000.0 + * along with a S.I. suffix. If you prefer ISO 80000 suffix you can + * replace 1000 by 1024. + */ +static char +sys_int_suf(double *val) +{ + char *big = "KMGTPEZY", *small = "mun"; + + if (*val < 1.0) { + for (; *val < 1.0 && small[1] != '\0'; *val *= 1000, small++); + return *small; + } else { + for (; *val > 1000 && big[1] != '\0'; *val /= 1000, big++); + return *big; + } +} + +/* + * Set 'str' to a human-readable form of 'num' with always a width of 8 (+ 1 + * the '\0' terminator). Buffer overflow is ensured not to happen due to the + * max size of a double. Return the exponent. + */ +int +ploot_humanize(char *str, double val) +{ + int exp, precision; + + precision = (ABS(val) < 10) ? 2 : (ABS(val) < 100) ? 1 : 0; + precision += (exp == 0); + + snprintf(str, 9, "%+.*f %c", precision, val, label[exp]); + str[8] = '\0'; + if (val >= 0) + str[0] = ' '; + + return exp * 3; +}