spark.c (790B)
1#include <stdio.h> 2#include <limits.h> 3#include <string.h> 4#include <stdlib.h> 5#include "spark.h" 6#include "stat.h" 7 8#define SPARK_SHIFT 8 9 10/* Print spark lines on outf for numval values in val. */ 11int print_spark(char *bf, int size, unsigned long *val, int numval) 12{ 13 static const char *ticks[NUM_SPARKS] = { 14 "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" 15 }; 16 int i, printed = 0; 17 unsigned long min = ULONG_MAX, max = 0, f; 18 19 for (i = 0; i < numval; i++) { 20 if (val[i] < min) 21 min = val[i]; 22 if (val[i] > max) 23 max = val[i]; 24 } 25 f = ((max - min) << SPARK_SHIFT) / (NUM_SPARKS - 1); 26 if (f < 1) 27 f = 1; 28 for (i = 0; i < numval; i++) { 29 printed += scnprintf(bf + printed, size - printed, "%s", 30 ticks[((val[i] - min) << SPARK_SHIFT) / f]); 31 } 32 33 return printed; 34}