Remove tty_esc() and tty_jump() wrappers, just output ANSI directly.

This commit is contained in:
Rob Landley 2022-01-03 22:02:21 -06:00
parent 25fd949e51
commit 9a1b068c35
6 changed files with 25 additions and 51 deletions

View File

@ -348,8 +348,6 @@ int scan_key_getsize(char *scratch, int timeout_ms, unsigned *xx, unsigned *yy);
void xsetspeed(struct termios *tio, int speed);
int set_terminal(int fd, int raw, int speed, struct termios *old);
void xset_terminal(int fd, int raw, int speed, struct termios *old);
void tty_esc(char *s);
void tty_jump(int x, int y);
void tty_reset(void);
void tty_sigreset(int i);
void start_redraw(unsigned *width, unsigned *height);

View File

@ -142,9 +142,9 @@ int crunch_rev_escape(FILE *out, int cols, int wc)
{
int rc;
tty_esc("7m");
xputsn("\e[7m");
rc = crunch_escape(out, cols, wc);
tty_esc("27m");
xputsn("\e[27m");
return rc;
}

View File

@ -252,27 +252,10 @@ int scan_key(char *scratch, int timeout_ms)
return scan_key_getsize(scratch, timeout_ms, NULL, NULL);
}
void tty_esc(char *s)
{
printf("\e[%s", s);
}
void tty_jump(int x, int y)
{
char s[32];
sprintf(s, "%d;%dH", y+1, x+1);
tty_esc(s);
}
void tty_reset(void)
{
set_terminal(0, 0, 0, 0);
tty_esc("?25h");
tty_esc("0m");
tty_jump(0, 999);
tty_esc("K");
fflush(0);
xputsn("\e[?25h\e[0m\e[999H\e[K");
}
// If you call set_terminal(), use sigatexit(tty_sigreset);

View File

