mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-05-30 19:43:38 +02:00
all possible insanities simultaneously.
but at least deleting more than i'm writing (hopefully!)
This commit is contained in:
parent
9dbf4fcab5
commit
5d627f7669
5 changed files with 868 additions and 815 deletions
63
src/text.rs
63
src/text.rs
|
|
@ -1,4 +1,4 @@
|
|||
use crate::draw::Draw;
|
||||
use crate::{Usually, draw::{Draw, WH}};
|
||||
pub(crate) use ::unicode_width::*;
|
||||
|
||||
/// Displays an owned [str]-like with fixed maximum width.
|
||||
|
|
@ -46,37 +46,60 @@ pub(crate) fn width_chars_max (max: u16, text: impl AsRef<str>) -> u16 {
|
|||
#[cfg(feature = "term")] mod impl_term {
|
||||
use super::*;
|
||||
use crate::draw::XYWH;
|
||||
use ratatui::prelude::{Buffer, Position};
|
||||
impl<'a, T> Draw<Buffer> for TrimStringRef<'a, T> {
|
||||
fn draw (&self, to: &mut Buffer) {
|
||||
let XYWH(x, y, w, ..) = XYWH(to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h());
|
||||
to.text(&self, x, y, w)
|
||||
}
|
||||
use crate::term::Tui;
|
||||
use ratatui::prelude::Position;
|
||||
|
||||
impl Draw<Tui> for str {
|
||||
fn draw (&self, to: &mut Tui) -> Usually<WH<u16>> { todo!() }
|
||||
}
|
||||
impl<T> Draw<Buffer> for TrimString<T> {
|
||||
fn draw (&self, to: &mut Buffer) { self.as_ref().draw(to) }
|
||||
|
||||
impl<T: AsRef<str>> Draw<Tui> for TrimString<T> {
|
||||
fn draw (&self, to: &mut Tui) -> Usually<WH<u16>> { self.as_ref().draw(to) }
|
||||
}
|
||||
impl<'a, T: AsRef<str>> Draw<Buffer> for TrimString<T> {
|
||||
fn draw (&self, to: &mut Buffer) { Draw::draw(&self.as_ref(), to) }
|
||||
}
|
||||
impl<T: AsRef<str>> Draw<Buffer> for TrimStringRef<'_, T> {
|
||||
fn draw (&self, target: &mut Buffer) {
|
||||
let area = target.area();
|
||||
let mut buf = target.buffer.write().unwrap();
|
||||
|
||||
impl<T: AsRef<str>> Draw<Tui> for TrimStringRef<'_, T> {
|
||||
fn draw (&self, to: &mut Tui) -> Usually<WH<u16>> {
|
||||
let area = to.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) = buf.cell_mut(Position {
|
||||
x: area.x() + width - 1,
|
||||
y: area.y()
|
||||
}) {
|
||||
let x = area.x() + width - 1;
|
||||
let y = area.y();
|
||||
let pos = Position { x, y };
|
||||
if let Some(cell) = to.cell_mut(pos) {
|
||||
cell.set_char(c);
|
||||
}
|
||||
width += c.width().unwrap_or(0) as u16;
|
||||
}
|
||||
let XYWH(x, y, w, ..) = XYWH(to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h());
|
||||
to.text(&self, x, y, w)
|
||||
}
|
||||
}
|
||||
|
||||
impl Tui {
|
||||
/// Write a line of text
|
||||
///
|
||||
/// TODO: do a paragraph (handle newlines)
|
||||
fn text (&mut self, text: &impl AsRef<str>, x0: u16, y: u16, max_width: u16) {
|
||||
let text = text.as_ref();
|
||||
let buf = 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.write().unwrap().cell_mut(ratatui::prelude::Position { x, y }) {
|
||||
cell.set_char(character);
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue