mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-08-28 05:06:56 +02:00
40 lines
1.4 KiB
Rust
40 lines
1.4 KiB
Rust
use crate::*;
|
|
|
|
fn_kw_layout!(kw_when |state, output, expr| {
|
|
let thunk = draw(move|screen|ok_flat(expr.nth(2)?.map(|x: &'a str|state.interpret(screen, x))));
|
|
ok_flat(matches!(expr.head()?, Some("when")).then(||{
|
|
when(state.namespace(&expr.nth(1)?)?.unwrap(), thunk) .draw(output)
|
|
}))
|
|
});
|
|
|
|
/// Only render when condition is true.
|
|
///
|
|
/// ```
|
|
/// # use ::tengri::*; fn test <'a> () -> impl Draw<'a, Tui> {
|
|
/// when(true, "Yes")
|
|
/// # }
|
|
/// ```
|
|
pub const fn when <'a, T: Screen> (condition: bool, item: impl Draw<'a, T>) -> impl Draw<'a, T> {
|
|
draw(move|to: &mut T|if condition { item.draw(to) } else { Ok(Default::default()) })
|
|
}
|
|
|
|
fn_kw_layout!(kw_either |state, output, expr| {
|
|
let thunk_a = draw(move|screen|ok_flat(expr.nth(2)?.map(|x: &'a str|state.interpret(screen, x))));
|
|
let thunk_b = draw(move|screen|ok_flat(expr.nth(3)?.map(|x: &'a str|state.interpret(screen, x))));
|
|
ok_flat(matches!(expr.head()?, Some("either")).then(||{
|
|
either(state.namespace(&expr.nth(1)?)?.unwrap(), thunk_a, thunk_b).draw(output)
|
|
}))
|
|
});
|
|
|
|
/// Render one thing if a condition is true and another false.
|
|
///
|
|
/// ```
|
|
/// # use ::tengri::*; fn test <'a> () -> impl Draw<'a, Tui> {
|
|
/// either(true, "Yes", "No")
|
|
/// # }
|
|
/// ```
|
|
pub const fn either <'a, T: Screen> (
|
|
condition: bool, a: impl Draw<'a, T>, b: impl Draw<'a, T>
|
|
) -> impl Draw<'a, T> {
|
|
draw(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) })
|
|
}
|