1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-11 15:51:01 +00:00
starship/src/segment.rs

187 lines
5.0 KiB
Rust
Raw Normal View History

use crate::print::{Grapheme, UnicodeWidthGraphemes};
use nu_ansi_term::{AnsiString, Style};
2019-05-01 20:34:24 +00:00
use std::fmt;
use unicode_segmentation::UnicodeSegmentation;
2019-04-12 21:49:20 +00:00
/// Type that holds text with an associated style
#[derive(Clone)]
pub struct TextSegment {
2019-05-01 20:34:24 +00:00
/// The segment's style. If None, will inherit the style of the module containing it.
style: Option<Style>,
2019-05-01 20:34:24 +00:00
/// The string value of the current segment.
value: String,
}
impl TextSegment {
// Returns the AnsiString of the segment value
fn ansi_string(&self) -> AnsiString {
match self.style {
Some(style) => style.paint(&self.value),
None => AnsiString::from(&self.value),
}
}
}
/// 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 {
// Returns the AnsiString of the segment value, not including its prefix and suffix
pub fn ansi_string(&self, width: Option<usize>) -> AnsiString {
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 {
Some(style) => style.paint(s),
None => AnsiString::from(s),
}
}
}
#[cfg(test)]
mod fill_seg_tests {
use super::FillSegment;
use nu_ansi_term::Color;
#[test]
fn ansi_string_width() {
let width: usize = 10;
let style = Color::Blue.bold();
let inputs = vec![
(".", ".........."),
(".:", ".:.:.:.:.:"),
("-:-", "-:--:--:--"),
("🟦", "🟦🟦🟦🟦🟦"),
("🟢🔵🟡", "🟢🔵🟡🟢🔵"),
];
2022-05-23 10:58:27 +00:00
for (text, expected) in &inputs {
let f = FillSegment {
value: String::from(*text),
style: Some(style),
};
let actual = f.ansi_string(Some(width));
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 21:49:20 +00:00
}
impl Segment {
2022-05-23 10:58:27 +00: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>
where
T: Into<String>,
{
2022-05-23 10:58:27 +00:00
let mut segs: Vec<Self> = Vec::new();
value.into().split(LINE_TERMINATOR).for_each(|s| {
if !segs.is_empty() {
2022-05-23 10:58:27 +00:00
segs.push(Self::LineTerm)
}
2022-05-23 10:58:27 +00:00
segs.push(Self::Text(TextSegment {
value: String::from(s),
style,
}))
});
segs
}
/// Creates a new fill segment
pub fn fill<T>(style: Option<Style>, value: T) -> Self
2019-04-12 23:11:40 +00:00
where
T: Into<String>,
{
2022-05-23 10:58:27 +00:00
Self::Fill(FillSegment {
style,
value: value.into(),
})
}
pub fn style(&self) -> Option<Style> {
match self {
2022-05-23 10:58:27 +00:00
Self::Fill(fs) => fs.style,
Self::Text(ts) => ts.style,
Self::LineTerm => None,
}
}
pub fn set_style_if_empty(&mut self, style: Option<Style>) {
match self {
2022-05-23 10:58:27 +00:00
Self::Fill(fs) => {
if fs.style.is_none() {
fs.style = style
}
}
2022-05-23 10:58:27 +00:00
Self::Text(ts) => {
if ts.style.is_none() {
ts.style = style
}
}
2022-05-23 10:58:27 +00:00
Self::LineTerm => {}
}
}
pub fn value(&self) -> &str {
match self {
2022-05-23 10:58:27 +00:00
Self::Fill(fs) => &fs.value,
Self::Text(ts) => &ts.value,
Self::LineTerm => LINE_TERMINATOR_STRING,
}
}
// Returns the AnsiString of the segment value, not including its prefix and suffix
pub fn ansi_string(&self) -> AnsiString {
match self {
2022-05-23 10:58:27 +00:00
Self::Fill(fs) => fs.ansi_string(None),
Self::Text(ts) => ts.ansi_string(),
Self::LineTerm => AnsiString::from(LINE_TERMINATOR_STRING),
}
}
pub fn width_graphemes(&self) -> usize {
match self {
2022-05-23 10:58:27 +00:00
Self::Fill(fs) => fs.value.width_graphemes(),
Self::Text(ts) => ts.value.width_graphemes(),
Self::LineTerm => 0,
2019-04-12 21:49:20 +00:00
}
}
}
const LINE_TERMINATOR: char = '\n';
const LINE_TERMINATOR_STRING: &str = "\n";
impl fmt::Display for Segment {
2019-05-01 20:34:24 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.ansi_string())
}
}