2 * $Id: inserts.c,v 1.13 2005/04/16 15:56:42 tom Exp $
4 * Demonstrate the winsstr() and winsch functions.
5 * Thomas Dickey - 2002/10/19
10 #define InsNStr insnstr
12 #define MvInsNStr mvinsnstr
13 #define MvInsStr mvinsstr
14 #define MvWInsNStr mvwinsnstr
15 #define MvWInsStr mvwinsstr
16 #define WInsNStr winsnstr
17 #define WInsStr winsstr
20 #define MvInsCh mvinsch
21 #define MvWInsCh mvwinsch
33 static bool m_opt = FALSE;
34 static bool w_opt = FALSE;
35 static int n_opt = -1;
38 legend(WINDOW *win, int level, Options state, char *buffer, int length)
40 NCURSES_CONST char *showstate;
48 showstate = " (mvXXX)";
51 showstate = " (winXXX)";
54 showstate = " (mvwinXXX)";
60 "The Strings/Chars displays should match. Enter any characters, except:\n");
62 "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
64 wprintw(win, "Level %d,%s inserted %d characters <%s>", level,
65 showstate, length, buffer);
69 ColOf(char *buffer, int length, int margin)
74 for (n = 0, result = margin + 1; n < length; ++n) {
75 int ch = UChar(buffer[n]);
78 /* actually newline should clear the remainder of the line
79 * and move to the next line - but that seems a little awkward
90 result += (TABSIZE - (result % TABSIZE));
105 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
107 test_inserts(int level)
109 static bool first = TRUE;
121 int margin = (2 * TABSIZE) - 1;
122 Options option = ((m_opt ? oMove : oDefault)
123 | ((w_opt || (level > 0)) ? oWindow : oDefault));
127 setlocale(LC_ALL, "");
129 putenv(strcpy(cmd, "TABSIZE=8"));
132 (void) cbreak(); /* take input chars one at a time, no wait for \n */
133 (void) noecho(); /* don't echo input */
134 keypad(stdscr, TRUE);
139 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
140 work = newwin(limit - 2, COLS - (2 * level), 1, level);
141 show = newwin(4, COLS, limit + 1, 0);
147 show = derwin(stdscr, 4, COLS, limit + 1, 0);
151 for (col = margin + 1; col < COLS; col += TABSIZE)
152 mvwvline(work, row, col, '.', limit - 2);
154 mvwvline(work, row, margin, ACS_VLINE, limit - 2);
155 mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
158 mvwaddstr(work, 1, 2, "String");
159 mvwaddstr(work, limit + 1, 2, "Chars");
162 buffer[length = 0] = '\0';
163 legend(show, level, option, buffer, length);
169 * Show the characters inserted in color, to distinguish from those that
174 init_pair(1, COLOR_WHITE, COLOR_BLUE);
175 wbkgdset(work, COLOR_PAIR(1) | ' ');
178 while ((ch = wgetch(work)) != 'q') {
183 wmove(work, row, margin + 1);
186 test_inserts(level + 1);
202 /* put the whole string in, all at once */
207 for (col = 0; col < length; col += n_opt) {
208 col2 = ColOf(buffer, col, margin);
209 if (move(row, col2) != ERR) {
210 InsNStr(buffer + col, LEN(col));
214 if (move(row, col2) != ERR) {
221 for (col = 0; col < length; col += n_opt) {
222 col2 = ColOf(buffer, col, margin);
223 MvInsNStr(row, col2, buffer + col, LEN(col));
226 MvInsStr(row, col2, buffer);
231 for (col = 0; col < length; col += n_opt) {
232 col2 = ColOf(buffer, col, margin);
233 if (wmove(work, row, col2) != ERR) {
234 WInsNStr(work, buffer + col, LEN(col));
238 if (wmove(work, row, col2) != ERR) {
239 WInsStr(work, buffer);
245 for (col = 0; col < length; col += n_opt) {
246 col2 = ColOf(buffer, col, margin);
247 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
250 MvWInsStr(work, row, col2, buffer);
255 /* do the corresponding single-character insertion */
257 for (col = 0; col < length; ++col) {
258 col2 = ColOf(buffer, col, margin);
261 if (move(row2, col2) != ERR) {
262 InsCh(UChar(buffer[col]));
266 MvInsCh(row2, col2, UChar(buffer[col]));
269 if (wmove(work, row2, col2) != ERR) {
270 WInsCh(work, UChar(buffer[col]));
274 MvWInsCh(work, row2, col2, UChar(buffer[col]));
286 if (ch <= 0 || ch > 255) {
290 buffer[length++] = ch;
291 buffer[length] = '\0';
293 /* put the string in, one character at a time */
294 col = ColOf(buffer, length - 1, margin);
297 if (move(row, col) != ERR) {
298 InsStr(buffer + length - 1);
302 MvInsStr(row, col, buffer + length - 1);
305 if (wmove(work, row, col) != ERR) {
306 WInsStr(work, buffer + length - 1);
310 MvWInsStr(work, row, col, buffer + length - 1);
314 /* do the corresponding single-character insertion */
317 if (move(limit + row, col) != ERR) {
322 MvInsCh(limit + row, col, UChar(ch));
325 if (wmove(work, limit + row, col) != ERR) {
326 WInsCh(work, UChar(ch));
330 MvWInsCh(work, limit + row, col, UChar(ch));
336 legend(show, level, option, buffer, length);
353 static const char *tbl[] =
355 "Usage: inserts [options]"
358 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
359 ," -m perform wmove/move separately from insert-functions"
360 ," -w use window-parameter even when stdscr would be implied"
363 for (n = 0; n < SIZEOF(tbl); ++n)
364 fprintf(stderr, "%s\n", tbl[n]);
365 ExitProgram(EXIT_FAILURE);
369 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
373 setlocale(LC_ALL, "");
375 while ((ch = getopt(argc, argv, "mn:w")) != EOF) {
381 n_opt = atoi(optarg);
398 ExitProgram(EXIT_SUCCESS);