]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_tabs.c
ncurses 6.1 - patch 20190720
[ncurses.git] / test / demo_tabs.c
1 /****************************************************************************
2  * Copyright (c) 2019 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /*
30  * Author: Thomas E. Dickey
31  *
32  * $Id: demo_tabs.c,v 1.4 2019/02/24 00:38:13 tom Exp $
33  *
34  * A simple demo of tabs in curses.
35  */
36 #define USE_TINFO
37 #include "test.priv.h"
38
39 static void
40 usage(void)
41 {
42     static const char *msg[] =
43     {
44         "Usage: demo_tabs [options]",
45         "",
46         "Print a grid to test tab-stops with the curses interface",
47         "",
48         "Options:",
49         " -l COUNT total number of lines to show",
50         " -t NUM   set TABSIZE variable to the given value",
51     };
52     unsigned n;
53     for (n = 0; n < SIZEOF(msg); ++n) {
54         fprintf(stderr, "%s\n", msg[n]);
55     }
56     ExitProgram(EXIT_FAILURE);
57 }
58
59 int
60 main(int argc, char *argv[])
61 {
62     int tabstop;
63     int n, col, row, step;
64     int line_limit = -1;
65     int curses_stops = -1;
66
67     while ((n = getopt(argc, argv, "l:t:")) != -1) {
68         switch (n) {
69         case 'l':
70             line_limit = atoi(optarg);
71             break;
72         case 't':
73             curses_stops = atoi(optarg);
74             break;
75         default:
76             usage();
77             break;
78         }
79     }
80
81     initscr();
82     noecho();
83     cbreak();
84     if (curses_stops > 0)
85         set_tabsize(curses_stops);
86 #if HAVE_TIGETNUM
87     tabstop = tigetnum("it");
88     if (tabstop <= 0)
89 #endif
90         tabstop = 8;
91     for (row = 0; row < LINES; ++row) {
92         move(row, 0);
93         for (col = step = 0; col < COLS - 1; ++col) {
94             if (row == 0) {
95                 chtype ch = '-';
96                 if ((col % tabstop) == 0)
97                     ch = '+';
98                 addch(ch);
99             } else if (col + 1 < row) {
100                 addch('*');
101             } else {
102                 printw("%x", step);
103                 col = (row + (tabstop * ++step));
104                 col /= tabstop;
105                 col *= tabstop;
106                 col -= 1;
107                 if ((col + tabstop) < COLS)
108                     addch('\t');
109                 refresh();
110             }
111         }
112         addch('\n');
113         if (line_limit > 0 && row >= line_limit)
114             break;
115     }
116     getch();
117     endwin();
118     ExitProgram(EXIT_SUCCESS);
119 }