-
Notifications
You must be signed in to change notification settings - Fork 21
/
crossline.h
178 lines (136 loc) · 5.8 KB
/
crossline.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* crossline.h -- Version 1.0
*
* Crossline is a small, self-contained, zero-config, MIT licensed,
* cross-platform, readline and libedit replacement.
*
* Press <F1> to get full shortcuts list.
*
* See crossline.c for more information.
*
* ------------------------------------------------------------------------
*
* MIT License
*
* Copyright (c) 2019, JC Wang (wang_junchuan@163.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ------------------------------------------------------------------------
*/
#ifndef __CROSSLINE_H
#define __CROSSLINE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
CROSSLINE_FGCOLOR_DEFAULT = 0x00,
CROSSLINE_FGCOLOR_BLACK = 0x01,
CROSSLINE_FGCOLOR_RED = 0x02,
CROSSLINE_FGCOLOR_GREEN = 0x03,
CROSSLINE_FGCOLOR_YELLOW = 0x04,
CROSSLINE_FGCOLOR_BLUE = 0x05,
CROSSLINE_FGCOLOR_MAGENTA = 0x06,
CROSSLINE_FGCOLOR_CYAN = 0x07,
CROSSLINE_FGCOLOR_WHITE = 0x08,
CROSSLINE_FGCOLOR_BRIGHT = 0x80,
CROSSLINE_FGCOLOR_MASK = 0x7F,
CROSSLINE_BGCOLOR_DEFAULT = 0x0000,
CROSSLINE_BGCOLOR_BLACK = 0x0100,
CROSSLINE_BGCOLOR_RED = 0x0200,
CROSSLINE_BGCOLOR_GREEN = 0x0300,
CROSSLINE_BGCOLOR_YELLOW = 0x0400,
CROSSLINE_BGCOLOR_BLUE = 0x0500,
CROSSLINE_BGCOLOR_MAGENTA = 0x0600,
CROSSLINE_BGCOLOR_CYAN = 0x0700,
CROSSLINE_BGCOLOR_WHITE = 0x0800,
CROSSLINE_BGCOLOR_BRIGHT = 0x8000,
CROSSLINE_BGCOLOR_MASK = 0x7F00,
CROSSLINE_UNDERLINE = 0x10000,
CROSSLINE_COLOR_DEFAULT = CROSSLINE_FGCOLOR_DEFAULT | CROSSLINE_BGCOLOR_DEFAULT
} crossline_color_e;
// Main API to read a line, return buf if get line, return NULL if EOF.
extern char* crossline_readline (const char *prompt, char *buf, int size);
// Same with crossline_readline except buf holding initial input for editing.
extern char* crossline_readline2 (const char *prompt, char *buf, int size);
// Set move/cut word delimiter, default is all not digital and alphabetic characters.
extern void crossline_delimiter_set (const char *delim);
// Read a character from terminal without echo
extern int crossline_getch (void);
/*
* History APIs
*/
// Save history to file
extern int crossline_history_save (const char *filename);
// Load history from file
extern int crossline_history_load (const char *filename);
// Show history in buffer
extern void crossline_history_show (void);
// Clear history
extern void crossline_history_clear (void);
/*
* Completion APIs
*/
typedef struct crossline_completions_t crossline_completions_t;
typedef void (*crossline_completion_callback) (const char *buf, crossline_completions_t *pCompletions);
// Register completion callback
extern void crossline_completion_register (crossline_completion_callback pCbFunc);
// Add completion in callback. Word is must, help for word is optional.
extern void crossline_completion_add (crossline_completions_t *pCompletions, const char *word, const char *help);
// Add completion with color.
extern void crossline_completion_add_color (crossline_completions_t *pCompletions, const char *word,
crossline_color_e wcolor, const char *help, crossline_color_e hcolor);
// Set syntax hints in callback
extern void crossline_hints_set (crossline_completions_t *pCompletions, const char *hints);
// Set syntax hints with color
extern void crossline_hints_set_color (crossline_completions_t *pCompletions, const char *hints, crossline_color_e color);
/*
* Paging APIs
*/
// Enable/Disble paging control
extern int crossline_paging_set (int enable);
// Check paging after print a line, return 1 means quit, 0 means continue
// if you know only one line is printed, just give line_len = 1
extern int crossline_paging_check (int line_len);
/*
* Cursor APIs
*/
// Get screen rows and columns
extern void crossline_screen_get (int *pRows, int *pCols);
// Clear current screen
extern void crossline_screen_clear (void);
// Get cursor postion (0 based)
extern int crossline_cursor_get (int *pRow, int *pCol);
// Set cursor postion (0 based)
extern void crossline_cursor_set (int row, int col);
// Move cursor with row and column offset, row_off>0 move up row_off lines, <0 move down abs(row_off) lines
// =0 no move for row, similar with col_off
extern void crossline_cursor_move (int row_off, int col_off);
// Hide or show cursor
extern void crossline_cursor_hide (int bHide);
/*
* Color APIs
*/
// Set text color, CROSSLINE_COLOR_DEFAULT will revert to default setting
// `\t` is not supported in Linux terminal, same below. Don't use `\n` in Linux terminal, same below.
extern void crossline_color_set (crossline_color_e color);
// Set default prompt color
extern void crossline_prompt_color_set (crossline_color_e color);
#ifdef __cplusplus
}
#endif
#endif /* __CROSSLINE_H */