📄 ncurses.c
字号:
per_row = 8; } row_limit = (pairs_max + per_row - 1) / per_row; move(0, 0); (void) printw("There are %d color pairs and %d colors\n", pairs_max, COLORS); clrtobot(); (void) mvprintw(top + 1, 0, "%dx%d matrix of foreground/background colors, bold *%s*\n", row_limit, per_row, opt_bold ? "on" : "off"); /* show color names/numbers across the top */ for (i = 0; i < per_row; i++) show_color_name(top + 2, (i + 1) * width, i, opt_wide); /* show a grid of colors, with color names/ numbers on the left */ for (i = (base_row * per_row); i < pairs_max; i++) { int row = grid_top + (i / per_row) - base_row; int col = (i % per_row + 1) * width; int pair = i; if (row >= 0 && move(row, col) != ERR) { init_pair(pair, i % COLORS, i / COLORS); attron((attr_t) COLOR_PAIR(pair)); if (opt_bold) attron((attr_t) A_BOLD); if (opt_nums) { sprintf(numbered, "{%02X}", i); hello = numbered; } printw("%-*.*s", width, width, hello); attrset(A_NORMAL); if ((i % per_row) == 0 && (i % COLORS) == 0) { show_color_name(row, 0, i / COLORS, opt_wide); } ++shown; } else if (shown) { break; } } switch (c = wGetchar(stdscr)) { case 'b': opt_bold = FALSE; break; case 'B': opt_bold = TRUE; break; case 'n': opt_nums = FALSE; break; case 'N': opt_nums = TRUE; break; case ESCAPE: case 'q': done = TRUE; continue; case 'w': set_color_test(opt_wide, FALSE); break; case 'W': set_color_test(opt_wide, TRUE); break; case CTRL('p'): case KEY_UP: if (base_row <= 0) { beep(); } else { base_row -= 1; } break; case CTRL('n'): case KEY_DOWN: if (base_row + page_size >= row_limit) { beep(); } else { base_row += 1; } break; case CTRL('b'): case KEY_PREVIOUS: case KEY_PPAGE: if (base_row <= 0) { beep(); } else { base_row -= (page_size - 1); if (base_row < 0) base_row = 0; } break; case CTRL('f'): case KEY_NEXT: case KEY_NPAGE: if (base_row + page_size >= row_limit) { beep(); } else { base_row += page_size - 1; if (base_row + page_size >= row_limit) { base_row = row_limit - page_size - 1; } } break; case '?': if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) { box(helpwin, 0, 0); color_legend(helpwin); wGetchar(helpwin); delwin(helpwin); } break; default: beep(); continue; } } erase(); endwin();}#if USE_WIDEC_SUPPORT/* generate a color test pattern */static voidwide_color_test(void){ int c; int i; int top = 0, width; int base_row = 0; int grid_top = top + 3; int page_size = (LINES - grid_top); int pairs_max = COLOR_PAIRS; int row_limit; int per_row; char numbered[80]; const char *hello; bool done = FALSE; bool opt_bold = FALSE; bool opt_wide = FALSE; bool opt_nums = FALSE; WINDOW *helpwin; while (!done) { int shown = 0; /* this assumes an 80-column line */ if (opt_wide) { width = 4; hello = "Test"; per_row = (COLORS > 8) ? 16 : 8; } else { width = 8; hello = "Hello"; per_row = 8; } row_limit = (pairs_max + per_row - 1) / per_row; move(0, 0); (void) printw("There are %d color pairs and %d colors\n", pairs_max, COLORS); clrtobot(); (void) mvprintw(top + 1, 0, "%dx%d matrix of foreground/background colors, bold *%s*\n", row_limit, per_row, opt_bold ? "on" : "off"); /* show color names/numbers across the top */ for (i = 0; i < per_row; i++) show_color_name(top + 2, (i + 1) * width, i, opt_wide); /* show a grid of colors, with color names/ numbers on the left */ for (i = (base_row * per_row); i < pairs_max; i++) { int row = grid_top + (i / per_row) - base_row; int col = (i % per_row + 1) * width; int pair = i; if (row >= 0 && move(row, col) != ERR) { init_pair(pair, i % COLORS, i / COLORS); color_set(pair, NULL); if (opt_bold) attr_on((attr_t) A_BOLD, NULL); if (opt_nums) { sprintf(numbered, "{%02X}", i); hello = numbered; } printw("%-*.*s", width, width, hello); attr_set(A_NORMAL, 0, NULL); if ((i % per_row) == 0 && (i % COLORS) == 0) { show_color_name(row, 0, i / COLORS, opt_wide); } ++shown; } else if (shown) { break; } } switch (c = wGetchar(stdscr)) { case 'b': opt_bold = FALSE; break; case 'B': opt_bold = TRUE; break; case 'n': opt_nums = FALSE; break; case 'N': opt_nums = TRUE; break; case ESCAPE: case 'q': done = TRUE; continue; case 'w': set_color_test(opt_wide, FALSE); break; case 'W': set_color_test(opt_wide, TRUE); break; case CTRL('p'): case KEY_UP: if (base_row <= 0) { beep(); } else { base_row -= 1; } break; case CTRL('n'): case KEY_DOWN: if (base_row + page_size >= row_limit) { beep(); } else { base_row += 1; } break; case CTRL('b'): case KEY_PREVIOUS: case KEY_PPAGE: if (base_row <= 0) { beep(); } else { base_row -= (page_size - 1); if (base_row < 0) base_row = 0; } break; case CTRL('f'): case KEY_NEXT: case KEY_NPAGE: if (base_row + page_size >= row_limit) { beep(); } else { base_row += page_size - 1; if (base_row + page_size >= row_limit) { base_row = row_limit - page_size - 1; } } break; case '?': if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) { box(helpwin, 0, 0); color_legend(helpwin); wGetchar(helpwin); delwin(helpwin); } break; default: beep(); continue; } } erase(); endwin();}#endif /* USE_WIDEC_SUPPORT */static voidchange_color(int current, int field, int value, int usebase){ short red, green, blue; if (usebase) color_content(current, &red, &green, &blue); else red = green = blue = 0; switch (field) { case 0: red += value; break; case 1: green += value; break; case 2: blue += value; break; } if (init_color(current, red, green, blue) == ERR) beep();}static voidinit_all_colors(void){ int c; for (c = 0; c < COLORS; ++c) init_color(c, all_colors[c].red, all_colors[c].green, all_colors[c].blue);}#define scaled_rgb(n) ((255 * (n)) / 1000)static voidcolor_edit(void)/* display the color test pattern, without trying to edit colors */{ int i, this_c = 0, value = 0, current = 0, field = 0; int last_c; int top_color = 0; int page_size = (LINES - 6); init_all_colors(); refresh(); for (i = 0; i < max_colors; i++) init_pair(i, COLOR_WHITE, i); mvprintw(LINES - 2, 0, "Number: %d", value); do { short red, green, blue; attron(A_BOLD); mvaddstr(0, 20, "Color RGB Value Editing"); attroff(A_BOLD); for (i = top_color; (i - top_color < page_size) && (i < max_colors); i++) { char numeric[80]; sprintf(numeric, "[%d]", i); mvprintw(2 + i - top_color, 0, "%c %-8s:", (i == current ? '>' : ' '), (i < (int) SIZEOF(the_color_names) ? the_color_names[i] : numeric)); attrset(COLOR_PAIR(i)); addstr(" "); attrset(A_NORMAL); color_content(i, &red, &green, &blue); addstr(" R = "); if (current == i && field == 0) attron(A_STANDOUT); printw("%04d", red); if (current == i && field == 0) attrset(A_NORMAL); addstr(", G = "); if (current == i && field == 1) attron(A_STANDOUT); printw("%04d", green); if (current == i && field == 1) attrset(A_NORMAL); addstr(", B = "); if (current == i && field == 2) attron(A_STANDOUT); printw("%04d", blue); if (current == i && field == 2) attrset(A_NORMAL); attrset(A_NORMAL); printw(" ( %3d %3d %3d )", scaled_rgb(red), scaled_rgb(green), scaled_rgb(blue)); } mvaddstr(LINES - 3, 0, "Use up/down to select a color, left/right to change fields."); mvaddstr(LINES - 2, 0, "Modify field by typing nnn=, nnn-, or nnn+. ? for help."); move(2 + current - top_color, 0); last_c = this_c; this_c = Getchar(); if (this_c < 256 && isdigit(this_c) && !isdigit(last_c)) value = 0; switch (this_c) { case CTRL('b'): case KEY_PPAGE: if (current > 0) current -= (page_size - 1); else beep(); break; case CTRL('f'): case KEY_NPAGE: if (current < (max_colors - 1)) current += (page_size - 1); else beep(); break; case CTRL('p'): case KEY_UP: current = (current == 0 ? (max_colors - 1) : current - 1); break; case CTRL('n'): case KEY_DOWN: current = (current == (max_colors - 1) ? 0 : current + 1); break; case KEY_RIGHT: field = (field == 2 ? 0 : field + 1); break; case KEY_LEFT: field = (field == 0 ? 2 : field - 1); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = value * 10 + (this_c - '0'); break; case '+': change_color(current, field, value, 1); break; case '-': change_color(current, field, -value, 1); break; case '=': change_color(current, field, value, 0); break; case '?': erase(); P(" RGB Value Editing Help"); P(""); P("You are in the RGB value editor. Use the arrow keys to select one of"); P("the fields in one of the RGB triples of the current colors; the one"); P("currently selected will be reverse-video highlighted."); P(""); P("To change a field, enter the digits of the new value; they are echoed"); P("as entered. Finish by typing `='. The change will take effect instantly."); P("To increment or decrement a value, use the same procedure, but finish"); P("with a `+' or `-'."); P(""); P("Press 'm' to invoke the top-level menu with the current color settings."); P("To quit, do `x' or 'q'"); Pause(); erase(); break; case 'm': endwin(); main_menu(FALSE); refresh(); break; case 'x': case 'q': break; default: beep(); break; } if (current < 0) current = 0; if (current >= max_colors) current = max_colors - 1; if (current < top_color) top_color = current; if (current - top_color >= page_size) top_color = current - (page_size - 1); mvprintw(LINES - 1, 0, "Number: %d", value); clrtoeol(); } while (this_c != 'x' && this_c != 'q'); erase(); /* * ncurses does not reset each color individually when calling endwin(). */ init_all_colors(); endwin();}/**************************************************************************** * * Soft-key label test * ****************************************************************************/#define SLK_HELP 17#define SLK_WORK (SLK_HELP + 3)static voidslk_help(void){ static const char *table[] = { "Available commands are:" ,"" ,"^L -- repaint this message and activate soft keys" ,"a/d -- activate/disable soft keys" ,"c -- set centered format for labels" ,"l -- set left-justified format for labels" ,"r -- set right-justified format for labels" ,"[12345678] -- set label; labels are numbered 1 through 8" ,"e -- erase stdscr (should not erase labels)" ,"s -- test scrolling of shortened screen"#if HAVE_SLK_COLOR ,"F/B -- cycle through foreground/background colors"#endif ,"x, q -- return to main menu" ,"" ,"Note: if activating the soft keys causes your terminal to scroll up" ,"one line, your terminal auto-scrolls when anything is written to the" ,"last screen position. The ncurses code does not yet handle this" ,"gracefully." }; unsigned j; move(2, 0); for (j = 0; j < SIZEOF(table); ++j) { P(table[j]); } refresh();}static voidslk_test(void)/* exercise the soft keys */{ int c, fmt = 1; char buf[9]; char *s;#if HAVE_SLK_COLOR short fg = COLOR_BLACK; short bg = COLOR_WHITE; bool new_color = FALSE;#endif c = CTRL('l');#if HAVE_SLK_COLOR if (has_colors()) { new_color = TRUE; }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -