HAMMER Utilities: Sync with 60F
[dragonfly.git] / contrib / ncurses-5.4 / test / inserts.c
blob990da1d4fe99c8e95609fd6ff5d1db1b99efadf2
1 /*
2 * $Id: inserts.c,v 1.5 2003/08/09 22:07:06 tom Exp $
4 * Demonstrate the winsstr() and winsch functions.
5 * Thomas Dickey - 2002/10/19
6 */
8 #include <test.priv.h>
10 #define TABSIZE 8
12 static int margin = (2 * TABSIZE) - 1;
14 static void
15 legend(WINDOW *win, char *buffer, int length)
17 wmove(win, 0, 0);
18 wprintw(win,
19 "The Strings/Chars displays should match. Enter any characters.\n");
20 wprintw(win,
21 "Use down-arrow or ^N to repeat on the next line, 'q' to exit.\n");
22 wclrtoeol(win);
23 wprintw(win, "Inserted %d characters <%s>", length, buffer);
26 static int
27 ColOf(char *buffer, int length)
29 int n;
30 int result;
32 for (n = 0, result = margin + 1; n < length; ++n) {
33 int ch = UChar(buffer[n]);
34 switch (ch) {
35 case '\n':
36 /* actually newline should clear the remainder of the line
37 * and move to the next line - but that seems a little awkward
38 * in this example.
40 case '\r':
41 result = 0;
42 break;
43 case '\b':
44 if (result > 0)
45 --result;
46 break;
47 case '\t':
48 result += (TABSIZE - (result % TABSIZE));
49 break;
50 case '\177':
51 result += 2;
52 break;
53 default:
54 ++result;
55 if (ch < 32)
56 ++result;
57 break;
60 return result;
63 int
64 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
66 int ch;
67 int limit;
68 int row = 1;
69 int col;
70 int length;
71 char buffer[BUFSIZ];
72 WINDOW *work;
73 WINDOW *show;
75 putenv("TABSIZE=8");
76 initscr();
77 (void) cbreak(); /* take input chars one at a time, no wait for \n */
78 (void) noecho(); /* don't echo input */
79 keypad(stdscr, TRUE);
81 limit = LINES - 5;
82 work = newwin(limit, COLS, 0, 0);
83 show = newwin(4, COLS, limit + 1, 0);
84 keypad(work, TRUE);
86 for (col = margin + 1; col < COLS; col += TABSIZE)
87 mvwvline(work, row, col, '.', limit - 2);
89 box(work, 0, 0);
90 mvwvline(work, row, margin, ACS_VLINE, limit - 2);
91 mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
92 limit /= 2;
94 mvwaddstr(work, 1, 2, "String");
95 mvwaddstr(work, limit + 1, 2, "Chars");
96 wnoutrefresh(work);
98 buffer[length = 0] = '\0';
99 legend(show, buffer, length);
100 wnoutrefresh(show);
102 doupdate();
105 * Show the characters inserted in color, to distinguish from those that
106 * are shifted.
108 if (has_colors()) {
109 start_color();
110 init_pair(1, COLOR_WHITE, COLOR_BLUE);
111 wbkgdset(work, COLOR_PAIR(1) | ' ');
114 while ((ch = wgetch(work)) != 'q') {
115 wmove(work, row, margin + 1);
116 switch (ch) {
117 case CTRL('N'):
118 case KEY_DOWN:
119 if (row < limit) {
120 ++row;
121 /* put the whole string in, all at once */
122 mvwinsstr(work, row, margin + 1, buffer);
124 /* do the corresponding single-character insertion */
125 for (col = 0; col < length; ++col) {
126 mvwinsch(work, limit + row, ColOf(buffer, col), buffer[col]);
128 } else {
129 beep();
131 break;
132 case KEY_BACKSPACE:
133 ch = '\b';
134 /* FALLTHRU */
135 default:
136 if (ch <= 0 || ch > 255) {
137 beep();
138 break;
140 buffer[length++] = ch;
141 buffer[length] = '\0';
142 /* put the string in, one character at a time */
143 mvwinsstr(work,
144 row,
145 ColOf(buffer, length - 1), buffer + length - 1);
147 /* do the corresponding single-character insertion */
148 mvwinsch(work,
149 limit + row,
150 ColOf(buffer, length - 1), ch);
151 wnoutrefresh(work);
153 legend(show, buffer, length);
154 wnoutrefresh(show);
156 doupdate();
157 break;
160 endwin();
161 ExitProgram(EXIT_SUCCESS);