summaryrefslogtreecommitdiff
path: root/src/reports
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-04-27 12:38:40 +1200
committerBen Bridle <ben@derelict.engineering>2025-04-27 12:38:40 +1200
commitc13c1f2748598343e01128c3f734df309aa4a26d (patch)
tree0fdef6bbdac5e32031ea90146ad030d4892e260a /src/reports
parent55e64d35273a425b52b7d913b9368af2f0370bbb (diff)
downloadassembler-c13c1f2748598343e01128c3f734df309aa4a26d.zip
Replace ansi library with inked library
The inked library handles colours correctly on Windows. The log library has also been updated to the newer version which uses inked internally.
Diffstat (limited to 'src/reports')
-rw-r--r--src/reports/mod.rs73
-rw-r--r--src/reports/source_hierarchy.rs22
-rw-r--r--src/reports/source_symbols.rs6
-rw-r--r--src/reports/unused_symbols.rs4
4 files changed, 50 insertions, 55 deletions
diff --git a/src/reports/mod.rs b/src/reports/mod.rs
index bbddba9..9fc5e27 100644
--- a/src/reports/mod.rs
+++ b/src/reports/mod.rs
@@ -10,53 +10,52 @@ pub use unused_symbols::*;
use crate::*;
-pub use log::LogLevel;
-pub use ansi::*;
-
pub fn report_source_issue(level: LogLevel, context: &Context, message: &str) {
- let mut reporter = SourceIssueReporter::new(context, message, level);
+ let mut reporter = SourceIssueReporter::new(context, level);
reporter.format_source_span(context.source);
- let string = reporter.output;
+ let details = reporter.details;
// Print the completed message.
match level {
- LogLevel::Info => log::info!( "{string}"),
- LogLevel::Warn => log::warn!( "{string}"),
- LogLevel::Error => log::error!("{string}"),
- LogLevel::Fatal => log::fatal!("{string}"),
+ LogLevel::Info => log_info(message, Some(details)),
+ LogLevel::Warn => log_warn(message, Some(details)),
+ LogLevel::Error => log_error(message, Some(details)),
+ LogLevel::Fatal => log_fatal(message, Some(details)),
}
}
struct SourceIssueReporter<'a> {
context: &'a Context<'a>,
- output: String,
+ details: InkedString,
level: LogLevel,
digits: usize,
}
impl<'a> SourceIssueReporter<'a> {
- pub fn new(context: &'a Context<'a>, message: &str, level: LogLevel) -> Self {
- let output = format!("{message}\n");
+ pub fn new(context: &'a Context<'a>, level: LogLevel) -> Self {
+ let details = InkedString::new();
let digits = get_end_line_number(context.source).to_string().len();
- Self { context, level, output, digits }
+ Self { context, level, details, digits }
}
pub fn format_source_span(&mut self, source: &SourceSpan) {
let colour = match self.level {
- LogLevel::Info => BLUE,
- LogLevel::Warn => YELLOW,
- LogLevel::Error => RED,
- LogLevel::Fatal => RED,
+ LogLevel::Info => Colour::Blue,
+ LogLevel::Warn => Colour::Yellow,
+ LogLevel::Error => Colour::Red,
+ LogLevel::Fatal => Colour::Red,
};
// Print location line.
- self.output.push_str(NORMAL);
let arrow = "-->";
+ let w = self.digits + 3;
+ self.details.push(ink!("{arrow:>w$} ").blue());
let location = source.location();
- self.push(&format!("{BLUE}{arrow:>w$}{NORMAL} {location}\n", w=self.digits+3));
+ self.details.push(ink!("{location}\n"));
+ // Print each source line.
let line_count = location.end.line - location.start.line + 1;
for line_i in 0..line_count {
let merged_i = source.in_merged.start.line + line_i;
@@ -74,25 +73,30 @@ impl<'a> SourceIssueReporter<'a> {
};
// Print source code line.
- self.push(&format!("{BLUE} {line_num:>w$} | {NORMAL}", line_num=location_i+1, w=self.digits));
+ let line_num = location_i + 1;
+ let w = self.digits;
+ self.details.push(ink!(" {line_num:>w$} | ").blue());
+ let mut line_colour = None;
for (i, c) in source_line.chars().enumerate() {
- if i == left { self.output.push_str(colour) }
- if i == right { self.output.push_str(NORMAL) }
- self.output.push(c);
+ if i == left { line_colour = Some(colour) }
+ if i == right { line_colour = None }
+ self.details.push(ink!("{c}").set_colour(line_colour));
}
- self.output.push_str(NORMAL);
- self.output.push('\n');
+ self.details.push(ink!("\n"));
// Print an underline on the final line.
if line_i + 1 == line_count {
// Print source code underline.
let space = " ";
- self.push(&format!("{BLUE} {space:>w$} | {NORMAL}", w=self.digits));
- for _ in 0..left { self.output.push_str(" "); }
- self.output.push_str(colour);
- for _ in left..right { self.output.push_str("^"); }
- self.output.push_str(NORMAL);
- self.output.push('\n');
+ let w = self.digits;
+ self.details.push(ink!(" {space:>w$} | ").blue());
+ for _ in 0..left {
+ self.details.push(ink!(" "));
+ }
+ for _ in left..right {
+ self.details.push(ink!("^").set_colour(Some(colour)));
+ }
+ self.details.push(ink!("\n"));
}
}
@@ -100,13 +104,6 @@ impl<'a> SourceIssueReporter<'a> {
if let Some(child) = &source.child {
self.format_source_span(&child);
}
-
- // Remove the trailing new-line.
- self.output.pop();
- }
-
- fn push(&mut self, string: &str) {
- self.output.push_str(&string);
}
}
diff --git a/src/reports/source_hierarchy.rs b/src/reports/source_hierarchy.rs
index 2e88a77..f391785 100644
--- a/src/reports/source_hierarchy.rs
+++ b/src/reports/source_hierarchy.rs
@@ -7,7 +7,7 @@ pub struct SourceHierarchy<'a> {
impl<'a> SourceHierarchy<'a> {
pub fn report(&self) {
- eprintln!(".");
+ ink!(".").eprintln();
let len = self.resolver.root_unit_ids.len();
for (i, id) in self.resolver.root_unit_ids.iter().enumerate() {
let end = i + 1 == len;
@@ -19,29 +19,29 @@ impl<'a> SourceHierarchy<'a> {
// A level entry is true if all entries in that level have been printed.
for level in &levels {
match level {
- false => eprint!("│ "),
- true => eprint!(" "),
+ false => ink!("│ ").eprint(),
+ true => ink!(" ").eprint(),
}
}
// The end value is true if all siblings of this entry have been printed.
match end {
- false => eprint!("├── "),
- true => eprint!("└── "),
+ false => ink!("├── ").eprint(),
+ true => ink!("└── ").eprint(),
}
let unit = &self.resolver.source_units[id];
let path_str = &unit.source_unit.main.path.as_os_str().to_string_lossy();
if let Some(name_str) = unit.source_unit.name() {
- eprint!("{name_str}{BLUE}");
- if unit.source_unit.head.is_some() { eprint!(" +head") }
- if unit.source_unit.tail.is_some() { eprint!(" +tail") }
+ ink!("{name_str}").eprint();
+ if unit.source_unit.head.is_some() { ink!(" +head").blue().eprint() }
+ if unit.source_unit.tail.is_some() { ink!(" +tail").blue().eprint() }
let mut unresolved = 0;
for symbol in &self.resolver.unresolved {
if symbol.source_id == id { unresolved += 1; }
}
- if unresolved > 0 { eprint!("{RED} ({unresolved})"); }
- eprintln!("{NORMAL} {DIM}({path_str}){NORMAL}");
+ if unresolved > 0 { ink!(" ({unresolved})").red().eprint(); }
+ ink!(" ({path_str})").dim().eprintln();
} else {
- eprintln!("{path_str}");
+ ink!("{path_str}").eprintln();
}
levels.push(end);
let len = unit.child_ids.len();
diff --git a/src/reports/source_symbols.rs b/src/reports/source_symbols.rs
index 976660f..a042f1c 100644
--- a/src/reports/source_symbols.rs
+++ b/src/reports/source_symbols.rs
@@ -9,7 +9,7 @@ impl<'a> SourceSymbols<'a> {
pub fn report(&self) {
for definition in &self.resolver.definitions {
let message = format!(
- "Definition '{BOLD}{:?}{NORMAL}' (resolves {})",
+ "Definition '{:?}' (resolves {})",
definition.tracked.symbol, definition.references.len());
let context = definition.tracked.context(self.resolver);
report_source_issue(LogLevel::Info, &context, &message);
@@ -18,7 +18,7 @@ impl<'a> SourceSymbols<'a> {
for reference in &self.resolver.resolved {
let definition = &self.resolver.definitions[reference.definition];
let message = format!(
- "Resolved reference '{BOLD}{:?}{NORMAL}' (defined by {:?})",
+ "Resolved reference '{:?}' (defined by {:?})",
reference.tracked.symbol, definition.tracked.symbol);
let context = reference.tracked.context(self.resolver);
report_source_issue(LogLevel::Info, &context, &message);
@@ -26,7 +26,7 @@ impl<'a> SourceSymbols<'a> {
for reference in &self.resolver.unresolved {
let message = format!(
- "Unresolved reference '{BOLD}{:?}{NORMAL}'", reference.symbol);
+ "Unresolved reference '{:?}'", reference.symbol);
let context = reference.context(self.resolver);
report_source_issue(LogLevel::Warn, &context, &message);
}
diff --git a/src/reports/unused_symbols.rs b/src/reports/unused_symbols.rs
index 7cd3596..90cb7d1 100644
--- a/src/reports/unused_symbols.rs
+++ b/src/reports/unused_symbols.rs
@@ -9,9 +9,7 @@ impl<'a> UnusedSymbols<'a> {
pub fn report(&self) {
for definition in &self.resolver.definitions {
if definition.references.is_empty() {
- let message = format!(
- "Unused definition '{BOLD}{:?}{NORMAL}'",
- definition.tracked.symbol);
+ let message = format!("Unused definition '{:?}'", definition.tracked.symbol);
let context = definition.tracked.context(self.resolver);
report_source_issue(LogLevel::Warn, &context, &message);
}