commit 551f981f93c8b045b5708f803d241f12dc5cda13
parent 2d3131c25602e96d239c8cae6fa1f9f1d132a14d
Author: typable <contact@typable.dev>
Date: Mon, 27 May 2024 17:01:56 +0200
fix: Fixed resize rendering
Diffstat:
M | src/main.c | | | 36 | ++++++++++++++++++++++++------------ |
1 file changed, 24 insertions(+), 12 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -25,6 +25,7 @@ bool do_edit_post(int id) {
snprintf(command, 200, "%s %s", editor, filename);
system(command);
term_write("\x1b[?25l");
+ term_write("\x1b[?1049h");
term_flush();
post_t *changes = NULL;
edit_read(filename, &changes);
@@ -57,6 +58,7 @@ int main(void) {
}
term_enable_raw_mode();
term_write("\x1b[?25l");
+ term_write("\x1b[?1049h");
bool quit = false;
bool update = true;
bool render = true;
@@ -65,20 +67,26 @@ int main(void) {
int height = 0;
post_result_t *post_result = NULL;
while (!quit) {
+ int rows = 0;
+ int cols = 0;
+ term_read_window_size(&rows, &cols);
+ if (rows - 4 != height) {
+ if (index > rows - 4) {
+ offset = index - (rows - 4) + 1;
+ }
+ else {
+ offset = 0;
+ }
+ render = true;
+ }
+ height = rows - 4;
if (update) {
post_result = db_post_search(POET_DB, "");
update = false;
}
if (render) {
- int rows = 0;
- int cols = 0;
- term_read_window_size(&rows, &cols);
- // TODO: adjust offset after resize
- height = rows - 4;
- term_write("\x1b[3J");
- term_write("\x1b[2J");
term_write("\x1b[H");
- term_write("\r\n q = quit, j/k = move, g = goto, n = create, i = edit, d = delete, x = public/private\r\n\r\n");
+ term_write("\x1b[K\r\n q = quit, j/k = move, g = goto, n = create, i = edit, d = delete, x = public/private\x1b[K\r\n\x1b[K\r\n");
for (int i = 0; i < post_result->len; i++) {
if (i == height) {
break;
@@ -97,13 +105,14 @@ int main(void) {
term_writef("%s", post->title);
}
char *date = util_format_json_date(post->date, "%Y-%m-%d");
- term_writef(" \x1b[38;2;124;182;84m(%s)\x1b[39m\r\n", date);
+ term_writef(" \x1b[38;2;124;182;84m(%s)\x1b[39m\x1b[K\r\n", date);
free(date);
}
+ term_write("\x1b[J");
term_flush();
render = false;
}
- int c = term_read_key();
+ int c = term_poll_key(500);
switch (c) {
case 'q': {
quit = true;
@@ -132,8 +141,8 @@ int main(void) {
case 'i': {
if (do_edit_post(post_result->result[index].id)) {
update = true;
- render = true;
}
+ render = true;
break;
}
case 'd': {
@@ -161,7 +170,9 @@ int main(void) {
case 'e': {
if (post_result->len > 1) {
index = post_result->len - 1;
- offset = index - height + 1;
+ if (index > height) {
+ offset = index - height + 1;
+ }
}
else {
index = 0;
@@ -192,6 +203,7 @@ int main(void) {
term_write("\x1b[2J");
term_write("\x1b[?25h");
term_write("\x1b[H");
+ term_write("\x1b[?1049l");
term_flush();
return 0;
}