term.ui: simplify/fix the truecolor feature test (#6963)

pull/6975/head
spaceface777 2020-11-26 23:52:20 +01:00 committed by GitHub
parent ea8adfdbf9
commit 6563535a3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 78 deletions

View File

@ -164,88 +164,30 @@ fn (mut ctx Context) termios_setup() ? {
fn get_cursor_position() (int, int) { fn get_cursor_position() (int, int) {
print('\033[6n') print('\033[6n')
// ESC [ YYY `;` XXX `R` buf := malloc(25)
mut reading_x, mut reading_y := false, false len := C.read(C.STDIN_FILENO, buf, 24)
mut x, mut y := 0, 0 unsafe { buf[len] = 0 }
for i := 0; i < 15 ; i++ { s := tos(buf, len)
ch := int(C.getchar()) a := s[2..].split(';')
b := byte(ch) if a.len != 2 { return -1, -1 }
i++ return a[0].int(), a[1].int()
// state management:
if b == `R` {
break
} else if b == `[` {
reading_y = true
reading_x = false
continue
} else if b == `;` {
reading_y = false
reading_x = true
continue
}
// converting string vals to ints:
if reading_x {
x = x * 10 + b - byte(`0`)
} else if reading_y {
y = y * 10 + b - byte(`0`)
}
}
return x, y
} }
fn supports_truecolor() bool { fn supports_truecolor() bool {
// faster/simpler, but less reliable, check
if os.getenv('COLORTERM') in ['truecolor', '24bit'] {
return true
}
// set the bg color to some arbirtrary value (#010203), assumed not to be the default // set the bg color to some arbirtrary value (#010203), assumed not to be the default
print('\x1b[48:2:1:2:3m') print('\x1b[48:2:1:2:3m')
// andquery the current color
sx, sy := get_cursor_position()
// sequence to query the current cursor position
print('\x1bP\$qm\x1b\\') print('\x1bP\$qm\x1b\\')
color := get_current_bg_color() buf := malloc(25)
ex, ey := get_cursor_position() len := C.read(C.STDIN_FILENO, buf, 24)
// if the terminal doesn't understand the "get current color", unsafe { buf[len] = 0 }
// assume it doesn't support truecolor either s := tos(buf, len)
if !(sx == ex && sy == ey) { return '1:2:3' in s
println('>>> different pos: ($sx, $sy) -> ($ex, $ey)')
return false
}
// TODO: iTerm emits a different sequence, but it's compatible anyways, so
if color !in [0x010203, 0x01010203] {
println('>>> no match: $color')
return false
}
return true
}
fn get_current_bg_color() int {
mut res := 0
mut colon_cnt := 0
mut cur_val := 0
for i := 0; i < 50 ; i++ {
ch := int(C.getchar())
b := byte(ch)
if b in [0, 255] {
return -1
} else if b == `m` {
if colon_cnt > 1 {
res = (res << 8) | cur_val
cur_val = 0
}
break
} else if b in [`:`, `;`] {
if colon_cnt > 1 {
res = (res << 8) | cur_val
cur_val = 0
}
colon_cnt++
} else if b.is_digit() {
if colon_cnt > 1 {
cur_val = cur_val * 10 + b - byte(`0`)
}
}
}
return res
} }
fn termios_reset() { fn termios_reset() {
@ -278,9 +220,11 @@ fn (mut ctx Context) termios_loop() {
if !ctx.paused { if !ctx.paused {
sw.restart() sw.restart()
if ctx.cfg.event_fn != voidptr(0) { if ctx.cfg.event_fn != voidptr(0) {
len := C.read(C.STDIN_FILENO, ctx.read_buf.data, ctx.read_buf.cap - ctx.read_buf.len) unsafe {
if len > 0 { len := C.read(C.STDIN_FILENO, ctx.read_buf.data + ctx.read_buf.len, ctx.read_buf.cap - ctx.read_buf.len)
ctx.resize_arr(len) ctx.resize_arr(ctx.read_buf.len + len)
}
if ctx.read_buf.len > 0 {
ctx.parse_events() ctx.parse_events()
} }
} }