pub mod ansi;
pub mod cursup;
pub mod gradient;
pub mod markdown;
use crate::style::Style;
use crate::utils::span::{IndexedCow, IndexedSpan, Span, SpannedStr, SpannedString, SpannedText};
use unicode_width::UnicodeWidthStr;
pub type StyledString = SpannedString<Style>;
pub type StyledStr<'a> = SpannedStr<'a, Style>;
pub type StyledIndexedSpan = IndexedSpan<Style>;
pub type StyledSpan<'a> = Span<'a, Style>;
pub struct PlainStr<'a> {
source: &'a str,
span: IndexedSpan<Style>,
}
impl<'a> SpannedText for PlainStr<'a> {
type S = IndexedSpan<Style>;
fn source(&self) -> &str {
self.source
}
fn spans(&self) -> &[Self::S] {
std::slice::from_ref(&self.span)
}
}
impl<'a> PlainStr<'a> {
pub fn new(source: &'a str) -> Self {
Self::new_with_width(source, source.width())
}
pub const fn new_with_width(source: &'a str, width: usize) -> Self {
let span = IndexedSpan {
content: IndexedCow::Borrowed {
start: 0,
end: source.len(),
},
attr: Style::none(),
width,
};
Self { source, span }
}
pub fn as_styled_str(&self) -> StyledStr {
StyledStr::from_spanned_text(self)
}
}
impl SpannedString<Style> {
pub fn plain<S>(content: S) -> Self
where
S: Into<String>,
{
Self::styled(content, Style::none())
}
pub fn styled<S, T>(content: S, style: T) -> Self
where
S: Into<String>,
T: Into<Style>,
{
let content = content.into();
let style = style.into();
Self::single_span(content, style)
}
pub fn append_plain<S>(&mut self, text: S)
where
S: Into<String>,
{
self.append(Self::plain(text));
}
pub fn append_styled<S, T>(&mut self, text: S, style: T)
where
S: Into<String>,
T: Into<Style>,
{
self.append(Self::styled(text, style));
}
}