mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-02-21 10:39:03 +01:00
638 lines
26 KiB
Rust
638 lines
26 KiB
Rust
use crate::*;
|
|
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
|
use rand::{thread_rng, distributions::uniform::UniformSampler};
|
|
|
|
impl Tui {
|
|
/// Create and launch a terminal user interface.
|
|
pub fn run <T: Send + Sync + Handle<TuiIn> + Draw<TuiOut> + 'static> (
|
|
state: &Arc<RwLock<T>>
|
|
) -> Usually<Arc<RwLock<Self>>> {
|
|
tui_run(state)
|
|
}
|
|
/// True if done
|
|
pub fn exited (&self) -> bool { self.exited.fetch_and(true, Relaxed) }
|
|
/// Prepare before run
|
|
pub fn setup (&mut self) -> Usually<()> { tui_setup(&mut self.backend) }
|
|
/// Clean up after run
|
|
pub fn teardown (&mut self) -> Usually<()> { tui_teardown(&mut self.backend) }
|
|
/// Apply changes to the display buffer.
|
|
pub fn flip (&mut self, mut new_buffer: Buffer, size: ratatui::prelude::Rect) -> Buffer {
|
|
let Self { buffer, backend, .. } = self;
|
|
tui_resized(&mut backend, &mut buffer, size);
|
|
tui_redrawn(&mut backend, &mut buffer, &mut new_buffer);
|
|
buffer
|
|
}
|
|
}
|
|
impl Input for TuiIn {
|
|
type Event = TuiEvent;
|
|
type Handled = bool;
|
|
fn event (&self) -> &TuiEvent { &self.event }
|
|
fn done (&self) { self.exited.store(true, Relaxed); }
|
|
fn is_done (&self) -> bool { self.exited.fetch_and(true, Relaxed) }
|
|
}
|
|
impl Ord for TuiEvent {
|
|
fn cmp (&self, other: &Self) -> std::cmp::Ordering {
|
|
self.partial_cmp(other)
|
|
.unwrap_or_else(||format!("{:?}", self).cmp(&format!("{other:?}"))) // FIXME perf
|
|
}
|
|
}
|
|
impl TuiEvent {
|
|
pub fn from_crossterm (event: Event) -> Self { Self(event) }
|
|
#[cfg(feature = "dsl")] pub fn from_dsl (dsl: impl Language) -> Perhaps<Self> {
|
|
Ok(TuiKey::from_dsl(dsl)?.to_crossterm().map(Self))
|
|
}
|
|
}
|
|
impl TuiKey {
|
|
const SPLIT: char = '/';
|
|
#[cfg(feature = "dsl")] pub fn from_dsl (dsl: impl Language) -> Usually<Self> {
|
|
if let Some(word) = dsl.word()? {
|
|
let word = word.trim();
|
|
Ok(if word == ":char" {
|
|
Self(None, KeyModifiers::NONE)
|
|
} else if word.chars().nth(0) == Some('@') {
|
|
let mut key = None;
|
|
let mut modifiers = KeyModifiers::NONE;
|
|
let mut tokens = word[1..].split(Self::SPLIT).peekable();
|
|
while let Some(token) = tokens.next() {
|
|
if tokens.peek().is_some() {
|
|
match token {
|
|
"ctrl" | "Ctrl" | "c" | "C" => modifiers |= KeyModifiers::CONTROL,
|
|
"alt" | "Alt" | "m" | "M" => modifiers |= KeyModifiers::ALT,
|
|
"shift" | "Shift" | "s" | "S" => {
|
|
modifiers |= KeyModifiers::SHIFT;
|
|
// + TODO normalize character case, BackTab, etc.
|
|
},
|
|
_ => panic!("unknown modifier {token}"),
|
|
}
|
|
} else {
|
|
key = if token.len() == 1 {
|
|
Some(KeyCode::Char(token.chars().next().unwrap()))
|
|
} else {
|
|
Some(named_key(token).unwrap_or_else(||panic!("unknown character {token}")))
|
|
}
|
|
}
|
|
}
|
|
Self(key, modifiers)
|
|
} else {
|
|
return Err(format!("TuiKey: unexpected: {word}").into())
|
|
})
|
|
} else {
|
|
return Err(format!("TuiKey: unspecified").into())
|
|
}
|
|
}
|
|
pub fn to_crossterm (&self) -> Option<Event> {
|
|
self.0.map(|code|Event::Key(KeyEvent {
|
|
code,
|
|
modifiers: self.1,
|
|
kind: KeyEventKind::Press,
|
|
state: KeyEventState::NONE,
|
|
}))
|
|
}
|
|
}
|
|
impl Out for TuiOut {
|
|
type Unit = u16;
|
|
#[inline] fn area (&self) -> XYWH<u16> { self.area }
|
|
#[inline] fn area_mut (&mut self) -> &mut XYWH<u16> { &mut self.area }
|
|
#[inline] fn place_at <'t, T: Draw<Self> + ?Sized> (&mut self, area: XYWH<u16>, content: &'t T) {
|
|
let last = self.area();
|
|
*self.area_mut() = area;
|
|
content.draw(self);
|
|
*self.area_mut() = last;
|
|
}
|
|
}
|
|
impl TuiOut {
|
|
#[inline] pub fn with_rect (&mut self, area: XYWH<u16>) -> &mut Self { self.area = area; self }
|
|
pub fn update (&mut self, area: XYWH<u16>, callback: &impl Fn(&mut Cell, u16, u16)) { tui_update(&mut self.buffer, area, callback); }
|
|
pub fn fill_char (&mut self, area: XYWH<u16>, c: char) { self.update(area, &|cell,_,_|{cell.set_char(c);}) }
|
|
pub fn fill_bg (&mut self, area: XYWH<u16>, color: Color) { self.update(area, &|cell,_,_|{cell.set_bg(color);}) }
|
|
pub fn fill_fg (&mut self, area: XYWH<u16>, color: Color) { self.update(area, &|cell,_,_|{cell.set_fg(color);}) }
|
|
pub fn fill_mod (&mut self, area: XYWH<u16>, on: bool, modifier: Modifier) {
|
|
if on {
|
|
self.update(area, &|cell,_,_|cell.modifier.insert(modifier))
|
|
} else {
|
|
self.update(area, &|cell,_,_|cell.modifier.remove(modifier))
|
|
}
|
|
}
|
|
pub fn fill_bold (&mut self, area: XYWH<u16>, on: bool) { self.fill_mod(area, on, Modifier::BOLD) }
|
|
pub fn fill_reversed (&mut self, area: XYWH<u16>, on: bool) { self.fill_mod(area, on, Modifier::REVERSED) }
|
|
pub fn fill_crossed_out (&mut self, area: XYWH<u16>, on: bool) { self.fill_mod(area, on, Modifier::CROSSED_OUT) }
|
|
pub fn fill_ul (&mut self, area: XYWH<u16>, color: Option<Color>) {
|
|
if let Some(color) = color {
|
|
self.update(area, &|cell,_,_|{
|
|
cell.modifier.insert(ratatui::prelude::Modifier::UNDERLINED);
|
|
cell.underline_color = color;
|
|
})
|
|
} else {
|
|
self.update(area, &|cell,_,_|{
|
|
cell.modifier.remove(ratatui::prelude::Modifier::UNDERLINED);
|
|
})
|
|
}
|
|
}
|
|
pub fn tint_all (&mut self, fg: Color, bg: Color, modifier: Modifier) {
|
|
for cell in self.buffer.content.iter_mut() {
|
|
cell.fg = fg;
|
|
cell.bg = bg;
|
|
cell.modifier = modifier;
|
|
}
|
|
}
|
|
pub fn blit (&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>) {
|
|
let text = text.as_ref();
|
|
let buf = &mut self.buffer;
|
|
let style = style.unwrap_or(Style::default());
|
|
if x < buf.area.width && y < buf.area.height {
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
impl BigBuffer {
|
|
pub fn new (width: usize, height: usize) -> Self {
|
|
Self { width, height, content: vec![Cell::default(); width*height] }
|
|
}
|
|
pub fn get (&self, x: usize, y: usize) -> Option<&Cell> {
|
|
let i = self.index_of(x, y);
|
|
self.content.get(i)
|
|
}
|
|
pub fn get_mut (&mut self, x: usize, y: usize) -> Option<&mut Cell> {
|
|
let i = self.index_of(x, y);
|
|
self.content.get_mut(i)
|
|
}
|
|
pub fn index_of (&self, x: usize, y: usize) -> usize {
|
|
y * self.width + x
|
|
}
|
|
}
|
|
// A single color within item theme parameters, in OKHSL and RGB representations.
|
|
impl ItemColor {
|
|
pub const fn from_rgb (rgb: Color) -> Self {
|
|
Self { rgb, okhsl: Okhsl::new_const(OklabHue::new(0.0), 0.0, 0.0) }
|
|
}
|
|
pub const fn from_okhsl (okhsl: Okhsl<f32>) -> Self {
|
|
Self { rgb: Color::Rgb(0, 0, 0), okhsl }
|
|
}
|
|
pub fn random () -> Self {
|
|
let mut rng = thread_rng();
|
|
let lo = Okhsl::new(-180.0, 0.01, 0.25);
|
|
let hi = Okhsl::new( 180.0, 0.9, 0.5);
|
|
UniformOkhsl::new(lo, hi).sample(&mut rng).into()
|
|
}
|
|
pub fn random_dark () -> Self {
|
|
let mut rng = thread_rng();
|
|
let lo = Okhsl::new(-180.0, 0.025, 0.075);
|
|
let hi = Okhsl::new( 180.0, 0.5, 0.150);
|
|
UniformOkhsl::new(lo, hi).sample(&mut rng).into()
|
|
}
|
|
pub fn random_near (color: Self, distance: f32) -> Self {
|
|
color.mix(Self::random(), distance)
|
|
}
|
|
pub fn mix (&self, other: Self, distance: f32) -> Self {
|
|
if distance > 1.0 { panic!("color mixing takes distance between 0.0 and 1.0"); }
|
|
self.okhsl.mix(other.okhsl, distance).into()
|
|
}
|
|
}
|
|
impl ItemTheme {
|
|
pub const G: [Self;256] = {
|
|
let mut builder = konst::array::ArrayBuilder::new();
|
|
while !builder.is_full() {
|
|
let index = builder.len() as u8;
|
|
let light = (index as f64 * 1.15) as u8;
|
|
let lighter = (index as f64 * 1.7) as u8;
|
|
let lightest = (index as f64 * 1.85) as u8;
|
|
let dark = (index as f64 * 0.9) as u8;
|
|
let darker = (index as f64 * 0.6) as u8;
|
|
let darkest = (index as f64 * 0.3) as u8;
|
|
builder.push(ItemTheme {
|
|
base: ItemColor::from_rgb(Color::Rgb(index, index, index )),
|
|
light: ItemColor::from_rgb(Color::Rgb(light, light, light, )),
|
|
lighter: ItemColor::from_rgb(Color::Rgb(lighter, lighter, lighter, )),
|
|
lightest: ItemColor::from_rgb(Color::Rgb(lightest, lightest, lightest, )),
|
|
dark: ItemColor::from_rgb(Color::Rgb(dark, dark, dark, )),
|
|
darker: ItemColor::from_rgb(Color::Rgb(darker, darker, darker, )),
|
|
darkest: ItemColor::from_rgb(Color::Rgb(darkest, darkest, darkest, )),
|
|
});
|
|
}
|
|
builder.build()
|
|
};
|
|
pub fn random () -> Self { ItemColor::random().into() }
|
|
pub fn random_near (color: Self, distance: f32) -> Self {
|
|
color.base.mix(ItemColor::random(), distance).into()
|
|
}
|
|
pub const G00: Self = {
|
|
let color: ItemColor = ItemColor {
|
|
okhsl: Okhsl { hue: OklabHue::new(0.0), lightness: 0.0, saturation: 0.0 },
|
|
rgb: Color::Rgb(0, 0, 0)
|
|
};
|
|
Self {
|
|
base: color,
|
|
light: color,
|
|
lighter: color,
|
|
lightest: color,
|
|
dark: color,
|
|
darker: color,
|
|
darkest: color,
|
|
}
|
|
};
|
|
pub fn from_tui_color (base: Color) -> Self {
|
|
Self::from_item_color(ItemColor::from_rgb(base))
|
|
}
|
|
pub fn from_item_color (base: ItemColor) -> Self {
|
|
let mut light = base.okhsl;
|
|
light.lightness = (light.lightness * 1.3).min(1.0);
|
|
let mut lighter = light;
|
|
lighter.lightness = (lighter.lightness * 1.3).min(1.0);
|
|
let mut lightest = base.okhsl;
|
|
lightest.lightness = 0.95;
|
|
let mut dark = base.okhsl;
|
|
dark.lightness = (dark.lightness * 0.75).max(0.0);
|
|
dark.saturation = (dark.saturation * 0.75).max(0.0);
|
|
let mut darker = dark;
|
|
darker.lightness = (darker.lightness * 0.66).max(0.0);
|
|
darker.saturation = (darker.saturation * 0.66).max(0.0);
|
|
let mut darkest = darker;
|
|
darkest.lightness = 0.1;
|
|
darkest.saturation = (darkest.saturation * 0.50).max(0.0);
|
|
Self {
|
|
base,
|
|
light: light.into(),
|
|
lighter: lighter.into(),
|
|
lightest: lightest.into(),
|
|
dark: dark.into(),
|
|
darker: darker.into(),
|
|
darkest: darkest.into(),
|
|
}
|
|
}
|
|
}
|
|
impl<T> Phat<T> {
|
|
pub const LO: &'static str = "▄";
|
|
pub const HI: &'static str = "▀";
|
|
/// A phat line
|
|
pub fn lo (fg: Color, bg: Color) -> impl Content<TuiOut> {
|
|
Fixed::Y(1, Tui::fg_bg(fg, bg, Repeat::X(Self::LO)))
|
|
}
|
|
/// A phat line
|
|
pub fn hi (fg: Color, bg: Color) -> impl Content<TuiOut> {
|
|
Fixed::Y(1, Tui::fg_bg(fg, bg, Repeat::X(Self::HI)))
|
|
}
|
|
}
|
|
impl Scrollbar {
|
|
const ICON_DEC_V: &[char] = &['▲'];
|
|
const ICON_INC_V: &[char] = &['▼'];
|
|
const ICON_DEC_H: &[char] = &[' ', '🞀', ' '];
|
|
const ICON_INC_H: &[char] = &[' ', '🞂', ' '];
|
|
}
|
|
impl<'a, T: AsRef<str>> TrimString<T> {
|
|
fn as_ref (&self) -> TrimStringRef<'_, T> { TrimStringRef(self.0, &self.1) }
|
|
}
|
|
impl<O: Out, T: Draw<O>> ErrorBoundary<O, T> {
|
|
pub fn new (content: Perhaps<T>) -> Self { Self(Default::default(), content) }
|
|
}
|
|
mod content {
|
|
use super::*;
|
|
impl<S: BorderStyle, W: Content<TuiOut>> HasContent<TuiOut> for Bordered<S, W> {
|
|
fn content (&self) -> impl Content<TuiOut> {
|
|
Fill::XY(lay!( When::new(self.0, Border(self.0, self.1)), Pad::XY(1, 1, &self.2) ))
|
|
}
|
|
}
|
|
impl<
|
|
A: Content<TuiOut>,
|
|
B: Content<TuiOut>,
|
|
C: Content<TuiOut>,
|
|
> HasContent<TuiOut> for Tryptich<A, B, C> {
|
|
fn content (&self) -> impl Content<TuiOut> {
|
|
let Self { top, h, left: (w_a, ref a), middle: (w_b, ref b), right: (w_c, ref c) } = *self;
|
|
Fixed::Y(h, if top {
|
|
Bsp::a(
|
|
Fill::X(Align::n(Fixed::X(w_b, Align::x(Tui::bg(Color::Reset, b))))),
|
|
Bsp::a(
|
|
Fill::X(Align::nw(Fixed::X(w_a, Tui::bg(Color::Reset, a)))),
|
|
Fill::X(Align::ne(Fixed::X(w_c, Tui::bg(Color::Reset, c)))),
|
|
),
|
|
)
|
|
} else {
|
|
Bsp::a(
|
|
Fill::XY(Align::c(Fixed::X(w_b, Align::x(Tui::bg(Color::Reset, b))))),
|
|
Bsp::a(
|
|
Fill::XY(Align::w(Fixed::X(w_a, Tui::bg(Color::Reset, a)))),
|
|
Fill::XY(Align::e(Fixed::X(w_c, Tui::bg(Color::Reset, c)))),
|
|
),
|
|
)
|
|
})
|
|
}
|
|
}
|
|
impl<T: Content<TuiOut>> HasContent<TuiOut> for Phat<T> {
|
|
fn content (&self) -> impl Content<TuiOut> {
|
|
let [fg, bg, hi, lo] = self.colors;
|
|
let top = Fixed::Y(1, Self::lo(bg, hi));
|
|
let low = Fixed::Y(1, Self::hi(bg, lo));
|
|
let content = Tui::fg_bg(fg, bg, &self.content);
|
|
Min::XY(self.width, self.height, Bsp::s(top, Bsp::n(low, Fill::XY(content))))
|
|
}
|
|
}
|
|
}
|
|
|
|
mod layout {
|
|
use super::*;
|
|
impl<T: Content<TuiOut>> Layout<TuiOut> for Modify<T> {}
|
|
impl<T: Content<TuiOut>> Layout<TuiOut> for Styled<T> {}
|
|
impl Layout<TuiOut> for Repeat<'_> {}
|
|
impl Layout<TuiOut> for &str {
|
|
fn layout (&self, to: XYWH<u16>) -> XYWH<u16> {
|
|
to.centered_xy([width_chars_max(to.w(), self), 1])
|
|
}
|
|
}
|
|
impl Layout<TuiOut> for String {
|
|
fn layout (&self, to: XYWH<u16>) -> XYWH<u16> {
|
|
self.as_str().layout(to)
|
|
}
|
|
}
|
|
impl Layout<TuiOut> for Arc<str> {
|
|
fn layout (&self, to: XYWH<u16>) -> XYWH<u16> {
|
|
self.as_ref().layout(to)
|
|
}
|
|
}
|
|
impl<'a, T: AsRef<str>> Layout<TuiOut> for TrimString<T> {
|
|
fn layout (&self, to: XYWH<u16>) -> XYWH<u16> {
|
|
Layout::layout(&self.as_ref(), to)
|
|
}
|
|
}
|
|
impl<'a, T: AsRef<str>> Layout<TuiOut> for TrimStringRef<'a, T> {
|
|
fn layout (&self, to: XYWH<u16>) -> XYWH<u16> {
|
|
XYWH(to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h())
|
|
}
|
|
}
|
|
}
|
|
|
|
mod draw {
|
|
use super::*;
|
|
|
|
impl<T: Draw<TuiOut>> Draw<TuiOut> for ErrorBoundary<TuiOut, T> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
match self.1.as_ref() {
|
|
Ok(Some(content)) => content.draw(to),
|
|
Ok(None) => to.blit(&"empty?", 0, 0, Some(Style::default().yellow())),
|
|
Err(e) => {
|
|
let err_fg = Color::Rgb(255,224,244);
|
|
let err_bg = Color::Rgb(96,24,24);
|
|
let title = Bsp::e(Tui::bold(true, "oops. "), "rendering failed.");
|
|
let error = Bsp::e("\"why?\" ", Tui::bold(true, format!("{e}")));
|
|
to.place(&Tui::fg_bg(err_fg, err_bg, Bsp::s(title, error)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for u64 {
|
|
fn draw (&self, _to: &mut TuiOut) {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for f64 {
|
|
fn draw (&self, _to: &mut TuiOut) {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for Repeat<'_> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
let XYWH(x, y, w, h) = to.area();
|
|
match self {
|
|
Self::X(c) => {
|
|
for x in x..x+w {
|
|
if let Some(cell) = to.buffer.cell_mut(Position::from((x, y))) {
|
|
cell.set_symbol(&c);
|
|
}
|
|
}
|
|
},
|
|
Self::Y(c) => {
|
|
for y in y..y+h {
|
|
if let Some(cell) = to.buffer.cell_mut(Position::from((x, y))) {
|
|
cell.set_symbol(&c);
|
|
}
|
|
}
|
|
},
|
|
Self::XY(c) => {
|
|
let a = c.len();
|
|
for (_v, y) in (y..y+h).enumerate() {
|
|
for (u, x) in (x..x+w).enumerate() {
|
|
if let Some(cell) = to.buffer.cell_mut(Position::from((x, y))) {
|
|
let u = u % a;
|
|
cell.set_symbol(&c[u..u+1]);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for Scrollbar {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
let XYWH(x1, y1, w, h) = to.area();
|
|
match self {
|
|
Self::X { .. } => {
|
|
let x2 = x1 + w;
|
|
for (i, x) in (x1..=x2).enumerate() {
|
|
if let Some(cell) = to.buffer.cell_mut(Position::from((x, y1))) {
|
|
if i < (Self::ICON_DEC_H.len()) {
|
|
cell.set_fg(Rgb(255, 255, 255));
|
|
cell.set_bg(Rgb(0, 0, 0));
|
|
cell.set_char(Self::ICON_DEC_H[i as usize]);
|
|
} else if i > (w as usize - Self::ICON_INC_H.len()) {
|
|
cell.set_fg(Rgb(255, 255, 255));
|
|
cell.set_bg(Rgb(0, 0, 0));
|
|
cell.set_char(Self::ICON_INC_H[w as usize - i]);
|
|
} else if false {
|
|
cell.set_fg(Rgb(255, 255, 255));
|
|
cell.set_bg(Reset);
|
|
cell.set_char('━');
|
|
} else {
|
|
cell.set_fg(Rgb(0, 0, 0));
|
|
cell.set_bg(Reset);
|
|
cell.set_char('╌');
|
|
}
|
|
}
|
|
}
|
|
},
|
|
Self::Y { .. } => {
|
|
let y2 = y1 + h;
|
|
for (i, y) in (y1..=y2).enumerate() {
|
|
if let Some(cell) = to.buffer.cell_mut(Position::from((x1, y))) {
|
|
if (i as usize) < (Self::ICON_DEC_V.len()) {
|
|
cell.set_fg(Rgb(255, 255, 255));
|
|
cell.set_bg(Rgb(0, 0, 0));
|
|
cell.set_char(Self::ICON_DEC_V[i as usize]);
|
|
} else if (i as usize) > (h as usize - Self::ICON_INC_V.len()) {
|
|
cell.set_fg(Rgb(255, 255, 255));
|
|
cell.set_bg(Rgb(0, 0, 0));
|
|
cell.set_char(Self::ICON_INC_V[h as usize - i]);
|
|
} else if false {
|
|
cell.set_fg(Rgb(255, 255, 255));
|
|
cell.set_bg(Reset);
|
|
cell.set_char('‖'); // ━
|
|
} else {
|
|
cell.set_fg(Rgb(0, 0, 0));
|
|
cell.set_bg(Reset);
|
|
cell.set_char('╎'); // ━
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for &str {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
let XYWH(x, y, w, ..) = self.layout(to.area());
|
|
to.text(&self, x, y, w)
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for String {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
self.as_str().draw(to)
|
|
}
|
|
}
|
|
|
|
impl Draw<TuiOut> for Arc<str> {
|
|
fn draw (&self, to: &mut TuiOut) { self.as_ref().draw(to) }
|
|
}
|
|
|
|
impl<T: Content<TuiOut>> Draw<TuiOut> for Foreground<Color, T> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
let area = self.layout(to.area());
|
|
to.fill_fg(area, self.0);
|
|
to.place_at(area, &self.1);
|
|
}
|
|
}
|
|
|
|
impl<T: Content<TuiOut>> Draw<TuiOut> for Background<Color, T> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
let area = self.layout(to.area());
|
|
to.fill_bg(area, self.0);
|
|
to.place_at(area, &self.1);
|
|
}
|
|
}
|
|
|
|
impl<T: Content<TuiOut>> Draw<TuiOut> for Modify<T> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
to.fill_mod(to.area(), self.0, self.1);
|
|
self.2.draw(to)
|
|
}
|
|
}
|
|
|
|
impl<T: Content<TuiOut>> Draw<TuiOut> for Styled<T> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
to.place(&self.1);
|
|
// TODO write style over area
|
|
}
|
|
}
|
|
|
|
impl<S: BorderStyle> Draw<TuiOut> for Border<S> {
|
|
fn draw (&self, to: &mut TuiOut) {
|
|
let Border(enabled, style) = self;
|
|
if *enabled {
|
|
let area = to.area();
|
|
if area.w() > 0 && area.y() > 0 {
|
|
to.blit(&style.border_nw(), area.x(), area.y(), style.style());
|
|
to.blit(&style.border_ne(), area.x() + area.w() - 1, area.y(), style.style());
|
|
to.blit(&style.border_sw(), area.x(), area.y() + area.h() - 1, style.style());
|
|
to.blit(&style.border_se(), area.x() + area.w() - 1, area.y() + area.h() - 1, style.style());
|
|
for x in area.x()+1..area.x()+area.w()-1 {
|
|
to.blit(&style.border_n(), x, area.y(), style.style());
|
|
to.blit(&style.border_s(), x, area.y() + area.h() - 1, style.style());
|
|
}
|
|
for y in area.y()+1..area.y()+area.h()-1 {
|
|
to.blit(&style.border_w(), area.x(), y, style.style());
|
|
to.blit(&style.border_e(), area.x() + area.w() - 1, y, style.style());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, T: AsRef<str>> Draw<TuiOut> for TrimString<T> {
|
|
fn draw (&self, to: &mut TuiOut) { Draw::draw(&self.as_ref(), to) }
|
|
}
|
|
|
|
impl<T: AsRef<str>> Draw<TuiOut> for TrimStringRef<'_, T> {
|
|
fn draw (&self, target: &mut TuiOut) {
|
|
let area = target.area();
|
|
let mut width: u16 = 1;
|
|
let mut chars = self.1.as_ref().chars();
|
|
while let Some(c) = chars.next() {
|
|
if width > self.0 || width > area.w() {
|
|
break
|
|
}
|
|
if let Some(cell) = target.buffer.cell_mut(Position {
|
|
x: area.x() + width - 1,
|
|
y: area.y()
|
|
}) {
|
|
cell.set_char(c);
|
|
}
|
|
width += c.width().unwrap_or(0) as u16;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// TUI helper defs.
|
|
impl Tui {
|
|
pub const fn fg <T> (color: Color, w: T) -> Foreground<Color, T> { Foreground(color, w) }
|
|
pub const fn bg <T> (color: Color, w: T) -> Background<Color, T> { Background(color, w) }
|
|
pub const fn fg_bg <T> (fg: Color, bg: Color, w: T) -> Background<Color, Foreground<Color, T>> { Background(bg, Foreground(fg, w)) }
|
|
pub const fn modify <T> (enable: bool, modifier: Modifier, w: T) -> Modify<T> { Modify(enable, modifier, w) }
|
|
pub const fn bold <T> (enable: bool, w: T) -> Modify<T> { Self::modify(enable, Modifier::BOLD, w) }
|
|
pub const fn border <S, T> (enable: bool, style: S, w: T) -> Bordered<S, T> { Bordered(enable, style, w) }
|
|
|
|
pub const fn null () -> Color { Color::Reset }
|
|
pub const fn red () -> Color { Color::Rgb(255,0, 0) }
|
|
pub const fn orange () -> Color { Color::Rgb(255,128,0) }
|
|
pub const fn yellow () -> Color { Color::Rgb(255,255,0) }
|
|
pub const fn brown () -> Color { Color::Rgb(128,255,0) }
|
|
pub const fn green () -> Color { Color::Rgb(0,255,0) }
|
|
pub const fn electric () -> Color { Color::Rgb(0,255,128) }
|
|
pub const fn g (g: u8) -> Color { Color::Rgb(g, g, g) }
|
|
//fn bg0 () -> Color { Color::Rgb(20, 20, 20) }
|
|
//fn bg () -> Color { Color::Rgb(28, 35, 25) }
|
|
//fn border_bg () -> Color { Color::Rgb(40, 50, 30) }
|
|
//fn border_fg (f: bool) -> Color { if f { Self::bo1() } else { Self::bo2() } }
|
|
//fn title_fg (f: bool) -> Color { if f { Self::ti1() } else { Self::ti2() } }
|
|
//fn separator_fg (_: bool) -> Color { Color::Rgb(0, 0, 0) }
|
|
//fn mode_bg () -> Color { Color::Rgb(150, 160, 90) }
|
|
//fn mode_fg () -> Color { Color::Rgb(255, 255, 255) }
|
|
//fn status_bar_bg () -> Color { Color::Rgb(28, 35, 25) }
|
|
//fn bo1 () -> Color { Color::Rgb(100, 110, 40) }
|
|
//fn bo2 () -> Color { Color::Rgb(70, 80, 50) }
|
|
//fn ti1 () -> Color { Color::Rgb(150, 160, 90) }
|
|
//fn ti2 () -> Color { Color::Rgb(120, 130, 100) }
|
|
}
|
|
|
|
from!(BigBuffer: |size:(usize, usize)| Self::new(size.0, size.1));
|
|
from!(ItemTheme: |base: Color| Self::from_tui_color(base));
|
|
from!(ItemTheme: |base: ItemColor| Self::from_item_color(base));
|
|
from!(ItemColor: |okhsl: Okhsl<f32>| Self { okhsl, rgb: okhsl_to_rgb(okhsl) });
|
|
from!(ItemColor: |rgb: Color| Self { rgb, okhsl: rgb_to_okhsl(rgb) });
|
|
impl_debug!(BigBuffer |self, f| {
|
|
write!(f, "[BB {}x{} ({})]", self.width, self.height, self.content.len())
|
|
});
|