#include <test.priv.h>
#define TABSIZE 8
static int margin = (2 * TABSIZE) - 1;
static void
legend(WINDOW *win, char *buffer, int length)
{
wmove(win, 0, 0);
wprintw(win,
"The Strings/Chars displays should match. Enter any characters.\n");
wprintw(win,
"Use down-arrow or ^N to repeat on the next line, 'q' to exit.\n");
wclrtoeol(win);
wprintw(win, "Inserted %d characters <%s>", length, buffer);
}
static int
ColOf(char *buffer, int length)
{
int n;
int result;
for (n = 0, result = margin + 1; n < length; ++n) {
int ch = UChar(buffer[n]);
switch (ch) {
case '\n':
case '\r':
result = 0;
break;
case '\b':
if (result > 0)
--result;
break;
case '\t':
result += (TABSIZE - (result % TABSIZE));
break;
case '\177':
result += 2;
break;
default:
++result;
if (ch < 32)
++result;
break;
}
}
return result;
}
int
main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
{
int ch;
int limit;
int row = 1;
int col;
int length;
char buffer[BUFSIZ];
WINDOW *work;
WINDOW *show;
putenv("TABSIZE=8");
initscr();
(void) cbreak();
(void) noecho();
keypad(stdscr, TRUE);
limit = LINES - 5;
work = newwin(limit, COLS, 0, 0);
show = newwin(4, COLS, limit + 1, 0);
keypad(work, TRUE);
for (col = margin + 1; col < COLS; col += TABSIZE)
mvwvline(work, row, col, '.', limit - 2);
box(work, 0, 0);
mvwvline(work, row, margin, ACS_VLINE, limit - 2);
mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
limit /= 2;
mvwaddstr(work, 1, 2, "String");
mvwaddstr(work, limit + 1, 2, "Chars");
wnoutrefresh(work);
buffer[length = 0] = '\0';
legend(show, buffer, length);
wnoutrefresh(show);
doupdate();
if (has_colors()) {
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLUE);
wbkgdset(work, COLOR_PAIR(1) | ' ');
}
while ((ch = wgetch(work)) != 'q') {
wmove(work, row, margin + 1);
switch (ch) {
case CTRL('N'):
case KEY_DOWN:
if (row < limit) {
++row;
mvwinsstr(work, row, margin + 1, buffer);
for (col = 0; col < length; ++col) {
mvwinsch(work, limit + row, ColOf(buffer, col), buffer[col]);
}
} else {
beep();
}
break;
case KEY_BACKSPACE:
ch = '\b';
default:
if (ch <= 0 || ch > 255) {
beep();
break;
}
buffer[length++] = ch;
buffer[length] = '\0';
mvwinsstr(work,
row,
ColOf(buffer, length - 1), buffer + length - 1);
mvwinsch(work,
limit + row,
ColOf(buffer, length - 1), ch);
wnoutrefresh(work);
legend(show, buffer, length);
wnoutrefresh(show);
doupdate();
break;
}
}
endwin();
ExitProgram(EXIT_SUCCESS);
}