use metrale_circuit::display::{Document, Style};
use super::ColorChoice;
use crate::tui::theme::{self, C, Depth};
pub(crate) fn resolve_depth(
choice: ColorChoice,
is_tty: bool,
no_color: Option<&str>,
colorterm: Option<&str>,
) -> Depth {
match (choice, is_tty) {
(ColorChoice::Never, _) | (ColorChoice::Auto, false) => Depth::None,
(ColorChoice::Auto, true) | (ColorChoice::Always, _) => {
theme::depth_of(no_color, colorterm)
}
}
}
fn look(style: Style) -> (Option<C>, bool) {
match style {
Style::Plain => (None, false),
Style::Heading => (Some(theme::PURPLE), true),
Style::Accent => (Some(theme::CYAN), false),
Style::Dim => (Some(theme::TEXT_DIM), false),
Style::Border | Style::OpLight | Style::Format | Style::LayerDense => {
(Some(theme::TEXT_2), false)
}
Style::OpHeavy | Style::LayerAttn => (Some(theme::PURPLE), false),
Style::EdgeMaterialized | Style::LayerMoe => (Some(theme::WARN), false),
Style::EdgeFused | Style::NumericsBitIdentical => (Some(theme::GREEN), false),
Style::FusedFrame => (Some(theme::CYAN), true),
Style::NumericsReference => (Some(theme::TEXT_2), false),
Style::NumericsDiffers => (Some(theme::ERROR), true),
Style::LayerGdn => (Some(theme::CYAN), false),
}
}
fn open(style: Style, depth: Depth) -> String {
let (colour, bold) = look(style);
let mut codes: Vec<String> = Vec::new();
if bold {
codes.push("1".into());
}
match (colour, depth) {
(_, Depth::None) => return String::new(),
(Some(c), Depth::True) => codes.push(format!("38;2;{};{};{}", c.0, c.1, c.2)),
(Some(c), Depth::Ansi256) => codes.push(format!("38;5;{}", c.3)),
(None, _) => {}
}
if codes.is_empty() {
String::new()
} else {
format!("\x1b[{}m", codes.join(";"))
}
}
pub(crate) fn paint(doc: &Document, depth: Depth) -> String {
let mut out = String::new();
for line in &doc.lines {
let mut text = String::new();
for span in &line.spans {
let start = open(span.style, depth);
text.push_str(&start);
text.push_str(&span.text);
if !start.is_empty() {
text.push_str("\x1b[0m");
}
}
out.push_str(text.trim_end());
out.push('\n');
}
out
}
#[cfg(test)]
#[path = "circuit_paint_tests.rs"]
mod circuit_paint_tests;