~ecs/sd

ref: f654cc6705b2e65181f3e1d915388bb592bceb5a sd/common.h -rw-r--r-- 1.4 KiB
f654cc67 — Eyal Sawady Use registry-based commands for normal mode 10 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
79
80
#ifndef COMMON_H
#define COMMON_H
#include <stddef.h>
#include <signal.h>

#define CTRL_C 0x3
#define ESCAPE 0x1b
#define DEL 0x7f

#define VT100 "\x1b["
#define CLEAR_SCREEN VT100 "2J"
#define CURSOR_HOME VT100 "H"

#define MAX_COLS "999"
#define MAX_ROWS "999"

enum mode {
	NORMAL,
	INPUT,
	CMD
};

struct line {
	size_t capacity;
	char *contents;
};

struct line_arr {
	size_t capacity;
	size_t sz;
	struct line *contents;
};

struct state {
	enum mode mode;

	sig_atomic_t winch;
	sig_atomic_t rendering;

	int rows;
	int cols;
	int cur_x;
	int cur_y;

	int dirty;

	size_t cur_line;
	struct line_arr *lines;

	struct line insert_buffer;

	struct line status;

	struct registry {
		struct command {
			const char *cmd;
			int (*func)(struct state *);
		} *contents;
		size_t capacity;
		size_t sz;
	} creg;

	struct registry nreg;

	char *filename;
};

void write_str(const char *s);
int set_row(struct line *row, const char *str);
int append_row(struct line *row, const char *str);
int insert_row(struct line *row, const char *str, size_t pos);
struct line split_row(struct line *row, size_t pos);
struct line_arr *new_line_arr(void);
int append_line(struct line_arr *arr, struct line line);
int insert_line(struct line_arr *arr, struct line line, size_t pos);
void init_registry(struct registry *reg);
int register_command(struct registry *reg, const char *cmd,
	int (*func)(struct state *));
int run_command(struct state *state, const char *cmd);
#endif