~ecs/sd

ref: f654cc6705b2e65181f3e1d915388bb592bceb5a sd/rendering.c -rw-r--r-- 3.3 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
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "commands.h"
#include "common.h"
#include "term.h"

static void
update_state(struct state *state)
{
	if (get_window_size(&(state->rows), &(state->cols)) == -1) {
		die("get_window_size");
	}
}

static void
load_file(struct state *state)
{
	char *line = NULL;
	size_t cap = 0;
	ssize_t sz = 0;
	FILE *f = fopen(state->filename, "r");
	if (!f) {
		die(state->filename);
	}
	while ((sz = getline(&line, &cap, f)) > 0) {
		struct line row = { 0, NULL };
		line[sz - 1] = '\0';
		set_row(&row, line);
		if (append_line(state->lines, row)) {
			break;
		}
	}
	free(line);
	fclose(f);
}

void
init_state(struct state *state, const char *filename)
{
	state->mode = NORMAL;

	state->winch = 0;
	state->rendering = 0;

	state->status.capacity = 0;
	set_row(&state->status, "Welcome to sd version " VERSION);
	set_row(&state->insert_buffer, "");

	state->cur_x = 0;
	state->cur_y = 0;

	state->cur_line = 0;
	state->lines = new_line_arr();

	init_creg(&state->creg);
	init_nreg(&state->nreg);

	state->filename = strdup(filename);

	load_file(state);

	raw_termios();

	update_state(state);
}

static int
move_cursor(struct state *state, struct line buf)
{
	size_t lineno = state->cur_y + state->cur_line;
	const char *cur_line = state->lines->contents[lineno].contents;
	size_t i;
	for (i = 0; i < (size_t)state->cur_x; i++) {
		switch (cur_line[i]) {
		case '\t':
			if (append_row(&buf, "\t") == -1) {
				return -1;
			}
			break;
		default:
			if (append_row(&buf, VT100 "C") == -1) {
				return -1;
			}
		}
	}
	if (state->mode != INPUT) {
		return 0;
	}
	for (i = 0; i < strlen(state->insert_buffer.contents); i++) {
		switch (state->insert_buffer.contents[i]) {
		case '\t':
			if (append_row(&buf, "\t") == -1) {
				return -1;
			}
			break;
		default:
			if (append_row(&buf, VT100 "C") == -1) {
				return -1;
			}
		}
	}
	return 0;
}

void
refresh_screen(struct state *state)
{
	size_t y;
	struct line buf = { 0, NULL };

	state->rendering = 1;

	if (state->winch) {
		update_state(state);
		state->winch = 0;
	}

	append_row(&buf, CURSOR_HOME VT100 "K");

	for (y = state->cur_line; y < state->cur_line + (size_t)state->rows - 1;
			y++) {
		if (y == (size_t)state->cur_y + state->cur_line
				&& state->mode == INPUT) {
			struct line cur = state->lines->contents[y];
			char contents_x = cur.contents[state->cur_x];
			cur.contents[state->cur_x] = '\0';
			append_row(&buf, cur.contents);
			cur.contents[state->cur_x] = contents_x;

			append_row(&buf, state->insert_buffer.contents);

			append_row(&buf, cur.contents + state->cur_x);
		} else if (y < state->lines->sz) {
			/* TODO: syntax highlighting */
			append_row(&buf, state->lines->contents[y].contents);
		} else {
			append_row(&buf, "~");
		}

		append_row(&buf, "\r\n" VT100 "K");
	}

	append_row(&buf, state->status.contents);

	if (state->mode == NORMAL) {
		char b[BUFSIZ];
		size_t ibuf_len = strlen(state->insert_buffer.contents);
		sprintf(b, "\r" VT100 "%dC", state->cols - (int)ibuf_len);
		append_row(&buf, b);
		append_row(&buf, state->insert_buffer.contents);
	}

	if (state->mode != CMD) {
		char cursor_pos[BUFSIZ];
		sprintf(cursor_pos, VT100 "%d;0H", state->cur_y + 1);
		append_row(&buf, cursor_pos);
		move_cursor(state, buf);
	}

	write_str(buf.contents);
	free(buf.contents);

	state->rendering = 0;
}