~rcr/rirc

ref: 7ca2a99a605b7e0b88fa85bc43a8fadc13695ba8 rirc/src/components/input.h -rw-r--r-- 2.1 KiB
7ca2a99a — Richard Robbins refactor order of includes 1 year, 2 months ago
                                                                                
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
#ifndef INPUT_H
#define INPUT_H

/* Buffer input
 *
 * Supports line editing, input history, word completion
 *
 * The working edit area is implemented as a fixed width
 * gap buffer for O(1) insertions, deletions and O(n)
 * cursor movements
 *
 * Input history is kept as a ring buffer of strings,
 * copied into the working area when scrolling
 */

#include <stddef.h>
#include <stdint.h>

/* 410 max characters for input should be sufficient given
 * rfc2812 maximum length of 50 characters for channel names,
 * plus 50 characters for additional message formatting */
#ifndef INPUT_LEN_MAX
#define INPUT_LEN_MAX 410
#endif

/* Number of history lines to keep for input. For proper
 * ring buffer masking this must be a power of 2 */
#ifndef INPUT_HIST_MAX
#define INPUT_HIST_MAX 16
#endif

/* Input completion callback type, returning length
 * of replacement word, or 0 if no match, with args: */
typedef uint16_t (*f_completion_cb)(
	char*,    /* word to replace */
	uint16_t, /* word length */
	uint16_t, /* word replacement max length */
	int);     /* word is start of input */

struct input
{
	char text[INPUT_LEN_MAX];
	struct {
		char *ptrs[INPUT_HIST_MAX];
		char *save;
		uint16_t current; /* Ring buffer current entry */
		uint16_t head;    /* Ring buffer head */
		uint16_t tail;    /* Ring buffer tail */
	} hist;
	uint16_t head;        /* Gap buffer head */
	uint16_t tail;        /* Gap buffer tail */
	uint16_t window;      /* Gap buffer frame window */
};

void input_init(struct input*);
void input_free(struct input*);

/* Input manipulation */
int input_cursor_back(struct input*);
int input_cursor_forw(struct input*);
int input_delete_back(struct input*);
int input_delete_forw(struct input*);
int input_insert(struct input*, const char*, size_t);
int input_reset(struct input*);

/* Input completion */
int input_complete(struct input*, f_completion_cb);

/* Input history */
int input_hist_back(struct input*);
int input_hist_forw(struct input*);
int input_hist_push(struct input*);

/* Write input to string */
uint16_t input_frame(struct input*, char*, uint16_t);
uint16_t input_write(struct input*, char*, uint16_t, uint16_t);

#endif