Skip to content

Commit 4d361ea

Browse files
committed
add color support for plot line
1 parent a0bafef commit 4d361ea

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

ttyplot.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
#define STR_(x) #x
4646
#define STR(x) STR_(x)
4747
#define VERSION_MAJOR 1
48-
#define VERSION_MINOR 6
49-
#define VERSION_PATCH 5
48+
#define VERSION_MINOR 7
49+
#define VERSION_PATCH 0
5050
#define VERSION_STR STR(VERSION_MAJOR) "." STR(VERSION_MINOR) "." STR(VERSION_PATCH)
5151

5252
#define T_RARR '>'
@@ -82,6 +82,7 @@ static int width = 0, height = 0, n = -1, v = 0, c = 0, rate = 0, two = 0,
8282
static bool fake_clock = false;
8383
static char *errstr = NULL;
8484
static bool redraw_needed = false;
85+
static int plot_color = -1; // -1 means no color specified
8586
static const char *verstring = "https://github.com/tenox7/ttyplot " VERSION_STR;
8687

8788
static void usage(void) {
@@ -106,6 +107,7 @@ static void usage(void) {
106107
"lower-limit of the plot scale is fixed\n"
107108
" -t title of the plot\n"
108109
" -u unit displayed beside vertical bar\n"
110+
" -C color set the color of the plot line (0-7 for basic colors)\n"
109111
" -v print the current version and exit\n"
110112
" -h print this help message and exit\n"
111113
"\n"
@@ -187,6 +189,15 @@ static void draw_line(int x, int ph, int l1, int l2, cchar_t *c1, cchar_t *c2,
187189
cchar_t c1r = *c1, c2r = *c2;
188190
c1r.attr |= A_REVERSE;
189191
c2r.attr |= A_REVERSE;
192+
193+
if (plot_color != -1) {
194+
c1->attr |= COLOR_PAIR(1);
195+
c2->attr |= COLOR_PAIR(1);
196+
c1r.attr |= COLOR_PAIR(1);
197+
c2r.attr |= COLOR_PAIR(1);
198+
space.attr |= COLOR_PAIR(1);
199+
}
200+
190201
if (l1 > l2) {
191202
mvvline_set(ph + 1 - l1, x, c1, l1 - l2);
192203
mvvline_set(ph + 1 - l2, x, &c2r, l2);
@@ -196,6 +207,14 @@ static void draw_line(int x, int ph, int l1, int l2, cchar_t *c1, cchar_t *c2,
196207
} else {
197208
mvvline_set(ph + 1 - l2, x, &c2r, l2);
198209
}
210+
211+
if (plot_color != -1) {
212+
c1->attr &= ~COLOR_PAIR(1);
213+
c2->attr &= ~COLOR_PAIR(1);
214+
c1r.attr &= ~COLOR_PAIR(1);
215+
c2r.attr &= ~COLOR_PAIR(1);
216+
space.attr &= ~COLOR_PAIR(1);
217+
}
199218
}
200219

201220
static void plot_values(int ph, int pw, double *v1, double *v2, double max, double min,
@@ -206,6 +225,9 @@ static void plot_values(int ph, int pw, double *v1, double *v2, double max, doub
206225
int x;
207226
int l1, l2;
208227

228+
if (plot_color != -1)
229+
attron(COLOR_PAIR(1));
230+
209231
for (x = first_col; x < first_col + pw; x++, i = (i + 1) % pw) {
210232
/* suppress drawing uninitialized entries */
211233
if (! v1 || isnan(v1[i]))
@@ -236,6 +258,9 @@ static void plot_values(int ph, int pw, double *v1, double *v2, double max, doub
236258
: pc,
237259
hce, lce);
238260
}
261+
262+
if (plot_color != -1)
263+
attroff(COLOR_PAIR(1));
239264
}
240265

241266
static void show_all_centered(const char *message) {
@@ -546,7 +571,7 @@ int main(int argc, char *argv[]) {
546571
int i;
547572
bool stdin_is_open = true;
548573
int cached_opterr;
549-
const char *optstring = "2rc:e:E:s:S:m:M:t:u:vh";
574+
const char *optstring = "2rc:e:E:s:S:m:M:t:u:vhC:";
550575
int show_ver;
551576
int show_usage;
552577

@@ -628,6 +653,9 @@ int main(int argc, char *argv[]) {
628653
case 'E':
629654
mbtowc(&min_errchar.chars[0], optarg, MB_CUR_MAX);
630655
break;
656+
case 'C':
657+
plot_color = atoi(optarg);
658+
break;
631659
case 's':
632660
softmax = atof(optarg);
633661
break;
@@ -663,6 +691,12 @@ int main(int argc, char *argv[]) {
663691
err(1, "pledge");
664692
#endif
665693

694+
if (plot_color != -1) {
695+
start_color();
696+
use_default_colors();
697+
init_pair(1, plot_color, -1); // -1 for default background
698+
}
699+
666700
gettimeofday(&now, NULL);
667701
noecho();
668702
curs_set(FALSE);

0 commit comments

Comments
 (0)