v/vlib/term/ui/input_nix.c.v

74 lines
1.5 KiB
V
Raw Permalink Normal View History

// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module ui
struct ExtraContext {
mut:
2022-04-15 14:35:35 +02:00
read_buf []u8
// read_all_bytes causes all the raw bytes to be read as one event unit.
// This is cruicial for UTF-8 support since Unicode codepoints can span several bytes.
read_all_bytes bool = true
}
const ctx_ptr = &Context(0)
pub fn init(cfg Config) &Context {
mut ctx := &Context{
2021-04-23 13:48:04 +02:00
cfg: cfg
}
2022-04-15 14:35:35 +02:00
ctx.read_buf = []u8{cap: cfg.buffer_size}
// lmao
unsafe {
2021-04-23 13:48:04 +02:00
x := &ui.ctx_ptr
*x = ctx
2021-04-23 13:48:04 +02:00
_ = x
}
return ctx
}
[inline]
fn save_title() {
2021-04-23 13:48:04 +02:00
// restore the previously saved terminal title
print('\x1b[22;0t')
flush_stdout()
}
[inline]
fn load_title() {
2021-04-23 13:48:04 +02:00
// restore the previously saved terminal title
print('\x1b[23;0t')
flush_stdout()
}
pub fn (mut ctx Context) run() ? {
if ctx.cfg.use_x11 {
ctx.fail('error: x11 backend not implemented yet')
exit(1)
} else {
ctx.termios_setup()?
ctx.termios_loop()
}
}
// shifts the array left, to remove any data that was just read, and updates its len
// TODO: remove
2021-04-23 13:48:04 +02:00
[inline]
fn (mut ctx Context) shift(len int) {
unsafe {
2022-04-15 13:58:56 +02:00
C.memmove(ctx.read_buf.data, &u8(ctx.read_buf.data) + len, ctx.read_buf.cap - len)
ctx.resize_arr(ctx.read_buf.len - len)
}
}
// TODO: don't actually do this, lmao
[inline]
fn (mut ctx Context) resize_arr(size int) {
mut l := unsafe { &ctx.read_buf.len }
2021-04-23 13:48:04 +02:00
unsafe {
*l = size
_ = l
}
}