Merge pull request #134 from chelovechishko/terminal_ncurses

terminal_ncurses: refactoring
master
karl 9 years ago committed by GitHub
commit d51e1f5777
  1. 193
      output/terminal_ncurses.c
  2. 10
      output/terminal_ncurses.h

@ -1,47 +1,48 @@
#include <curses.h>
#include <locale.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct cols {
int col;
int R;
int G;
int B;
struct colors {
NCURSES_COLOR_T color;
NCURSES_COLOR_T R;
NCURSES_COLOR_T G;
NCURSES_COLOR_T B;
};
#define DEFAULTCOL 6
#define DEFAULTBGCOL -1
#define COLOR_REDEFINITION -2
struct cols parse_color(char *col) {
struct cols ret;
if (col[0] == '#') {
if (can_change_color() != TRUE) {
static void parse_color(char* color_string, struct colors* color) {
if (color_string[0] == '#') {
if (!can_change_color()) {
cleanup_terminal_ncurses();
fprintf(stderr,
"Your terminal can not change color definitions, please use one of the predefined colors.\n");
fprintf(stderr, "Your terminal can not change color definitions, "
"please use one of the predefined colors.\n");
exit(EXIT_FAILURE);
}
color->color = COLOR_REDEFINITION;
sscanf(++color_string, "%02hx%02hx%02hx", &color->R, &color->G, &color->B);
}
}
// Set to -2 to indicate color_redefinition
ret.col = -2;
++col;
sscanf(col, "%02x%02x%02x", &ret.R, &ret.G, &ret.B);
return ret;
} else {
//using predefined colors
ret.col = 0;
return ret;
// ncurses use color range [0, 1000], and we - [0, 255]
#define CURSES_COLOR_COEFFICIENT( X ) (( X ) * 1000.0 / 0xFF + 0.5)
#define COLORS_STRUCT_NORMALIZE( X ) CURSES_COLOR_COEFFICIENT( X.R ), \
CURSES_COLOR_COEFFICIENT( X.G ), CURSES_COLOR_COEFFICIENT( X.B )
static NCURSES_COLOR_T change_color_definition(NCURSES_COLOR_T color_number,
char* const color_string, NCURSES_COLOR_T predef_color) {
struct colors color = { 0 };
parse_color(color_string, &color);
NCURSES_COLOR_T return_color_number = predef_color;
if (color.color == COLOR_REDEFINITION) {
init_color(color_number, COLORS_STRUCT_NORMALIZE(color));
return_color_number = color_number;
}
return return_color_number;
}
int init_terminal_ncurses(char *color, char *bcolor, int predefcol, int predefbgcol) {
struct cols col, bgcol;
void init_terminal_ncurses(char* const fg_color_string,
char* const bg_color_string, int predef_fg_color, int predef_bg_color) {
initscr();
curs_set(0);
timeout(0);
@ -49,97 +50,93 @@ int init_terminal_ncurses(char *color, char *bcolor, int predefcol, int predefbg
start_color();
use_default_colors();
double magic = 1000 / 255.0;
int colp = 0, bgcolp = 0;
NCURSES_COLOR_T fg_color_number;
fg_color_number = change_color_definition(1, fg_color_string, predef_fg_color);
col = parse_color(color);
bgcol = parse_color(bcolor);
if (col.col == -2) {
init_color(1, (int)(col.R * magic), (int)(col.G * magic), (int)(col.B * magic));
}
if (bgcol.col == -2) {
init_color(2, (int)(bgcol.R * magic), (int)(bgcol.G * magic), (int)(bgcol.B * magic));
}
NCURSES_COLOR_T bg_color_number;
bg_color_number = change_color_definition(2, bg_color_string, predef_bg_color);
switch (col.col) {
case -2:
colp = 1;
break;
case -1:
colp = DEFAULTCOL;
break;
default:
colp = predefcol;
}
switch (bgcol.col) {
case -2:
bgcolp = 2;
break;
case -1:
bgcolp = DEFAULTBGCOL;
break;
default:
bgcolp = predefbgcol;
}
NCURSES_COLOR_T color_pair_number = 1;
init_pair(color_pair_number, fg_color_number, bg_color_number);
init_pair(1, colp, bgcolp);
if (bgcolp != -1)
bkgd(COLOR_PAIR(1));
attron(COLOR_PAIR(1));
// attron(A_BOLD);
return 0;
if (bg_color_number != -1)
bkgd(COLOR_PAIR(color_pair_number));
attron(COLOR_PAIR(color_pair_number));
refresh();
}
void get_terminal_dim_ncurses(int *w, int *h) {
getmaxyx(stdscr,*h,*w);
clear(); //clearing in case of resieze
void get_terminal_dim_ncurses(int* width, int* height) {
getmaxyx(stdscr, *height, *width);
clear(); // clearing in case of resieze
}
#define TERMINAL_RESIZED -1
int draw_terminal_ncurses(int tty, int h, int w, int bars, int bw, int bs, int rest, int f[200], int flastd[200]) {
int i, n, q;
const wchar_t* bh[] = {L"\u2581", L"\u2582", L"\u2583", L"\u2584", L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width,
int bars_count, int bar_width, int bar_spacing, int rest, const int f[200],
int flastd[200]) {
const wchar_t* bar_heights[] = {L"\u2581", L"\u2582", L"\u2583",
L"\u2584", L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
#define LAST ((sizeof(bar_heights) / sizeof(bar_heights[0])) - 1)
// output: check if terminal has been resized
if (!tty) {
if ( LINES != h || COLS != w) {
return -1;
if (!is_tty) {
if (LINES != terminal_height || COLS != terminal_width) {
return TERMINAL_RESIZED;
}
}
h = h - 1;
for (i = 0; i < bars; i++) {
if(f[i] > flastd[i]){//higher then last one
if (tty) for (n = flastd[i] / 8; n < f[i] / 8; n++) for (q = 0; q < bw; q++) mvprintw((h - n), i*bw + q + i*bs + rest, "%d",8);
else for (n = flastd[i] / 8; n < f[i] / 8; n++) for (q = 0; q < bw; q++) mvaddwstr((h - n), i*bw + q + i*bs + rest, bh[7]);
if (f[i] % 8 != 0) {
if (tty) for (q = 0; q < bw; q++) mvprintw( (h - n), i*bw + q + i*bs + rest, "%d",(f[i] % 8) );
else for (q = 0; q < bw; q++) mvaddwstr( (h - n), i*bw + q + i*bs + rest, bh[(f[i] % 8) - 1]);
const int height = terminal_height - 1;
#define CURRENT_COLUMN bar*bar_width + width + bar*bar_spacing + rest
for (int bar = 0; bar < bars_count; bar++) {
if (f[bar] > flastd[bar]) { // higher then last one
if (is_tty) {
for (int n = flastd[bar] / 8; n < f[bar] / 8; n++)
for (int width = 0; width < bar_width; width++)
mvprintw((height - n), CURRENT_COLUMN, "%d", 8);
} else {
for (int n = flastd[bar] / 8; n < f[bar] / 8; n++)
for (int width = 0; width < bar_width; width++)
mvaddwstr((height - n), CURRENT_COLUMN,
bar_heights[LAST]);
}
}else if(f[i] < flastd[i]){//lower then last one
for (n = f[i] / 8; n < flastd[i]/8 + 1; n++) for (q = 0; q < bw; q++) mvaddstr( (h - n), i*bw + q + i*bs + rest, " ");
if (f[i] % 8 != 0) {
if (tty) for (q = 0; q < bw; q++) mvprintw((h - f[i] / 8), i*bw + q + i*bs + rest, "%d",(f[i] % 8) );
else for (q = 0; q < bw; q++) mvaddwstr((h - f[i] / 8), i*bw + q + i*bs + rest, bh[(f[i] % 8) - 1]);
if (f[bar] % 8) {
if (is_tty) {
for (int width = 0; width < bar_width; width++)
mvprintw((height - f[bar] / 8), CURRENT_COLUMN, "%d",
(f[bar] % 8));
} else {
for (int width = 0; width < bar_width; width++)
mvaddwstr((height - f[bar] / 8), CURRENT_COLUMN,
bar_heights[(f[bar] % 8) - 1]);
}
}
} else if(f[bar] < flastd[bar]) { // lower then last one
for (int n = f[bar] / 8; n < flastd[bar]/8 + 1; n++)
for (int width = 0; width < bar_width; width++)
mvaddstr((height - n), CURRENT_COLUMN, " ");
if (f[bar] % 8) {
if (is_tty) {
for (int width = 0; width < bar_width; width++)
mvprintw((height - f[bar] / 8), CURRENT_COLUMN, "%d",
(f[bar] % 8));
} else {
for (int width = 0; width < bar_width; width++)
mvaddwstr((height - f[bar] / 8), CURRENT_COLUMN,
bar_heights[(f[bar] % 8) - 1]);
}
}
}
flastd[i] = f[i]; //memmory for falloff func
#undef CURRENT_COLUMN
flastd[bar] = f[bar]; // memory for falloff func
}
refresh();
return 0;
}
// general: cleanup
void cleanup_terminal_ncurses(void)
{
void cleanup_terminal_ncurses(void) {
echo();
system("setfont >/dev/null 2>&1");
system("setfont /usr/share/consolefonts/Lat2-Fixed16.psf.gz >/dev/null 2>&1");

@ -1,5 +1,7 @@
int init_terminal_ncurses(char *color, char *bcolor, int predefcol, int predefbgcol);
void get_terminal_dim_ncurses(int *w, int *h);
int draw_terminal_ncurses(int virt, int height, int width, int bars, int bw, int bs, int rest, int f[200], int flastd[200]);
void init_terminal_ncurses(char* const fg_color_string,
char* const bg_color_string, int predef_fg_color, int predef_bg_color);
void get_terminal_dim_ncurses(int* width, int* height);
int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width,
int bars_count, int bar_width, int bar_spacing, int rest, const int f[200],
int flastd[200]);
void cleanup_terminal_ncurses(void);

Loading…
Cancel
Save