use super::*;
pub fn wrap(text: &str, width: usize) -> Vec<String> {
let mut lines = Vec::new();
for line in text.split('\n') {
let chars: Vec<(char, Style)> = line.chars().map(|c| (c, Style::Plain)).collect();
let pieces = wrap_chars(&chars, width);
lines.extend(pieces.iter().map(|piece| piece.iter().map(|(c, _)| *c).collect()));
}
lines
}
pub(super) fn wrap_chars(chars: &[(char, Style)], width: usize) -> Vec<&[(char, Style)]> {
let width = width.max(1);
let mut pieces = Vec::new();
let mut start = 0;
while chars.len() - start > width {
let limit = start + width;
let cut = (start + 1..=limit).rev().find(|at| chars[*at].0 == ' ').unwrap_or(limit);
pieces.push(&chars[start..cut]);
start = cut + (chars[cut].0 == ' ') as usize;
}
pieces.push(&chars[start..]);
pieces
}
pub(super) fn row_of(prefix: Option<Span>, chars: &[(char, Style)], width: usize) -> Row {
let mut row: Row = prefix.into_iter().collect();
let mut used: usize = row.iter().map(|span| span.text.chars().count()).sum();
for (c, style) in chars {
match row.last_mut() {
Some(last) if last.style == *style => last.text.push(*c),
_ => row.push(Span::new(*style, c.to_string())),
}
}
used += chars.len();
row.push(Span::new(Style::Plain, " ".repeat(width.saturating_sub(used))));
row
}
pub(super) fn fence_language(tag: &str) -> Option<Language> {
let extension = match tag.trim().to_ascii_lowercase().as_str() {
"rust" => "rs".into(),
"python" => "py".into(),
"javascript" | "typescript" => "js".into(),
"c++" => "cpp".into(),
"golang" => "go".into(),
other => other.to_string(),
};
Language::from_path(&format!("code.{extension}"))
}
pub(super) fn inline(text: &str, base: Style) -> Vec<(char, Style)> {
let code = Style::Colored(Color::Cyan);
let chars: Vec<char> = text.chars().collect();
let (mut in_code, mut bold) = (false, false);
let mut styled = Vec::new();
let mut i = 0;
while i < chars.len() {
match chars[i] {
'`' => in_code = !in_code,
'*' if !in_code && chars.get(i + 1) == Some(&'*') => {
bold = !bold;
i += 1;
}
c if in_code => styled.push((c, code)),
c if bold => styled.push((c, Style::Bold)),
c => styled.push((c, base)),
}
i += 1;
}
styled
}
pub fn markdown(text: &str, width: usize) -> Vec<Row> {
let mut rows = Vec::new();
let mut code: Option<(Option<Language>, LineState)> = None;
for line in text.split('\n') {
let line = &widen(line);
if let Some(tag) = line.trim_start().strip_prefix("```") {
code = match code {
Some(_) => None,
None => Some((fence_language(tag), LineState::Code)),
};
continue;
}
let Some((language, state)) = &mut code else {
let trimmed = line.trim_start();
let indent = &line[..line.len() - trimmed.len()];
let heading = trimmed.trim_start_matches('#');
let chars = if heading.len() < trimmed.len() && heading.starts_with(' ') {
inline(heading.trim_start(), Style::Bold)
} else if let Some(item) = trimmed.strip_prefix("- ").or(trimmed.strip_prefix("* ")) {
inline(&format!("{indent}• {item}"), Style::Plain)
} else {
inline(line, Style::Plain)
};
for piece in wrap_chars(&chars, width) {
rows.push(row_of(None, piece, width));
}
continue;
};
let mut chars: Vec<(char, Style)> = line.chars().map(|c| (c, Style::Plain)).collect();
if let Some(language) = language {
let mut tokens = Vec::new();
*state = highlight::lex_line(line, *language, *state, &mut tokens);
for token in tokens {
let decoration = Decoration { kind: token.kind, ..Decoration::default() };
let end = token.end.min(chars.len());
for (_, style) in &mut chars[token.start.min(end)..end] {
*style = Style::Code(decoration);
}
}
}
for piece in wrap_chars(&chars, width.saturating_sub(2)) {
rows.push(row_of(Some(Span::new(Style::Dim, "│ ")), piece, width));
}
}
rows
}