summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWormHeamer2025-11-04 18:10:20 -0500
committerWormHeamer2025-11-04 18:10:20 -0500
commite379662045c4dc6e149b84ab9e43ceecb427451e (patch)
treefdaa95df54659fbb4dd093d7c51e137d89f87c85
parent1101c551c96d8440aa19537f30ed47732bf2b4c8 (diff)
more curs_move() escapes
-rw-r--r--main.c59
1 files changed, 37 insertions, 22 deletions
diff --git a/main.c b/main.c
index c3de4f8..0468589 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
#include <string.h>
#include <assert.h>
#include <stdarg.h>
+#include <stdbit.h>
#include <stdio.h>
#include <unistd.h>
@@ -13,6 +14,8 @@
#include <poll.h>
#include <time.h>
+#define CSI "\x1b["
+
/* TODO
*
* - utf-8 input
@@ -171,9 +174,9 @@ static void vui_adjust(void) {
void vui_curs_vis(int vis) {
if (vis) {
- fputs("\x1b[?25h", stdout);
+ fputs(CSI "?25h", stdout);
} else {
- fputs("\x1b[?25l", stdout);
+ fputs(CSI "?25l", stdout);
}
}
@@ -215,7 +218,7 @@ void vui_fini(void) {
vui_curs_vis(1);
free_buf(&vui_win.buf1);
free_buf(&vui_win.buf2);
- printf("\x1b[H\x1b[2J\x1b[0m");
+ printf(CSI "H" CSI "2J" CSI "0m");
}
static inline int bchr_equiv(VuiBuffer *back, VuiBuffer *front, int x, int y) {
@@ -268,14 +271,30 @@ static inline void vui_out_flush(void) {
static inline void curs_move(int src_x, int src_y, int dst_x, int dst_y) {
if (src_x != dst_x && src_y != dst_y) {
if (dst_x > 0) {
- vui_outf("\x1b[%d;%dH", dst_y + 1, dst_x + 1);
+ vui_outf(CSI "%d;%dH", dst_y + 1, dst_x + 1);
+ } else if (dst_y > 0) {
+ vui_outf(CSI "%dH", dst_y + 1);
} else {
- vui_outf("\x1b[%dH", dst_y + 1);
+ vui_outs(CSI "H");
}
} else if (src_x != dst_x) {
- vui_outf("\x1b[%dG", dst_x + 1);
+ if (dst_x == 0) {
+ vui_outc('\r');
+ } else if (dst_x == src_x - 1) {
+ vui_outc('\b');
+ } else {
+ vui_outf(CSI "%dG", dst_x + 1);
+ }
} else if (src_y != dst_y) {
- vui_outf("\x1b[%dd", dst_y + 1);
+ if (dst_y == src_y - 1) {
+ vui_outs("\x1bM");
+ } else if (dst_y == src_y + 1) {
+ vui_outs("\x1bD");
+ } if (dst_y > 0) {
+ vui_outf(CSI "%dd", dst_y + 1);
+ } else {
+ vui_outs(CSI "d");
+ }
}
}
@@ -289,7 +308,7 @@ static void attr_chg(VuiAttr *ptr, VuiAttr to) {
/* popcnt(f_attr ^ t_attr) to detect nr. of changes */
if (chg_attr) {
- vui_outs("\x1b[0");
+ vui_outs(CSI "0");
if (to & A_BOLD) vui_outs(";1");
if (to & A_DIM) vui_outs(";2");
if (to & A_ITALIC) vui_outs(";3");
@@ -310,7 +329,7 @@ static void attr_chg(VuiAttr *ptr, VuiAttr to) {
int chg_bg = (t_bg != f_bg);
if (chg_fg || chg_bg) {
if (chg_attr) vui_outc(';');
- else vui_outs("\x1b[");
+ else vui_outs(CSI);
if (chg_fg) {
vui_outc(t_fg > 7 ? '9' : '3');
vui_outc((t_fg & 7) + '0');
@@ -340,7 +359,7 @@ void vui_blit(void) {
VuiBuffer *front = vui_win.front;
VuiBuffer *back = vui_win.back;
- vui_outf("\x1b[H\x1b[0m");
+ vui_outf(CSI "H" CSI "0m");
VuiAttr attr_last = ATTR_DEFAULT;
if (vui_win.redraw_all) {
@@ -372,31 +391,29 @@ void vui_blit(void) {
if (vui_win.scroll_x || vui_win.scroll_y) {
if (abs(vui_win.scroll_x) >= vui_win.width || abs(vui_win.scroll_y) > vui_win.height) {
- vui_outs("\x1b[2J");
+ vui_outs(CSI "2J");
goto scrolled;
}
if (vui_win.scroll_x < 0) {
for (unsigned y = 0; y < vui_win.height; y++) {
curs_move(cur_x, cur_y, 0, y);
- vui_outf("\x1b[%dP", -vui_win.scroll_x);
+ vui_outf(CSI "%dP", -vui_win.scroll_x);
cur_y = y;
}
} else if (vui_win.scroll_x > 0) {
for (unsigned y = 0; y < vui_win.height; y++) {
curs_move(cur_x, cur_y, 0, y);
- vui_outf("\x1b[%d@", vui_win.scroll_x);
+ vui_outf(CSI "%d@", vui_win.scroll_x);
cur_y = y;
}
}
if (vui_win.scroll_y < 0) {
- curs_move(cur_x, cur_y, 0, 0);
- cur_x = (cur_y = 0);
- vui_outf("\x1b[%dM", -vui_win.scroll_y);
+ if (vui_win.scroll_y == -1) vui_outs(CSI "S");
+ else vui_outf(CSI "%dS", -vui_win.scroll_y);
} else if (vui_win.scroll_y > 0) {
- curs_move(cur_x, cur_y, 0, 0);
- cur_x = (cur_y = 0);
- vui_outf("\x1b[%dL", vui_win.scroll_y);
+ if (vui_win.scroll_y == 1) vui_outs(CSI "T");
+ else vui_outf(CSI "%dT", vui_win.scroll_y);
}
scrolled:
@@ -545,13 +562,11 @@ int half_y = 1;
void draw(void *ctx) {
(void)ctx;
- /*
vui_chra(x, y, C, ((x + y) & 0xf) | BG_BBLUE);
vui_chra(x-1, y, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
vui_chra(x+1, y, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
vui_chra(x, y-1, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
vui_chra(x, y+1, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
- */
vui_printf(-1, -1, "(%u, %u)", vui_win.width, vui_win.height);
//vui_printf(0, -1, "front buffer = %p", (void*)vui_win.front);
@@ -669,6 +684,6 @@ int main(int argc, const char **argv) {
}
}
vui_fini();
- printf("\x1b[2J\x1b[H %u, %u\n", vui_win.width, vui_win.height);
+ printf(CSI "2J" CSI "H %u, %u\n", vui_win.width, vui_win.height);
return 0;
}