0.6.0: add trim_string

This commit is contained in:
🪞👃🪞 2025-04-06 18:59:50 +03:00
parent ea01deb854
commit b8ad5dae9f
4 changed files with 26 additions and 12 deletions

View file

@ -1,12 +1,10 @@
use crate::*;
use ratatui::prelude::Position;
macro_rules! impl_content_layout_render {
(
$Output:ty: |$self:ident: $Struct:ty, $to:ident|
layout = $layout:expr;
render = $render:expr
) => {
($Output:ty: |$self:ident: $Struct:ty, $to:ident|
layout = $layout:expr;
render = $render:expr) =>
{
impl Content<$Output> for $Struct {
fn layout (&$self, $to: [u16;4]) -> [u16;4] { $layout }
fn render (&$self, $to: &mut $Output) { $render }

View file

@ -20,6 +20,22 @@ impl_content_layout_render!(TuiOut: |self: Arc<str>, to|
layout = to.center_xy([self.chars().count() as u16, 1]);
render = to.blit(self, to.area.x(), to.area.y(), None));
/// Trim string with [unicode_width].
pub fn trim_string (max_width: usize, input: impl AsRef<str>) -> String {
let input = input.as_ref();
let mut output = Vec::with_capacity(input.len());
let mut width: usize = 1;
let mut chars = input.chars();
while let Some(c) = chars.next() {
if width > max_width {
break
}
output.push(c);
width += c.width().unwrap_or(0);
}
return output.into_iter().collect()
}
/// Displays an owned [str]-like with fixed maximum width.
///
/// Width is computed using [unicode_width].