252 lines
6.7 KiB
Rust
252 lines
6.7 KiB
Rust
|
|
//! Text output and presentation roles shared with the terminal.
|
||
|
|
|
||
|
|
use super::*;
|
||
|
|
use std::ops::Range;
|
||
|
|
|
||
|
|
/// A presentation role for a span of report text.
|
||
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||
|
|
pub enum TextStyle {
|
||
|
|
Heading,
|
||
|
|
Layer,
|
||
|
|
Added,
|
||
|
|
Removed,
|
||
|
|
Changed,
|
||
|
|
Success,
|
||
|
|
Warning,
|
||
|
|
Error,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl TextStyle {
|
||
|
|
pub(super) fn change(marker: char) -> Self {
|
||
|
|
match marker {
|
||
|
|
'+' => Self::Added,
|
||
|
|
'-' => Self::Removed,
|
||
|
|
_ => Self::Changed,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// A plain text report with optional style spans.
|
||
|
|
#[derive(Default)]
|
||
|
|
pub struct TextReport {
|
||
|
|
text: String,
|
||
|
|
spans: Option<Vec<(Range<usize>, TextStyle)>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl TextReport {
|
||
|
|
pub fn new(styled: bool) -> Self {
|
||
|
|
Self {
|
||
|
|
text: String::new(),
|
||
|
|
spans: styled.then(Vec::new),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn as_str(&self) -> &str {
|
||
|
|
&self.text
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn into_string(self) -> String {
|
||
|
|
self.text
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Byte ranges are in output order and never overlap.
|
||
|
|
pub fn styles(&self) -> &[(Range<usize>, TextStyle)] {
|
||
|
|
self.spans.as_deref().unwrap_or(&[])
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(super) fn is_styled(&self) -> bool {
|
||
|
|
self.spans.is_some()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn write_styled(&mut self, style: TextStyle, text: fmt::Arguments<'_>) {
|
||
|
|
let start = self.len();
|
||
|
|
self.write_fmt(text).unwrap();
|
||
|
|
self.mark(start, style);
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(super) fn push_str(&mut self, text: &str) {
|
||
|
|
self.text.push_str(text);
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(super) fn push(&mut self, ch: char) {
|
||
|
|
self.text.push(ch);
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(super) fn len(&self) -> usize {
|
||
|
|
self.text.len()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(super) fn mark(&mut self, start: usize, style: TextStyle) {
|
||
|
|
let end = self.text.len();
|
||
|
|
if start < end
|
||
|
|
&& let Some(spans) = &mut self.spans
|
||
|
|
{
|
||
|
|
if let Some((range, previous)) = spans.last_mut()
|
||
|
|
&& range.end == start
|
||
|
|
&& *previous == style
|
||
|
|
{
|
||
|
|
range.end = end;
|
||
|
|
} else {
|
||
|
|
spans.push((start..end, style));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl fmt::Write for TextReport {
|
||
|
|
fn write_str(&mut self, text: &str) -> fmt::Result {
|
||
|
|
self.push_str(text);
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
enum Part {
|
||
|
|
Text(String),
|
||
|
|
Layer(Layer),
|
||
|
|
Placements(Option<(u64, u64)>),
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Cell details awaiting inline layer labels and reference placement totals.
|
||
|
|
pub(super) struct CellText<'a> {
|
||
|
|
names: &'a BTreeMap<Layer, String>,
|
||
|
|
name_changes: &'a [LayerNameChange],
|
||
|
|
layers: BTreeMap<Layer, bool>,
|
||
|
|
parts: Vec<(Option<TextStyle>, Part)>,
|
||
|
|
styled: bool,
|
||
|
|
style: Option<TextStyle>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<'a> CellText<'a> {
|
||
|
|
pub fn new(
|
||
|
|
names: &'a BTreeMap<Layer, String>,
|
||
|
|
name_changes: &'a [LayerNameChange],
|
||
|
|
styled: bool,
|
||
|
|
) -> Self {
|
||
|
|
Self {
|
||
|
|
names,
|
||
|
|
name_changes,
|
||
|
|
layers: BTreeMap::new(),
|
||
|
|
parts: Vec::new(),
|
||
|
|
styled,
|
||
|
|
style: None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn style(&mut self, style: Option<TextStyle>) {
|
||
|
|
self.style = style.filter(|_| self.styled);
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn write_layer(&mut self, layer: Layer, side: Option<Side>) {
|
||
|
|
if self.names.contains_key(&layer) {
|
||
|
|
self.layers
|
||
|
|
.entry(layer)
|
||
|
|
.and_modify(|repeated| *repeated = true)
|
||
|
|
.or_insert(false);
|
||
|
|
self.parts.push((self.style, Part::Layer(layer)));
|
||
|
|
} else {
|
||
|
|
write!(self, "{}/{}", layer.0, layer.1).unwrap();
|
||
|
|
if let Some(name) = self.transition_name(layer, side) {
|
||
|
|
write!(self, " ({})", cell_label(name)).unwrap();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn reserve_placements(&mut self) -> usize {
|
||
|
|
let index = self.parts.len();
|
||
|
|
self.parts.push((self.style, Part::Placements(None)));
|
||
|
|
index
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn set_placements(&mut self, index: usize, old: u64, new: u64) {
|
||
|
|
self.parts[index].1 = Part::Placements(Some((old, new)));
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn push_str(&mut self, text: &str) {
|
||
|
|
self.write_str(text).unwrap();
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn push(&mut self, ch: char) {
|
||
|
|
self.write_char(ch).unwrap();
|
||
|
|
}
|
||
|
|
|
||
|
|
fn append(&mut self, text: &str) {
|
||
|
|
if text.is_empty() {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if let Some((style, Part::Text(current))) = self.parts.last_mut()
|
||
|
|
&& *style == self.style
|
||
|
|
{
|
||
|
|
current.push_str(text);
|
||
|
|
} else {
|
||
|
|
self.parts.push((self.style, Part::Text(text.to_string())));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn transition_name(&self, layer: Layer, side: Option<Side>) -> Option<&'a str> {
|
||
|
|
let side = side?;
|
||
|
|
let at = self
|
||
|
|
.name_changes
|
||
|
|
.binary_search_by_key(&layer, |change| change.layer)
|
||
|
|
.ok()?;
|
||
|
|
let change = &self.name_changes[at];
|
||
|
|
match side {
|
||
|
|
Side::Old => change.old.as_deref(),
|
||
|
|
Side::New => change.new.as_deref(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn write_legend(&self, out: &mut TextReport) {
|
||
|
|
for (&(layer, datatype), &repeated) in &self.layers {
|
||
|
|
if repeated {
|
||
|
|
writeln!(
|
||
|
|
out,
|
||
|
|
"{layer}/{datatype}: name {:?}",
|
||
|
|
self.names[&(layer, datatype)]
|
||
|
|
)
|
||
|
|
.unwrap();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn write_body(self, out: &mut TextReport) {
|
||
|
|
for (style, part) in self.parts {
|
||
|
|
let start = out.len();
|
||
|
|
match part {
|
||
|
|
Part::Text(text) => out.push_str(&text),
|
||
|
|
Part::Layer((layer, datatype)) => {
|
||
|
|
write!(out, "{layer}/{datatype}").unwrap();
|
||
|
|
if !self.layers[&(layer, datatype)] {
|
||
|
|
write!(out, " ({})", cell_label(&self.names[&(layer, datatype)])).unwrap();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Part::Placements(Some((old, new))) if old == new => {
|
||
|
|
let suffix = if new == 1 { "" } else { "s" };
|
||
|
|
write!(out, " · {new} total placement{suffix}").unwrap();
|
||
|
|
}
|
||
|
|
Part::Placements(Some((old, new))) => {
|
||
|
|
write!(out, " · placements {old} -> {new}").unwrap();
|
||
|
|
}
|
||
|
|
Part::Placements(None) => (),
|
||
|
|
}
|
||
|
|
if let Some(style) = style {
|
||
|
|
out.mark(start, style);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl fmt::Write for CellText<'_> {
|
||
|
|
fn write_str(&mut self, text: &str) -> fmt::Result {
|
||
|
|
if self.style.is_some()
|
||
|
|
&& let Some((first, rest)) = text.split_once('\n')
|
||
|
|
{
|
||
|
|
self.append(first);
|
||
|
|
self.style = None;
|
||
|
|
self.append("\n");
|
||
|
|
self.append(rest);
|
||
|
|
} else {
|
||
|
|
self.append(text);
|
||
|
|
}
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
}
|