@ -30,20 +30,17 @@ void demo_scankey_main(void)
y = 1;
sigatexit(tty_sigreset); // Make ctrl-c restore tty
tty_esc("?25l"); // hide cursor
tty_esc("0m"); // reset color to default
tty_esc("2J"); // Clear screen
// hide cursor, reset color to default, clear screen
xputsn("\e[?25l\e0m\e[2J");
xset_terminal(1, 1, 0, 0); // Raw mode
for (;;) {
tty_jump(x, y);
xputc(c);
printf("\e[%u;%uH%c", y+1, x+1, c);
t[1&++tick] = time(0);
if (t[0] != t[1]) terminal_probesize(&width, &height);
// Don't block first time through, to force header print
key = scan_key_getsize(scratch, -1*!!t[0], &width, &height);
tty_jump(0, 0);
printf("ESC to exit: ");
printf("\e[HESC to exit: ");
// Print unknown escape sequence
if (*scratch) {
printf("key=[ESC");
@ -58,8 +55,7 @@ void demo_scankey_main(void)
if (key == -2) continue;
if (key <= ' ') break;
if (key>=256) {
tty_jump(x, y);
xputc(' ');
printf("\e[%u;%uH ", y+1, x+1);
key -= 256;
if (key==KEY_UP) y--;

View File

@ -1294,9 +1294,9 @@ static int vi_crunch(FILE *out, int cols, int wc)
{
int ret = 0;
if (wc < 32 && TT.list) {
tty_esc("1m");
xputsn("\e[1m");
ret = crunch_escape(out,cols,wc);
tty_esc("m");
xputsn("\e[m");
} else if (wc == 0x09) {
if (out) {
int i = TT.tabstop;
@ -1368,10 +1368,10 @@ static void draw_page()
if (TT.drawn_row<0 || TT.cur_row<0 || TT.scr_row<0) redraw = 3;
else if (abs(scroll)>TT.screen_height/2) redraw = 3;
tty_jump(0, 0);
if (redraw&2) tty_esc("2J"), tty_esc("H"); //clear screen
else if (scroll>0) printf("\033[%dL", scroll); //scroll up
else if (scroll<0) printf("\033[%dM", -scroll); //scroll down
xputsn("\e[H"); // jump to top left
if (redraw&2) xputsn("\e[2J\e[h"); //clear screen
else if (scroll>0) printf("\e[%dL", scroll); //scroll up
else if (scroll<0) printf("\e[%dM", -scroll); //scroll down
SOL = text_sol(TT.cursor);
bytes = text_getline(toybuf, SOL, ARRAY_LEN(toybuf));
@ -1388,8 +1388,7 @@ static void draw_page()
end = line;
tty_jump(0, y);
tty_esc("2K");
xputsn("\e[%uH\e[2K", y+1);
//find cursor position
aw = crunch_nstr(&end, INT_MAX, bytes, 0, "\t\n", vi_crunch);
@ -1454,9 +1453,9 @@ static void draw_page()
scroll++, draw_line++;
else if (scroll>0) scroll--, draw_line++;
tty_jump(0, y);
printf("\e[%uH", y+1);
if (draw_line) {
tty_esc("2K");
xputsn("\e[2K");
if (line && strlen(line)) {
aw = crunch_nstr(&line, clip, bytes, 0, "\t\n", vi_crunch);
crunch_str(&line, TT.screen_width-1, stdout, "\t\n", vi_crunch);
@ -1473,8 +1472,7 @@ static void draw_page()
TT.drawn_row = TT.scr_row, TT.drawn_col = clip;
// Finished updating visual area, show status line.
tty_jump(0, TT.screen_height);
tty_esc("2K");
printf("\e[%uH\e[2K", TT.screen_height+1);
if (TT.vi_mode == 2) printf("\033[1m-- INSERT --\033[m");
if (!TT.vi_mode) {
cx_scr = printf("%s", TT.il->data);
@ -1487,10 +1485,8 @@ static void draw_page()
(100*TT.cursor)/(TT.filesize ? : 1), TT.cur_row+1, TT.cur_col+1);
if (TT.cur_col != cx_scr) sprintf(toybuf+strlen(toybuf),"-%d", cx_scr+1);
}
tty_jump(TT.screen_width-strlen(toybuf), TT.screen_height);
printf("%s", toybuf);
tty_jump(cx_scr, cy_scr);
printf("\e[%u;%uH%s\e[%u;%uH", toybuf, TT.screen_height+1,
1+TT.screen_width-strlen(toybuf), cy_scr+1, cx_scr+1);
xflush(1);
}
@ -1525,7 +1521,7 @@ void vi_main(void)
set_terminal(0, 1, 0, 0);
//writes stdout into different xterm buffer so when we exit
//we dont get scroll log full of junk
tty_esc("?1049h");
xputsn("\e[?1049h");
for (;;) {
int key = 0;
@ -1696,5 +1692,5 @@ cleanup_vi:
linelist_unload();
free(TT.il->data), free(TT.il), free(TT.yank.data);
tty_reset();
tty_esc("?1049l");
xputsn("\e[?1049l");
}

View File

@ -43,6 +43,7 @@
* at right edge? (Not adjusting to screen size at all? Header wraps?)
* TODO: top: thread support and SMP
* TODO: pgrep -f only searches the amount of cmdline that fits in toybuf.
* TODO: pgrep qemu-system-i386 never matches because one char too long
USE_PS(NEWTOY(ps, "k(sort)*P(ppid)*aAdeflMno*O*p(pid)*s*t*Tu*U*g*G*wZ[!ol][+Ae][!oO]", TOYFLAG_BIN|TOYFLAG_LOCALE))
// stayroot because iotop needs root to read other process' proc/$$/io
@ -1466,7 +1467,7 @@ static int header_line(int line, int rev)
static void top_cursor_cleanup(void)
{
tty_esc("?25h");
xputsn("\e[?25h");
}
static void top_common(
@ -1490,7 +1491,7 @@ static void top_common(
if (!FLAG(b)) {
setbuf(stdout, stdout_buf);
sigatexit(top_cursor_cleanup);
tty_esc("?25l");
xputsn("\e[?25l");
}
toys.signal = SIGWINCH;