libite
conio.h
Go to the documentation of this file.
1 /* A conio.h like implementation for VTANSI displays.
2  *
3  * Copyright (c) 2009-2021 Joachim Wiberg <troglobit@gmail.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #ifndef LIBITE_CONIO_H_
33 #define LIBITE_CONIO_H_
34 
35 #include <stdio.h>
36 
38 #define RESETATTR 0
39 #define BRIGHT 1
40 #define DIM 2
41 #define UNDERSCORE 4
42 #define BLINK 5 /* May not work on all displays. */
43 #define REVERSE 7
44 #define HIDDEN 8
45 
47 #define BLACK 0x0
48 #define RED 0x1
49 #define GREEN 0x2
50 #define BROWN 0x3
51 #define BLUE 0x4
52 #define MAGENTA 0x5
53 #define CYAN 0x6
54 #define LIGHTGREY 0x7
55 
56 #define DARKGREY 0x10
57 #define LIGHTRED 0x11
58 #define LIGHTGREEN 0x12
59 #define YELLOW 0x13
60 #define LIGHTBLUE 0x14
61 #define LIGHTMAGENTA 0x15
62 #define LIGHTCYAN 0x16
63 #define WHITE 0x17
64 
65 #ifndef SCREEN_WIDTH
66 #define SCREEN_WIDTH 80
67 #endif
68 
70 #define clrscr() fputs("\033[2J\033[1;1H", stdout)
71 
72 #define clreol() fputs("\033[K", stdout)
73 
74 #define delline() fputs("\033[2K", stdout)
75 
76 #define gotoxy(x,y) fprintf(stdout, "\033[%d;%dH", y, x)
77 
78 #define hidecursor() fputs("\033[?25l", stdout)
79 
80 #define showcursor() fputs("\033[?25h", stdout)
81 
83 #define __set_gm(a,c,v) \
84  if (!c) \
85  fprintf(stdout, "\033[%dm", a); \
86  else \
87  fprintf(stdout, "\033[%d;%dm", c & 0x10 ? 1 : 0, (c & 0xF) + v)
88 
90 #define textattr(attr) __set_gm(attr, 0, 0)
91 
92 #define textcolor(color) __set_gm(RESETATTR, color, 30)
93 
94 #define textbackground(color) __set_gm(RESETATTR, color, 40)
95 
96 void initscr(int *row, int *col);
97 
111 static inline void printhdr(FILE *fp, const char *line, int nl, int attr)
112 {
113  if (attr < 0 || attr > 8)
114  attr = REVERSE;
115 
116  fprintf(fp ?: stdout,
117  "%s\033[%dm%s%*s\033[0m\n",
118  nl ? "\n" : "", attr, line,
119  SCREEN_WIDTH - (int)strlen(line), "");
120 }
121 
128 static inline void printheader(FILE *fp, const char *line, int nl)
129 {
130  printhdr(fp, line, nl, REVERSE);
131 }
132 
133 #endif /* LIBITE_CONIO_H_ */
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
#define SCREEN_WIDTH
Fallback screen width, possible to override.
Definition: conio.h:66
void initscr(int *row, int *col)
Probe terminal size.
Definition: conio.c:44