-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathttyio.h
More file actions
115 lines (95 loc) · 3.8 KB
/
ttyio.h
File metadata and controls
115 lines (95 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright ttyio (C) by Alex Eski 2025 */
/* Licensed under GPLv3, see LICENSE for more information. */
/* ttyio.h: public interface for the ttyio library */
#ifndef TTYIO_GUARD_H_
#define TTYIO_GUARD_H_
/* WARN: current implementation is not tested to be threadsafe,
* despite unibilium being threadsafe.
*/
#include "tcaps.h"
#ifndef TTYIO_RED_ERROR
# define TTYIO_RED_ERROR 196
#endif
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
typedef struct {
size_t x;
size_t y;
} Coordinates;
/* enum input_type
* Canonical: read line by line, only get the line after user presses enter. a lot of programs work this way.
* Noncanonical: read character by character. programs who need control over each input need to use this.
*/
#if __STDC_VERSION__ >= 202311L /* C23 */
enum input_type: short {
TTY_NONE = 0,
TTY_CANONICAL_MODE = 1,
TTY_NONCANONICAL_MODE = 2
};
#else
enum input_type {
TTY_NONE = 0,
TTY_CANONICAL_MODE = 1,
TTY_NONCANONICAL_MODE = 2
};
#endif /* C23 */
extern termcaps tcaps;
Coordinates tty_get_size(void);
Coordinates tty_get_pos(void);
/* Just init term and tcaps */
void tty_init_caps(void);
/* Just init the input mode (canonical or noncanonical). */
void tty_init_input_mode(enum input_type input_type);
/* Init everything (set input mode and setup term & tcaps) */
void tty_init(enum input_type input_type);
/* Just deinit term and tcaps (free internally used memory) */
void tty_deinit_caps(void);
/* Just deinit the input mode (reset to original) */
void tty_deinit_input_mode(void);
/* Deinit everything (reset input mode and free internally used memory) */
void tty_deinit(void);
/* Output, tracks pos of cursor for you and stores in term */
int tty_putc_invis();
int tty_putc(char c);
int tty_fputc(FILE* restrict file, char c);
int tty_dputc(int fd, char c);
int tty_write(const char* restrict buf, size_t n);
int tty_writeln(const char* restrict buf, size_t n);
int tty_fwrite(FILE* restrict file, const char* restrict buf, size_t n);
int tty_fwriteln(FILE* restrict file, const char* restrict buf, size_t n);
int tty_dwrite(int fd, const char* restrict buf, size_t n);
int tty_dwriteln(int fd, const char* restrict buf, size_t n);
int tty_puts(const char* restrict str);
int tty_fputs(const char* restrict str, FILE* restrict file);
int tty_print(const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
int tty_println(const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
int tty_fprint(FILE* restrict file, const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
int tty_fprintln(FILE* restrict file, const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
int tty_dprint(int fd, const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
int tty_dprintln(int fd, const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
int tty_perror(const char* restrict msg);
int tty_perrorf(const char* restrict fmt, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
/* Output using tcaps, fallsback to ASCII control characters if cap not found. */
int tty_send(cap* restrict c);
int tty_dsend(int fd, cap* restrict c);
int tty_fsend(cap* restrict c, FILE* restrict file);
void tty_send_n(cap* restrict c, size_t n);
void tty_dsend_n(int fd, cap* restrict c, size_t n);
void tty_fsend_n(cap* restrict c, size_t n, FILE* restrict file);
/* Colors */
/* Colors don't have a fallback. If tcaps.color_max is 0, no color is set. */
int tty_color_set(int color);
int tty_color_bg_set(int color);
#define tty_color_reset() tty_send(&tcaps.color_reset)
#ifdef __cplusplus
}
#endif // __cplusplus
#endif /* !TTYIO_GUARD_H_ */