sds是一个基础的数据的结构,这两个函数没有看得很透彻,先记录学习一下,后面调到了,再具体看看:
- /* Render part of a sequence, so that render_sequence() call call this function
- * with different parts in order to create the full output without overflowing
- * the current terminal columns. */
- sds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) {
- int j;
- double relmax = seq->max - seq->min;
- int steps = charset_len*rows;
- int row = 0;
- char *chars = zmalloc(len);
- int loop = 1;
- int opt_fill = flags & SPARKLINE_FILL;
- int opt_log = flags & SPARKLINE_LOG_SCALE;
-
- if (opt_log) {
- relmax = log(relmax+1);
- } else if (relmax == 0) {
- relmax = 1;
- }
-
- while(loop) {
- loop = 0;
- memset(chars,' ',len);
- for (j = 0; j < len; j++) {
- struct sample *s = &seq->samples[j+offset];
- double relval = s->value - seq->min;
- int step;
-
- if (opt_log) relval = log(relval+1);
- step = (int) (relval*steps)/relmax;
- if (step < 0) step = 0;
- if (step >= steps) step = steps-1;
-
- if (row < rows) {
- /* Print the character needed to create the sparkline */
- int charidx = step-((rows-row-1)*charset_len);
- loop = 1;
- if (charidx >= 0 && charidx < charset_len) {
- chars[j] = opt_fill ? charset_fill[charidx] :
- charset[charidx];
- } else if(opt_fill && charidx >= charset_len) {
- chars[j] = '|';
- }
- } else {
- /* Labels spacing */
- if (seq->labels && row-rows < label_margin_top) {
- loop = 1;
- break;
- }
- /* Print the label if needed. */
- if (s->label) {
- int label_len = strlen(s->label);
- int label_char = row - rows - label_margin_top;
-
- if (label_len > label_char) {
- loop = 1;
- chars[j] = s->label[label_char];
- }
- }
- }
- }
- if (loop) {
- row++;
- output = sdscatlen(output,chars,len);
- output = sdscatlen(output,"\n",1);
- }
- }
- zfree(chars);
- return output;
- }
-
- /* Turn a sequence into its ASCII representation */
- sds sparklineRender(sds output, struct sequence *seq, int columns, int rows, int flags) {
- int j;
-
- for (j = 0; j < seq->length; j += columns) {
- int sublen = (seq->length-j) < columns ? (seq->length-j) : columns;
-
- if (j != 0) output = sdscatlen(output,"\n",1);
- output = sparklineRenderRange(output, seq, rows, j, sublen, flags);
- }
- return output;
- }
-