2021-01-27 15:52:39 +03:00
|
|
|
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
2020-11-26 02:28:57 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-11-12 14:12:51 +03:00
|
|
|
module ui
|
|
|
|
|
2021-01-27 15:52:39 +03:00
|
|
|
struct ExtraContext {
|
|
|
|
mut:
|
|
|
|
read_buf []byte
|
2022-01-25 15:46:48 +03:00
|
|
|
// 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
|
2021-01-27 15:52:39 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 19:04:14 +03:00
|
|
|
const ctx_ptr = &Context(0)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
|
|
|
pub fn init(cfg Config) &Context {
|
|
|
|
mut ctx := &Context{
|
2021-04-23 14:48:04 +03:00
|
|
|
cfg: cfg
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2021-04-23 14:48:04 +03:00
|
|
|
ctx.read_buf = []byte{cap: cfg.buffer_size}
|
2020-11-13 16:30:47 +03:00
|
|
|
|
|
|
|
// lmao
|
2020-11-12 14:12:51 +03:00
|
|
|
unsafe {
|
2021-04-23 14:48:04 +03:00
|
|
|
x := &ui.ctx_ptr
|
2020-11-12 14:12:51 +03:00
|
|
|
*x = ctx
|
2021-04-23 14:48:04 +03:00
|
|
|
_ = x
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:52:39 +03:00
|
|
|
[inline]
|
|
|
|
fn save_title() {
|
2021-04-23 14:48:04 +03:00
|
|
|
// restore the previously saved terminal title
|
|
|
|
print('\x1b[22;0t')
|
2020-11-13 16:30:47 +03:00
|
|
|
}
|
2021-01-27 15:52:39 +03:00
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn load_title() {
|
2021-04-23 14:48:04 +03:00
|
|
|
// restore the previously saved terminal title
|
|
|
|
print('\x1b[23;0t')
|
2020-11-13 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2020-11-26 02:28:57 +03:00
|
|
|
pub fn (mut ctx Context) run() ? {
|
2020-11-12 14:12:51 +03:00
|
|
|
if ctx.cfg.use_x11 {
|
|
|
|
ctx.fail('error: x11 backend not implemented yet')
|
|
|
|
exit(1)
|
|
|
|
} else {
|
2021-04-23 14:48:04 +03:00
|
|
|
ctx.termios_setup() ?
|
2020-11-12 14:12:51 +03:00
|
|
|
ctx.termios_loop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shifts the array left, to remove any data that was just read, and updates its len
|
|
|
|
// TODO: remove
|
2021-04-23 14:48:04 +03:00
|
|
|
[inline]
|
2020-11-12 14:12:51 +03:00
|
|
|
fn (mut ctx Context) shift(len int) {
|
|
|
|
unsafe {
|
2022-04-15 14:58:56 +03:00
|
|
|
C.memmove(ctx.read_buf.data, &u8(ctx.read_buf.data) + len, ctx.read_buf.cap - len)
|
2020-11-12 14:12:51 +03:00
|
|
|
ctx.resize_arr(ctx.read_buf.len - len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: don't actually do this, lmao
|
|
|
|
[inline]
|
|
|
|
fn (mut ctx Context) resize_arr(size int) {
|
2021-04-25 21:40:38 +03:00
|
|
|
mut l := unsafe { &ctx.read_buf.len }
|
2021-04-23 14:48:04 +03:00
|
|
|
unsafe {
|
|
|
|
*l = size
|
|
|
|
_ = l
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|