Skip to content

Commit

Permalink
cli: add optional utf-8 graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco149 committed Jan 22, 2018
1 parent 867d6bc commit 895678b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,30 @@ internalfn
void asciiplt(float (* getvalue)(void* data, size_t i), size_t n,
void* data)
{
static char const* charset = " _.-^";
static char const* charset[] = {
#ifdef OPPAI_UTF8GRAPH
"\xe2\x96\x81",
"\xe2\x96\x82",
"\xe2\x96\x83",
"\xe2\x96\x84",
"\xe2\x96\x85",
"\xe2\x96\x86",
"\xe2\x96\x87",
"\xe2\x96\x88"
#else
" ", "_", ".", "-", "^"
#endif
};

static size_t const charsetsize =
sizeof(charset) / sizeof(charset[0]);

float values[ASCIIPLT_W];
float minval = get_inf();
float maxval = -get_inf();
float range;
size_t i;
size_t chunksize;
size_t charsetsize;
size_t w = mymin(ASCIIPLT_W, n);

memset(values, 0, sizeof(values));
Expand All @@ -217,15 +233,14 @@ void asciiplt(float (* getvalue)(void* data, size_t i), size_t n,
}

range = mymax(0.00001, maxval - minval);
charsetsize = strlen(charset);

for (i = 0; i < w; ++i)
{
size_t chari = (size_t)(
((values[i] - minval) / range) * charsetsize
);
chari = mymax(0, mymin(chari, charsetsize - 1));
printf("%c", charset[chari]);
printf("%s", charset[chari]);
}

puts("");
Expand Down
2 changes: 1 addition & 1 deletion oppai.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

#define OPPAI_VERSION_MAJOR 1
#define OPPAI_VERSION_MINOR 1
#define OPPAI_VERSION_PATCH 43
#define OPPAI_VERSION_PATCH 44

/* if your compiler doesn't have stdint, define this */
#ifdef OPPAI_NOSTDINT
Expand Down

0 comments on commit 895678b

Please sign in to comment.