Skip to content

Commit 63dbeee

Browse files
committed
Extract CSI sequence
1 parent 8ac766d commit 63dbeee

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

ext/io/console/console.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ getattr(int fd, conmode *t)
7575
#define SET_LAST_ERROR (0)
7676
#endif
7777

78+
#define CSI "\x1b\x5b"
79+
7880
static ID id_getc, id_console, id_close;
7981
static ID id_gets, id_flush, id_chomp_bang;
8082

@@ -1142,7 +1144,7 @@ static VALUE
11421144
console_scroll(VALUE io, int line)
11431145
{
11441146
if (line) {
1145-
VALUE s = rb_sprintf("\x1b[%d%c", line < 0 ? -line : line,
1147+
VALUE s = rb_sprintf(CSI "%d%c", line < 0 ? -line : line,
11461148
line < 0 ? 'T' : 'S');
11471149
rb_io_write(io, s);
11481150
}
@@ -1204,7 +1206,7 @@ console_goto(VALUE io, VALUE y, VALUE x)
12041206
rb_syserr_fail(LAST_ERROR, 0);
12051207
}
12061208
#else
1207-
rb_io_write(io, rb_sprintf("\x1b[%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
1209+
rb_io_write(io, rb_sprintf(CSI "%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
12081210
#endif
12091211
return io;
12101212
}
@@ -1229,8 +1231,8 @@ console_move(VALUE io, int y, int x)
12291231
#else
12301232
if (x || y) {
12311233
VALUE s = rb_str_new_cstr("");
1232-
if (y) rb_str_catf(s, "\x1b[%d%c", y < 0 ? -y : y, y < 0 ? 'A' : 'B');
1233-
if (x) rb_str_catf(s, "\x1b[%d%c", x < 0 ? -x : x, x < 0 ? 'D' : 'C');
1234+
if (y) rb_str_catf(s, CSI "%d%c", y < 0 ? -y : y, y < 0 ? 'A' : 'B');
1235+
if (x) rb_str_catf(s, CSI "%d%c", x < 0 ? -x : x, x < 0 ? 'D' : 'C');
12341236
rb_io_write(io, s);
12351237
rb_io_flush(io);
12361238
}
@@ -1255,7 +1257,7 @@ console_goto_column(VALUE io, VALUE val)
12551257
rb_syserr_fail(LAST_ERROR, 0);
12561258
}
12571259
#else
1258-
rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1));
1260+
rb_io_write(io, rb_sprintf(CSI "%dG", NUM2UINT(val)+1));
12591261
#endif
12601262
return io;
12611263
}
@@ -1290,7 +1292,7 @@ console_erase_line(VALUE io, VALUE val)
12901292
constat_clear(h, ws.wAttributes, w, *pos);
12911293
return io;
12921294
#else
1293-
rb_io_write(io, rb_sprintf("\x1b[%dK", mode));
1295+
rb_io_write(io, rb_sprintf(CSI "%dK", mode));
12941296
#endif
12951297
return io;
12961298
}
@@ -1332,7 +1334,7 @@ console_erase_screen(VALUE io, VALUE val)
13321334
}
13331335
constat_clear(h, ws.wAttributes, w, *pos);
13341336
#else
1335-
rb_io_write(io, rb_sprintf("\x1b[%dJ", mode));
1337+
rb_io_write(io, rb_sprintf(CSI "%dJ", mode));
13361338
#endif
13371339
return io;
13381340
}

0 commit comments

Comments
 (0)