mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
Compare commits
2 commits
ab1afa219f
...
a1190a24a1
| Author | SHA1 | Date | |
|---|---|---|---|
| a1190a24a1 | |||
| 3298d6b6e1 |
3 changed files with 71 additions and 19 deletions
|
|
@ -6,15 +6,15 @@ pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||||
/// Namespace where keys are symbols.
|
/// Namespace where keys are symbols.
|
||||||
pub trait DslSymNs<'t, T: 't>: 't {
|
pub trait DslSymNs<'t, T: 't>: 't {
|
||||||
const SYMS: DslNs<'t, fn (&'t Self)->T>;
|
const SYMS: DslNs<'t, fn (&'t Self)->T>;
|
||||||
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Usually<T> {
|
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
if let Some(dsl) = dsl.sym()? {
|
if let Some(dsl) = dsl.sym()? {
|
||||||
for (sym, get) in Self::SYMS.0 {
|
for (sym, get) in Self::SYMS.0 {
|
||||||
if dsl == *sym {
|
if dsl == *sym {
|
||||||
return Ok(get(self))
|
return Ok(Some(get(self)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Err(format!("not found: sym: {dsl:?}").into())
|
return Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,11 +26,21 @@ pub trait DslSymNs<'t, T: 't>: 't {
|
||||||
|
|
||||||
pub trait DslExpNs<'t, T: 't>: 't {
|
pub trait DslExpNs<'t, T: 't>: 't {
|
||||||
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
|
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
|
||||||
|
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Some(exp) = dsl.exp()? {
|
||||||
|
for (key, value) in Self::EXPS.0.iter() {
|
||||||
|
if exp.head() == Ok(Some(key)) {
|
||||||
|
return Ok(Some(value(self, exp.tail()?.unwrap_or(""))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export] macro_rules! dsl_exp (
|
#[macro_export] macro_rules! dsl_exp (
|
||||||
(|$state:ident:$State:ty|->$type:ty { $(
|
(|$state:ident:$State:ty$(, $tail:ident)?|->$type:ty { $(
|
||||||
[$key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*] => $body:expr
|
($key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*) => $body:expr
|
||||||
),* $(,)? }) => {
|
),* $(,)? }) => {
|
||||||
impl<'t> DslExpNs<'t, $type> for $State {
|
impl<'t> DslExpNs<'t, $type> for $State {
|
||||||
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
|
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,37 @@ use crate::ratatui::prelude::Position;
|
||||||
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
||||||
|
|
||||||
impl_content_layout_render!(TuiOut: |self: &str, to|
|
impl_content_layout_render!(TuiOut: |self: &str, to|
|
||||||
layout = to.center_xy([self.chars().count() as u16, 1]);
|
layout = to.center_xy([width_chars_max(to.w(), self), 1]);
|
||||||
render = {let [x, y, ..] = Content::layout(self, to.area());
|
render = {let [x, y, w, ..] = Content::layout(self, to.area());
|
||||||
to.blit(self, x, y, None)});
|
to.text(self, x, y, w)});
|
||||||
|
|
||||||
impl_content_layout_render!(TuiOut: |self: String, to|
|
impl_content_layout_render!(TuiOut: |self: String, to|
|
||||||
layout = to.center_xy([self.chars().count() as u16, 1]);
|
layout = Content::<TuiOut>::layout(&self.as_str(), to);
|
||||||
render = {let [x, y, ..] = Content::layout(self, to.area());
|
render = Content::<TuiOut>::render(&self.as_str(), to));
|
||||||
to.blit(self, x, y, None)});
|
|
||||||
|
impl_content_layout_render!(TuiOut: |self: Arc<str>, to|
|
||||||
|
layout = Content::<TuiOut>::layout(&self.as_ref(), to);
|
||||||
|
render = Content::<TuiOut>::render(&self.as_ref(), to));
|
||||||
|
|
||||||
impl_content_layout_render!(TuiOut: |self: std::sync::RwLock<String>, to|
|
impl_content_layout_render!(TuiOut: |self: std::sync::RwLock<String>, to|
|
||||||
layout = Content::<TuiOut>::layout(&self.read().unwrap(), to);
|
layout = Content::<TuiOut>::layout(&self.read().unwrap(), to);
|
||||||
render = Content::<TuiOut>::render(&self.read().unwrap(), to));
|
render = Content::<TuiOut>::render(&self.read().unwrap(), to));
|
||||||
|
|
||||||
impl_content_layout_render!(TuiOut: |self: std::sync::RwLockReadGuard<'_, String>, to|
|
impl_content_layout_render!(TuiOut: |self: std::sync::RwLockReadGuard<'_, String>, to|
|
||||||
layout = Content::<TuiOut>::layout(&**self, to);
|
layout = Content::<TuiOut>::layout(&**self, to);
|
||||||
render = Content::<TuiOut>::render(&**self, to));
|
render = Content::<TuiOut>::render(&**self, to));
|
||||||
impl_content_layout_render!(TuiOut: |self: Arc<str>, to|
|
|
||||||
layout = to.center_xy([self.chars().count() as u16, 1]);
|
fn width_chars_max (max: u16, text: impl AsRef<str>) -> u16 {
|
||||||
render = to.blit(self, to.area.x(), to.area.y(), None));
|
let mut width: u16 = 0;
|
||||||
|
let mut chars = text.as_ref().chars();
|
||||||
|
while let Some(c) = chars.next() {
|
||||||
|
width += c.width().unwrap_or(0) as u16;
|
||||||
|
if width > max {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return width
|
||||||
|
}
|
||||||
|
|
||||||
/// Trim string with [unicode_width].
|
/// Trim string with [unicode_width].
|
||||||
pub fn trim_string (max_width: usize, input: impl AsRef<str>) -> String {
|
pub fn trim_string (max_width: usize, input: impl AsRef<str>) -> String {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::thread::{spawn, JoinHandle};
|
use std::thread::{spawn, JoinHandle};
|
||||||
|
use unicode_width::*;
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct TuiOut {
|
pub struct TuiOut {
|
||||||
pub buffer: Buffer,
|
pub buffer: Buffer,
|
||||||
|
|
@ -10,8 +11,12 @@ impl Output for TuiOut {
|
||||||
type Unit = u16;
|
type Unit = u16;
|
||||||
type Size = [Self::Unit;2];
|
type Size = [Self::Unit;2];
|
||||||
type Area = [Self::Unit;4];
|
type Area = [Self::Unit;4];
|
||||||
#[inline] fn area (&self) -> [u16;4] { self.area }
|
#[inline] fn area (&self) -> [u16;4] {
|
||||||
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
|
self.area
|
||||||
|
}
|
||||||
|
#[inline] fn area_mut (&mut self) -> &mut [u16;4] {
|
||||||
|
&mut self.area
|
||||||
|
}
|
||||||
#[inline] fn place <'t, T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &'t T) {
|
#[inline] fn place <'t, T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &'t T) {
|
||||||
let last = self.area();
|
let last = self.area();
|
||||||
*self.area_mut() = area;
|
*self.area_mut() = area;
|
||||||
|
|
@ -63,10 +68,32 @@ impl TuiOut {
|
||||||
pub fn blit (
|
pub fn blit (
|
||||||
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
|
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
|
||||||
) {
|
) {
|
||||||
let text = text.as_ref();
|
let text = text.as_ref();
|
||||||
let buf = &mut self.buffer;
|
let buf = &mut self.buffer;
|
||||||
|
let style = style.unwrap_or(Style::default());
|
||||||
if x < buf.area.width && y < buf.area.height {
|
if x < buf.area.width && y < buf.area.height {
|
||||||
buf.set_string(x, y, text, style.unwrap_or(Style::default()));
|
buf.set_string(x, y, text, style);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Write a line of text
|
||||||
|
///
|
||||||
|
/// TODO: do a paragraph (handle newlines)
|
||||||
|
pub fn text (&mut self, text: &impl AsRef<str>, x0: u16, y: u16, max_width: u16) {
|
||||||
|
let text = text.as_ref();
|
||||||
|
let buf = &mut self.buffer;
|
||||||
|
let mut string_width: u16 = 0;
|
||||||
|
for character in text.chars() {
|
||||||
|
let x = x0 + string_width;
|
||||||
|
let character_width = character.width().unwrap_or(0) as u16;
|
||||||
|
string_width += character_width;
|
||||||
|
if string_width > max_width {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if let Some(cell) = buf.cell_mut(ratatui::prelude::Position { x, y }) {
|
||||||
|
cell.set_char(character);
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {
|
pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue