ervy

Log

Files

Refs

README

LICENSE

bullet.c (2167B)

     1 
     2 #include "bullet.h"
     3 #include "math.h"
     4 #include "limits.h"
     5 
     6 char *bullet(char *data, char *opts) {
     7   var dat = verifyData(data);
     8   if (!dat) ret NULL;
     9 
    10   createSmallJson(newOpts);
    11   parseG(&newOpts, "{\
    12     barWidth: 1,\
    13     left: 1,\
    14     width: 20,\
    15     padding: 1,\
    16     style: '*'\
    17     }");
    18 
    19   createAllocateSmallJson(oopts);
    20   if (opts) {
    21     parseG(oopts, opts);
    22   }
    23 
    24   mergeNSmashG(&newOpts, oopts);
    25 
    26   var barWidth = getG(&newOpts, rtI64, "barWidth");
    27   var left     = getG(&newOpts, rtI64, "left");
    28   var width    = getG(&newOpts, rtI64, "width");
    29   var padding  = getG(&newOpts, rtI64, "padding");
    30   var style    = getG(&newOpts, rtChar, "style");
    31 
    32   var result = repeatS(PAD, left);
    33 
    34 
    35   createSmallArray(values);
    36 
    37   iter(dat, V) {
    38     cast(smallDictt*, v, V);
    39     pushG(&values, getNumG(v, "value"));
    40   }
    41 
    42   var max = getG(&values, rtF64, 0);
    43 
    44   range(i, lenG(&values)) {
    45     max = maxV(max, getNumG(&values, i));
    46   }
    47   var maxKeyLength = maxKeyLen(dat);
    48   var valLength    = lenG(dat);
    49 
    50   range(i, valLength) {
    51     var tmp         = getG(dat, rtSmallDictt, i);
    52     var ratioLength = round(width * getNumG(tmp, "value") / max);
    53 
    54     var padChar = style;
    55     if (hasG(tmp, "style")) padChar = getG(tmp, rtChar, "style");
    56 
    57     var pads = repeatS(padChar, ratioLength);
    58     var pad  = repeatS(PAD, left);
    59     var line = catS(pads, EOL, pad);
    60     freeManyS(pads, pad);
    61 
    62     var key = getNDupG(tmp, rtChar, "key");
    63     iPadStartS(&key, maxKeyLength, PAD);
    64     iAppendManyS(&result, key, PAD);
    65     free(key);
    66 
    67     var curBarWidth = barWidth;
    68     if (hasG(tmp, "barWidth")) curBarWidth = getG(tmp, rtI64, "barWidth");
    69 
    70     range(j, curBarWidth) {
    71       if (j > 0) {
    72         var pad = repeatS(PAD, maxKeyLength + 1);
    73         iAppendManyS(&result, pad, line);
    74         free(pad);
    75       }
    76       else {
    77         iAppendS(&result, line);
    78       }
    79     }
    80 
    81     free(line);
    82 
    83     if (i != valLength - 1) {
    84       var eol = repeatS(EOL, padding);
    85       var pad = repeatS(PAD, left);
    86       iAppendManyS(&result, eol, pad);
    87       freeManyS(eol,pad);
    88     }
    89 
    90     finishG(tmp);
    91   }
    92 
    93 
    94   freeManyG(&newOpts, &values);
    95   terminateG(dat);
    96 
    97   ret result;
    98 }
    99 // vim: set expandtab ts=2 sw=2: