[ruby/io-console] Extract CSI sequence

https://github.com/ruby/io-console/commit/63dbeeecf4
This commit is contained in:
Nobuyoshi Nakada 2023-12-16 15:30:02 +09:00 committed by Hiroshi SHIBATA
parent 6931222883
commit 4173c4bcfb

View File

@ -75,6 +75,8 @@ getattr(int fd, conmode *t)
#define SET_LAST_ERROR (0) #define SET_LAST_ERROR (0)
#endif #endif
#define CSI "\x1b\x5b"
static ID id_getc, id_console, id_close; static ID id_getc, id_console, id_close;
static ID id_gets, id_flush, id_chomp_bang; static ID id_gets, id_flush, id_chomp_bang;
@ -1142,7 +1144,7 @@ static VALUE
console_scroll(VALUE io, int line) console_scroll(VALUE io, int line)
{ {
if (line) { if (line) {
VALUE s = rb_sprintf("\x1b[%d%c", line < 0 ? -line : line, VALUE s = rb_sprintf(CSI "%d%c", line < 0 ? -line : line,
line < 0 ? 'T' : 'S'); line < 0 ? 'T' : 'S');
rb_io_write(io, s); rb_io_write(io, s);
} }
@ -1204,7 +1206,7 @@ console_goto(VALUE io, VALUE y, VALUE x)
rb_syserr_fail(LAST_ERROR, 0); rb_syserr_fail(LAST_ERROR, 0);
} }
#else #else
rb_io_write(io, rb_sprintf("\x1b[%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1)); rb_io_write(io, rb_sprintf(CSI "%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
#endif #endif
return io; return io;
} }
@ -1229,8 +1231,8 @@ console_move(VALUE io, int y, int x)
#else #else
if (x || y) { if (x || y) {
VALUE s = rb_str_new_cstr(""); VALUE s = rb_str_new_cstr("");
if (y) rb_str_catf(s, "\x1b[%d%c", y < 0 ? -y : y, y < 0 ? 'A' : 'B'); if (y) rb_str_catf(s, CSI "%d%c", y < 0 ? -y : y, y < 0 ? 'A' : 'B');
if (x) rb_str_catf(s, "\x1b[%d%c", x < 0 ? -x : x, x < 0 ? 'D' : 'C'); if (x) rb_str_catf(s, CSI "%d%c", x < 0 ? -x : x, x < 0 ? 'D' : 'C');
rb_io_write(io, s); rb_io_write(io, s);
rb_io_flush(io); rb_io_flush(io);
} }
@ -1255,7 +1257,7 @@ console_goto_column(VALUE io, VALUE val)
rb_syserr_fail(LAST_ERROR, 0); rb_syserr_fail(LAST_ERROR, 0);
} }
#else #else
rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1)); rb_io_write(io, rb_sprintf(CSI "%dG", NUM2UINT(val)+1));
#endif #endif
return io; return io;
} }
@ -1290,7 +1292,7 @@ console_erase_line(VALUE io, VALUE val)
constat_clear(h, ws.wAttributes, w, *pos); constat_clear(h, ws.wAttributes, w, *pos);
return io; return io;
#else #else
rb_io_write(io, rb_sprintf("\x1b[%dK", mode)); rb_io_write(io, rb_sprintf(CSI "%dK", mode));
#endif #endif
return io; return io;
} }
@ -1332,7 +1334,7 @@ console_erase_screen(VALUE io, VALUE val)
} }
constat_clear(h, ws.wAttributes, w, *pos); constat_clear(h, ws.wAttributes, w, *pos);
#else #else
rb_io_write(io, rb_sprintf("\x1b[%dJ", mode)); rb_io_write(io, rb_sprintf(CSI "%dJ", mode));
#endif #endif
return io; return io;
} }