add color
This commit is contained in:
parent
323666f128
commit
b168ae2927
59
src/main.c
59
src/main.c
@ -48,7 +48,7 @@ int smaller_of(int a, int b) {
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
void win_int(WINDOW *lwin, int data, int digits) {
|
||||
void win_int(WINDOW *lwin, int data, int digits, int use_color, int color) {
|
||||
int lwiny, lwinx, len;
|
||||
char buf[16];
|
||||
getmaxyx(lwin, lwiny, lwinx);
|
||||
@ -67,6 +67,8 @@ void win_int(WINDOW *lwin, int data, int digits) {
|
||||
|
||||
win_clear(lwin);
|
||||
|
||||
if (use_color) wattron(lwin, COLOR_PAIR(color));
|
||||
|
||||
for (int d = 0; d < len; d++) {
|
||||
int digit = buf[d] - '0';
|
||||
int dx = startx + d * (bw * size + 1) + offset;
|
||||
@ -85,10 +87,12 @@ void win_int(WINDOW *lwin, int data, int digits) {
|
||||
}
|
||||
}
|
||||
|
||||
if (use_color) wattroff(lwin, COLOR_PAIR(color));
|
||||
|
||||
wrefresh(lwin);
|
||||
}
|
||||
|
||||
void win_float(WINDOW *lwin, float data, int digits) {
|
||||
void win_float(WINDOW *lwin, float data, int digits, int use_color, int color) {
|
||||
int lwiny, lwinx, len;
|
||||
char buf[16];
|
||||
|
||||
@ -109,6 +113,8 @@ void win_float(WINDOW *lwin, float data, int digits) {
|
||||
|
||||
win_clear(lwin);
|
||||
|
||||
if (use_color) wattron(lwin, COLOR_PAIR(color));
|
||||
|
||||
for (int d = 0; d < len; d++) {
|
||||
int dx = startx + d * (bw * size + 1) + offset;
|
||||
|
||||
@ -140,15 +146,19 @@ void win_float(WINDOW *lwin, float data, int digits) {
|
||||
}
|
||||
}
|
||||
|
||||
if (use_color) wattroff(lwin, COLOR_PAIR(color));
|
||||
|
||||
wrefresh(lwin);
|
||||
}
|
||||
|
||||
void win_bar(WINDOW *lwin, int data, int data_max) {
|
||||
void win_bar(WINDOW *lwin, int data, int data_max, int use_color, int color) {
|
||||
int lwiny, lwinx;
|
||||
getmaxyx(lwin, lwiny, lwinx);
|
||||
|
||||
win_clear(lwin);
|
||||
|
||||
if (use_color) wattron(lwin, COLOR_PAIR(color));
|
||||
|
||||
int bar_width = lwinx - 4;
|
||||
int bar_height = lwiny - 2;
|
||||
int filled = (data * bar_width) / data_max;
|
||||
@ -164,9 +174,24 @@ void win_bar(WINDOW *lwin, int data, int data_max) {
|
||||
snprintf(buf, sizeof(buf), "%d rpm", data);
|
||||
mvwprintw(lwin, lwiny / 2 - 1, 3, "%s", buf);
|
||||
|
||||
if (use_color) wattroff(lwin, COLOR_PAIR(color));
|
||||
|
||||
wrefresh(lwin);
|
||||
}
|
||||
|
||||
int color_high(float data, float mdata) {
|
||||
int percent = (int)((100.0f * data) / mdata + 0.5f);
|
||||
if (percent >= 90) return 1;
|
||||
else if (percent >= 70) return 2;
|
||||
else return 3;
|
||||
}
|
||||
|
||||
short color_low(float data, float mdata) {
|
||||
int percent = 100 - (int)((100.0f * data) / mdata + 0.5f);
|
||||
if (percent >= 90) return 1;
|
||||
else if (percent >= 70) return 2;
|
||||
else return 3;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int option;
|
||||
@ -203,6 +228,16 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int use_color;
|
||||
if (has_colors()) {
|
||||
use_color = 1;
|
||||
start_color();
|
||||
use_default_colors();
|
||||
init_pair(1, COLOR_RED, -1);
|
||||
init_pair(2, COLOR_YELLOW, -1);
|
||||
init_pair(3, COLOR_GREEN, -1);
|
||||
}
|
||||
|
||||
WINDOW *win[10];
|
||||
|
||||
int x3 = x / 3;
|
||||
@ -256,18 +291,18 @@ int main(int argc, char **argv) {
|
||||
long now = now_ms();
|
||||
|
||||
if (now - t100 >= 100 * delay) {
|
||||
win_int(win[0], speed, 3);
|
||||
win_int(win[1], power, 4);
|
||||
win_float(win[3], tq, 3);
|
||||
win_bar(win[4], rpm, 6000);
|
||||
win_float(win[5], eff, 5);
|
||||
win_int(win[0], speed, 3, use_color, color_high(speed, mspeed));
|
||||
win_int(win[1], power, 4, use_color, color_high(power, mpower));
|
||||
win_float(win[3], tq, 3, use_color, color_high(tq, mtq));
|
||||
win_bar(win[4], rpm, 6000, use_color, color_high(rpm, mrpm));
|
||||
win_float(win[5], eff, 5, use_color, color_low(eff, meff));
|
||||
t100 = now;
|
||||
}
|
||||
if (now - t1000 >= 1000 * delay) {
|
||||
win_int(win[2], bat, 3);
|
||||
win_float(win[6], bat_temp, 5);
|
||||
win_float(win[7], var_temp, 5);
|
||||
win_float(win[8], mot_temp, 5);
|
||||
win_int(win[2], bat, 3, use_color, color_low(bat, mbat));
|
||||
win_float(win[6], bat_temp, 5, use_color, color_high(bat_temp, mbat_temp));
|
||||
win_float(win[7], var_temp, 5, use_color, color_high(var_temp, mvar_temp));
|
||||
win_float(win[8], mot_temp, 5, use_color, color_high(mot_temp, mmot_temp));
|
||||
t1000 = now;
|
||||
}
|
||||
//win[9];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user