📄 ncurses.c
字号:
if (has_colors()) { int pair = (fg * max_colors) + bg; if (!pairs[pair]) { init_pair(pair, fg, bg); pairs[pair] = TRUE; } normal |= COLOR_PAIR(pair); } bkgd(normal); bkgdset(normal); erase(); box(stdscr, 0, 0); mvaddstr(0, 20, "Character attribute test display"); row = show_attr(row, n, ac | A_STANDOUT, "STANDOUT"); row = show_attr(row, n, ac | A_REVERSE, "REVERSE"); row = show_attr(row, n, ac | A_BOLD, "BOLD"); row = show_attr(row, n, ac | A_UNDERLINE, "UNDERLINE"); row = show_attr(row, n, ac | A_DIM, "DIM"); row = show_attr(row, n, ac | A_BLINK, "BLINK"); row = show_attr(row, n, ac | A_PROTECT, "PROTECT"); row = show_attr(row, n, ac | A_INVIS, "INVISIBLE"); row = show_attr(row, n, ac | A_NORMAL, "NORMAL"); mvprintw(row, 8, "This terminal does %shave the magic-cookie glitch", tigetnum("xmc") > -1 ? "" : "not "); mvprintw(row + 1, 8, "Enter a digit to set gaps on each side of displayed attributes"); mvprintw(row + 2, 8, "^L repaints, </> shifts, "); if (has_colors()) printw("f/F/b/F toggle color (now %d/%d), a/A ACS (%d)", fg, bg, ac != 0); else printw("a/A ACS (%d)", ac != 0); refresh(); } while (attr_getc(&n, &fg, &bg, &ac)); free((char *) pairs); bkgdset(A_NORMAL | BLANK); erase(); endwin();}/**************************************************************************** * * Color support tests * ****************************************************************************/static NCURSES_CONST char *the_color_names[] ={ "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "BLACK", "RED", "GREEN", "YELLOW", "BLUE", "MAGENTA", "CYAN", "WHITE"};static voidshow_color_name(int y, int x, int color){ if (max_colors > 8) mvprintw(y, x, "%02d ", color); else mvaddstr(y, x, the_color_names[color]);}static voidcolor_test(void)/* generate a color test pattern */{ int i; int base, top, width; const char *hello; refresh(); (void) printw("There are %d color pairs\n", COLOR_PAIRS); width = (max_colors > 8) ? 4 : 8; hello = (max_colors > 8) ? "Test" : "Hello"; for (base = 0; base < 2; base++) { top = (max_colors > 8) ? 0 : base * (max_colors + 3); clrtobot(); (void) mvprintw(top + 1, 0, "%dx%d matrix of foreground/background colors, bright *%s*\n", max_colors, max_colors, base ? "on" : "off"); for (i = 0; i < max_colors; i++) show_color_name(top + 2, (i + 1) * width, i); for (i = 0; i < max_colors; i++) show_color_name(top + 3 + i, 0, i); for (i = 1; i < max_pairs; i++) { init_pair(i, i % max_colors, i / max_colors); attron((attr_t) COLOR_PAIR(i)); if (base) attron((attr_t) A_BOLD); mvaddstr(top + 3 + (i / max_colors), (i % max_colors + 1) * width, hello); attrset(A_NORMAL); } if ((max_colors > 8) || base) Pause(); } erase(); endwin();}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 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; 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 = 0; i < max_colors; i++) { mvprintw(2 + i, 0, "%c %-8s:", (i == current ? '>' : ' '), (i < (int) SIZEOF(the_color_names) ? the_color_names[i] : "")); attrset(COLOR_PAIR(i)); addstr(" "); attrset(A_NORMAL); /* * Note: this refresh should *not* be necessary! It works around * a bug in attribute handling that apparently causes the A_NORMAL * attribute sets to interfere with the actual emission of the * color setting somehow. This needs to be fixed. */ refresh(); 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); addstr(")"); } mvaddstr(max_colors + 3, 0, "Use up/down to select a color, left/right to change fields."); mvaddstr(max_colors + 4, 0, "Modify field by typing nnn=, nnn-, or nnn+. ? for help."); move(2 + current, 0); last_c = this_c; this_c = Getchar(); if (isdigit(this_c) && !isdigit(last_c)) value = 0; switch (this_c) { case KEY_UP: current = (current == 0 ? (max_colors - 1) : current - 1); break; 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("To quit, do `x' or 'q'"); Pause(); erase(); break; case 'x': case 'q': break; default: beep(); break; } mvprintw(LINES - 2, 0, "Number: %d", value); clrtoeol(); } while (this_c != 'x' && this_c != 'q'); erase(); 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 do {#if HAVE_SLK_COLOR if (new_color) { init_pair(1, bg, fg); slk_color(1); new_color = FALSE; mvprintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg); refresh(); }#endif move(0, 0); switch (c) { case CTRL('l'): erase(); attron(A_BOLD); mvaddstr(0, 20, "Soft Key Exerciser"); attroff(A_BOLD); slk_help(); /* fall through */ case 'a': slk_restore(); break; case 'e': wclear(stdscr); break; case 's': mvprintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: "); while ((c = Getchar()) != 'Q' && (c != ERR)) addch((chtype) c); break; case 'd': slk_clear(); break; case 'l': fmt = 0; break; case 'c': fmt = 1; break; case 'r': fmt = 2; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': (void) mvaddstr(SLK_WORK, 0, "Please enter the label value: "); strcpy(buf, ""); if ((s = slk_label(c - '0')) != 0) { strncpy(buf, s, 8); } wGetstring(stdscr, buf, 8); slk_set((c - '0'), buf, fmt); slk_refresh(); move(SLK_WORK, 0); clrtobot(); break; case 'x': case 'q': goto done;#if HAVE_SLK_COLOR case 'F': if (has_colors()) { fg = (fg + 1) % max_colors; new_color = TRUE; } break; case 'B': if (has_colors()) { bg = (bg + 1) % max_colors; new_color = TRUE; } break;#endif default: beep(); } } while ((c = Getchar()) != EOF); done: erase(); endwin();}#if USE_WIDEC_SUPPORTstatic voidwide_slk_test(void)/* exercise the soft keys */{ int c, fmt = 1; wchar_t buf[9]; char *s; short fg = COLOR_BLACK; short bg = COLOR_WHITE; bool new_color = FALSE; c = CTRL('l'); if (has_colors()) { new_color = TRUE; } do { if (new_color) { init_pair(1, bg, fg); slk_color(1); new_color = FALSE; mvprintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg); refresh(); } move(0, 0); switch (c) { case CTRL('l'): erase(); attr_on(WA_BOLD, NULL); mvaddstr(0, 20, "Soft Key Exerciser"); attr_off(WA_BOLD, NULL); slk_help(); /* fall through */ case 'a': slk_restore(); break; case 'e': wclear(stdscr); break; case 's': mvprintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: "); while ((c = Getchar()) != 'Q' && (c != ERR)) addch((chtype) c); break; case 'd': slk_clear(); break; case 'l': fmt = 0; break; case 'c': fmt = 1; break; case 'r': fmt = 2; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': (void) mvaddstr(SLK_WORK, 0, "Please enter the label value: "); *buf = 0; if ((s = slk_label(c - '0')) != 0) { int j; for (j = 0; j < 8; ++j) { if ((buf[j] = UChar(s[j])) == 0) break; } buf[j] = 0; } wGet_wstring(stdscr, buf, 8); slk_wset((c - '0'), buf, fmt); slk_refresh(); move(SLK_WORK, 0); clrtobot(); break; case 'x': case 'q': goto done; case 'F': if (has_colors()) { fg = (fg + 1) % max_colors; new_color = TRUE; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -