rustom 0.1.0

A library for building rich, stateful terminal user interfaces and dashboards.
Documentation
//! Styled text primitives. A `Span` is a run of text with one style, a
//! `Line` is a sequence of spans laid out on a single row, and `Text` is a
//! sequence of lines making up a paragraph.

use std::borrow::Cow;

use crate::style::Style;
use unicode_width::UnicodeWidthStr;

/// Horizontal text alignment within its containing area.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Alignment {
    #[default]
    Left,
    Center,
    Right,
}

/// A run of text sharing a single style.
#[derive(Debug, Clone, PartialEq)]
pub struct Span<'a> {
    pub content: Cow<'a, str>,
    pub style: Style,
}

impl<'a> Span<'a> {
    pub fn raw<T>(content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span {
            content: content.into(),
            style: Style::default(),
        }
    }

    pub fn styled<T>(content: T, style: Style) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span {
            content: content.into(),
            style,
        }
    }

    pub fn width(&self) -> usize {
        UnicodeWidthStr::width(self.content.as_ref())
    }
}

impl<'a> From<&'a str> for Span<'a> {
    fn from(s: &'a str) -> Span<'a> {
        Span::raw(s)
    }
}

impl<'a> From<String> for Span<'a> {
    fn from(s: String) -> Span<'a> {
        Span::raw(s)
    }
}

/// A single row of styled text made up of one or more spans.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Line<'a> {
    pub spans: Vec<Span<'a>>,
    pub alignment: Option<Alignment>,
}

impl<'a> Line<'a> {
    pub fn raw<T>(content: T) -> Line<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Line {
            spans: vec![Span::raw(content)],
            alignment: None,
        }
    }

    pub fn styled<T>(content: T, style: Style) -> Line<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Line {
            spans: vec![Span::styled(content, style)],
            alignment: None,
        }
    }

    pub fn from_spans(spans: Vec<Span<'a>>) -> Line<'a> {
        Line {
            spans,
            alignment: None,
        }
    }

    pub fn alignment(mut self, alignment: Alignment) -> Line<'a> {
        self.alignment = Some(alignment);
        self
    }

    pub fn width(&self) -> usize {
        self.spans.iter().map(Span::width).sum()
    }
}

impl<'a> From<&'a str> for Line<'a> {
    fn from(s: &'a str) -> Line<'a> {
        Line::raw(s)
    }
}

impl<'a> From<String> for Line<'a> {
    fn from(s: String) -> Line<'a> {
        Line::raw(s)
    }
}

impl<'a> From<Span<'a>> for Line<'a> {
    fn from(span: Span<'a>) -> Line<'a> {
        Line {
            spans: vec![span],
            alignment: None,
        }
    }
}

/// A block of text made up of multiple lines, used by widgets such as
/// `Paragraph`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Text<'a> {
    pub lines: Vec<Line<'a>>,
}

impl<'a> Text<'a> {
    pub fn raw<T>(content: T) -> Text<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        let content = content.into();
        let lines = match content {
            Cow::Borrowed(s) => s.lines().map(Line::from).collect(),
            Cow::Owned(s) => s.lines().map(|l| Line::from(l.to_string())).collect(),
        };
        Text { lines }
    }

    pub fn from_lines(lines: Vec<Line<'a>>) -> Text<'a> {
        Text { lines }
    }

    pub fn height(&self) -> usize {
        self.lines.len()
    }

    pub fn width(&self) -> usize {
        self.lines.iter().map(Line::width).max().unwrap_or(0)
    }
}

impl<'a> From<&'a str> for Text<'a> {
    fn from(s: &'a str) -> Text<'a> {
        Text::raw(s)
    }
}

impl<'a> From<String> for Text<'a> {
    fn from(s: String) -> Text<'a> {
        Text::raw(s)
    }
}

impl<'a> From<Line<'a>> for Text<'a> {
    fn from(line: Line<'a>) -> Text<'a> {
        Text { lines: vec![line] }
    }
}