2024-06-28 23:40:35 +02:00
|
|
|
use crate::{
|
|
|
|
config::Style,
|
|
|
|
print::{Grapheme, UnicodeWidthGraphemes},
|
|
|
|
};
|
|
|
|
use nu_ansi_term::{AnsiString, Style as AnsiStyle};
|
2021-09-13 01:59:15 +02:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2019-04-12 23:49:20 +02:00
|
|
|
|
2021-09-13 01:59:15 +02:00
|
|
|
/// Type that holds text with an associated style
|
2020-04-06 19:16:18 +02:00
|
|
|
#[derive(Clone)]
|
2021-09-13 01:59:15 +02:00
|
|
|
pub struct TextSegment {
|
2019-05-01 22:34:24 +02:00
|
|
|
/// The segment's style. If None, will inherit the style of the module containing it.
|
2021-09-13 01:59:15 +02:00
|
|
|
style: Option<Style>,
|
2019-05-01 22:34:24 +02:00
|
|
|
|
|
|
|
/// The string value of the current segment.
|
2021-09-13 01:59:15 +02:00
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextSegment {
|
2022-09-04 18:44:54 +02:00
|
|
|
// Returns the AnsiString of the segment value
|
2024-06-28 23:40:35 +02:00
|
|
|
fn ansi_string(&self, prev: Option<&AnsiStyle>) -> AnsiString {
|
2021-09-13 01:59:15 +02:00
|
|
|
match self.style {
|
2024-06-28 23:40:35 +02:00
|
|
|
Some(style) => style.to_ansi_style(prev).paint(&self.value),
|
2022-09-04 18:44:54 +02:00
|
|
|
None => AnsiString::from(&self.value),
|
2021-09-13 01:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Type that holds fill text with an associated style
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FillSegment {
|
|
|
|
/// The segment's style. If None, will inherit the style of the module containing it.
|
|
|
|
style: Option<Style>,
|
|
|
|
|
|
|
|
/// The string value of the current segment.
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FillSegment {
|
2022-09-04 18:44:54 +02:00
|
|
|
// Returns the AnsiString of the segment value, not including its prefix and suffix
|
2024-06-28 23:40:35 +02:00
|
|
|
pub fn ansi_string(&self, width: Option<usize>, prev: Option<&AnsiStyle>) -> AnsiString {
|
2021-09-13 01:59:15 +02:00
|
|
|
let s = match width {
|
|
|
|
Some(w) => self
|
|
|
|
.value
|
|
|
|
.graphemes(true)
|
|
|
|
.cycle()
|
|
|
|
.scan(0usize, |len, g| {
|
|
|
|
*len += Grapheme(g).width();
|
|
|
|
if *len <= w {
|
|
|
|
Some(g)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<String>(),
|
|
|
|
None => String::from(&self.value),
|
|
|
|
};
|
|
|
|
match self.style {
|
2024-06-28 23:40:35 +02:00
|
|
|
Some(style) => style.to_ansi_style(prev).paint(s),
|
2022-09-04 18:44:54 +02:00
|
|
|
None => AnsiString::from(s),
|
2021-09-13 01:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod fill_seg_tests {
|
|
|
|
use super::FillSegment;
|
2022-09-04 18:44:54 +02:00
|
|
|
use nu_ansi_term::Color;
|
2021-09-13 01:59:15 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ansi_string_width() {
|
|
|
|
let width: usize = 10;
|
|
|
|
let style = Color::Blue.bold();
|
|
|
|
|
|
|
|
let inputs = vec![
|
|
|
|
(".", ".........."),
|
|
|
|
(".:", ".:.:.:.:.:"),
|
|
|
|
("-:-", "-:--:--:--"),
|
|
|
|
("🟦", "🟦🟦🟦🟦🟦"),
|
|
|
|
("🟢🔵🟡", "🟢🔵🟡🟢🔵"),
|
|
|
|
];
|
|
|
|
|
2022-05-23 12:58:27 +02:00
|
|
|
for (text, expected) in &inputs {
|
2021-09-13 01:59:15 +02:00
|
|
|
let f = FillSegment {
|
|
|
|
value: String::from(*text),
|
2024-06-28 23:40:35 +02:00
|
|
|
style: Some(style.into()),
|
2021-09-13 01:59:15 +02:00
|
|
|
};
|
2024-06-28 23:40:35 +02:00
|
|
|
let actual = f.ansi_string(Some(width), None);
|
2021-09-13 01:59:15 +02:00
|
|
|
assert_eq!(style.paint(*expected), actual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A segment is a styled text chunk ready for printing.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum Segment {
|
|
|
|
Text(TextSegment),
|
|
|
|
Fill(FillSegment),
|
|
|
|
LineTerm,
|
2019-04-12 23:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Segment {
|
2022-05-23 12:58:27 +02:00
|
|
|
/// Creates new segments from a text with a style; breaking out `LineTerminators`.
|
|
|
|
pub fn from_text<T>(style: Option<Style>, value: T) -> Vec<Self>
|
2021-09-13 01:59:15 +02:00
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
2022-05-23 12:58:27 +02:00
|
|
|
let mut segs: Vec<Self> = Vec::new();
|
2021-09-13 01:59:15 +02:00
|
|
|
value.into().split(LINE_TERMINATOR).for_each(|s| {
|
|
|
|
if !segs.is_empty() {
|
2022-05-23 12:58:27 +02:00
|
|
|
segs.push(Self::LineTerm)
|
2021-09-13 01:59:15 +02:00
|
|
|
}
|
2022-05-23 12:58:27 +02:00
|
|
|
segs.push(Self::Text(TextSegment {
|
2021-09-13 01:59:15 +02:00
|
|
|
value: String::from(s),
|
|
|
|
style,
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
segs
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new fill segment
|
|
|
|
pub fn fill<T>(style: Option<Style>, value: T) -> Self
|
2019-04-13 01:11:40 +02:00
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::Fill(FillSegment {
|
2020-08-17 04:16:05 +02:00
|
|
|
style,
|
|
|
|
value: value.into(),
|
2021-09-13 01:59:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-28 23:40:35 +02:00
|
|
|
pub fn style(&self) -> Option<AnsiStyle> {
|
2021-09-13 01:59:15 +02:00
|
|
|
match self {
|
2024-07-25 22:17:06 +02:00
|
|
|
Self::Fill(fs) => fs.style.map(|cs| cs.to_ansi_style(None)),
|
|
|
|
Self::Text(ts) => ts.style.map(|cs| cs.to_ansi_style(None)),
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::LineTerm => None,
|
2021-09-13 01:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_style_if_empty(&mut self, style: Option<Style>) {
|
|
|
|
match self {
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::Fill(fs) => {
|
2021-09-13 01:59:15 +02:00
|
|
|
if fs.style.is_none() {
|
|
|
|
fs.style = style
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::Text(ts) => {
|
2021-09-13 01:59:15 +02:00
|
|
|
if ts.style.is_none() {
|
|
|
|
ts.style = style
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::LineTerm => {}
|
2021-09-13 01:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> &str {
|
|
|
|
match self {
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::Fill(fs) => &fs.value,
|
|
|
|
Self::Text(ts) => &ts.value,
|
|
|
|
Self::LineTerm => LINE_TERMINATOR_STRING,
|
2020-08-17 04:16:05 +02:00
|
|
|
}
|
2020-01-02 05:19:08 +01:00
|
|
|
}
|
|
|
|
|
2022-09-04 18:44:54 +02:00
|
|
|
// Returns the AnsiString of the segment value, not including its prefix and suffix
|
2024-06-28 23:40:35 +02:00
|
|
|
pub fn ansi_string(&self, prev: Option<&AnsiStyle>) -> AnsiString {
|
2021-09-13 01:59:15 +02:00
|
|
|
match self {
|
2024-06-28 23:40:35 +02:00
|
|
|
Self::Fill(fs) => fs.ansi_string(None, prev),
|
|
|
|
Self::Text(ts) => ts.ansi_string(prev),
|
2022-09-04 18:44:54 +02:00
|
|
|
Self::LineTerm => AnsiString::from(LINE_TERMINATOR_STRING),
|
2021-09-13 01:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn width_graphemes(&self) -> usize {
|
|
|
|
match self {
|
2022-05-23 12:58:27 +02:00
|
|
|
Self::Fill(fs) => fs.value.width_graphemes(),
|
|
|
|
Self::Text(ts) => ts.value.width_graphemes(),
|
|
|
|
Self::LineTerm => 0,
|
2019-04-12 23:49:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 01:59:15 +02:00
|
|
|
const LINE_TERMINATOR: char = '\n';
|
|
|
|
const LINE_TERMINATOR_STRING: &str = "\n";
|