💾 Archived View for gmi.noulin.net › gitRepositories › ervy › file › ribbon.c.gmi captured on 2024-07-09 at 00:17:16. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
ribbon.c (1888B)
1 2 #include "ribbon.h" 3 #include "math.h" 4 #include "limits.h" 5 6 char *ribbon(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: 60,\ 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 f64 sum = 0; 43 44 range(i, lenG(&values)) { 45 sum += getNumG(&values, i); 46 } 47 var maxKeyLength = maxKeyLen(dat); 48 var valLength = lenG(dat); 49 50 range(wd, barWidth) { 51 range(i, valLength) { 52 var tmp = getG(dat, rtSmallDictt, i); 53 var ratioLength = round(width * getNumG(tmp, "value") / sum); 54 55 var padChar = style; 56 if (hasG(tmp, "style")) padChar = getG(tmp, rtChar, "style"); 57 58 var pads = repeatS(padChar, ratioLength); 59 60 iAppendS(&result, pads); 61 62 free(pads); 63 64 finishG(tmp); 65 } 66 67 var eol = repeatS(EOL, padding); 68 var pad = repeatS(PAD, left); 69 iAppendManyS(&result, eol, pad); 70 freeManyS(eol,pad); 71 } 72 73 range(i, valLength) { 74 var tmp = getG(dat, rtSmallDictt, i); 75 var ratioLength = round(width * getNumG(tmp, "value") / sum); 76 77 var k = catS(PAD, getG(tmp, rtChar, "key")); 78 iPadEndS(&k, ratioLength, PAD); 79 80 iAppendS(&result, k); 81 82 free(k); 83 84 finishG(tmp); 85 } 86 87 freeManyG(&newOpts, &values); 88 terminateG(dat); 89 90 ret result; 91 } 92 // vim: set expandtab ts=2 sw=2: