mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: 21 errors!
This commit is contained in:
parent
694970bf0d
commit
ea5bc2e3b1
30 changed files with 392 additions and 362 deletions
|
|
@ -15,12 +15,15 @@ pub struct Tui {
|
|||
buffer: usize,
|
||||
buffers: [Buffer;2],
|
||||
backend: CrosstermBackend<Stdout>,
|
||||
event: RwLock<Option<TuiEvent>>,
|
||||
area: Rect,
|
||||
sleep: Duration,
|
||||
poll: Duration,
|
||||
}
|
||||
impl Engine for Tui {
|
||||
type HandleInput = Self;
|
||||
type Handled = bool;
|
||||
type RenderInput = Self;
|
||||
type Rendered = Rect;
|
||||
fn exited (&self) -> bool {
|
||||
self.exited.fetch_and(true, Ordering::Relaxed)
|
||||
|
|
@ -46,8 +49,12 @@ impl Engine for Tui {
|
|||
code: KeyCode::Char('c'), modifiers: KeyModifiers::CONTROL, ..
|
||||
}) = event {
|
||||
self.exited.store(true, Ordering::Relaxed);
|
||||
} else if let Err(e) = state.handle(self) {
|
||||
panic!("{e}")
|
||||
} else {
|
||||
*self.event.write().unwrap() = Some(TuiEvent::Input(event));
|
||||
let result = state.handle(self);
|
||||
if let Err(e) = result {
|
||||
panic!("{e}")
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
@ -69,6 +76,7 @@ impl Tui {
|
|||
sleep: Duration::from_millis(20),
|
||||
poll: Duration::from_millis(100),
|
||||
exited: Arc::new(AtomicBool::new(false)),
|
||||
event: RwLock::new(None),
|
||||
buffer: 0,
|
||||
buffers: [Buffer::empty(area), Buffer::empty(area)],
|
||||
backend,
|
||||
|
|
@ -102,6 +110,15 @@ impl Tui {
|
|||
main_thread.join().expect("main thread failed");
|
||||
Ok(state)
|
||||
}
|
||||
pub fn event (&self) -> TuiEvent {
|
||||
self.event.read().unwrap().clone().unwrap()
|
||||
}
|
||||
pub fn area (&self) -> Rect {
|
||||
self.area
|
||||
}
|
||||
pub fn buffer (&mut self) -> &mut Buffer {
|
||||
&mut self.buffers[self.buffer]
|
||||
}
|
||||
fn flip (&mut self) {
|
||||
let previous_buffer = &self.buffers[1 - self.buffer];
|
||||
let current_buffer = &self.buffers[self.buffer];
|
||||
|
|
@ -110,12 +127,6 @@ impl Tui {
|
|||
self.buffers[1 - self.buffer].reset();
|
||||
self.buffer = 1 - self.buffer;
|
||||
}
|
||||
pub fn area (&self) -> Rect {
|
||||
self.area
|
||||
}
|
||||
pub fn buffer (&mut self) -> &mut Buffer {
|
||||
&mut self.buffers[self.buffer]
|
||||
}
|
||||
pub fn buffer_update (&mut self, area: Rect, callback: &impl Fn(&mut Cell, u16, u16)) {
|
||||
buffer_update(self.buffer(), area, callback)
|
||||
}
|
||||
|
|
@ -151,15 +162,40 @@ impl Tui {
|
|||
}
|
||||
Ok(Some(Rect { x, y, width: text.len() as u16, height: 1 }))
|
||||
}
|
||||
#[inline]
|
||||
pub fn alter_area (
|
||||
&mut self, alter: impl Fn(u16, u16, u16, u16)->(u16, u16, u16, u16)
|
||||
) -> &mut Self {
|
||||
let (x, y, width, height) = alter(
|
||||
self.area.x, self.area.y, self.area.width, self.area.height
|
||||
);
|
||||
self.area = Rect { x, y, width, height };
|
||||
self.with_area(x, y, width, height)
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_area (&mut self, x: u16, y: u16, w: u16, h: u16) -> &mut Self {
|
||||
self.with_rect(Rect { x, y, width: w, height: h });
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_rect (&mut self, area: Rect) -> &mut Self {
|
||||
self.area = area;
|
||||
self
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TuiEvent {
|
||||
/// Terminal input
|
||||
Input(::crossterm::event::Event),
|
||||
/// Update values but not the whole form.
|
||||
Update,
|
||||
/// Update the whole form.
|
||||
Redraw,
|
||||
/// Device gains focus
|
||||
Focus,
|
||||
/// Device loses focus
|
||||
Blur,
|
||||
// /// JACK notification
|
||||
// Jack(JackEvent)
|
||||
}
|
||||
|
||||
/// Rendering unit struct to Ratatui returns zero-sized [Rect] at render coordinates.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue