From 056211ba050c1885d3b8fb11e1423dc4654574ae Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Sep 2024 08:25:48 +0100 Subject: [PATCH] feat: pretty-print found markers --- src/main.rs | 5 +++-- src/model/line.rs | 12 ++++++++++++ src/model/markers/found.rs | 8 ++++++++ src/model/markers/marker.rs | 9 +++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index adba03e..9a339cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn main() -> Result<()> { } } - println!("{found_markers:#?}"); + println!("{found_markers}"); // TODO: add authentication when provided // let issues = ureq::get(&api).call()?.into_string()?; @@ -53,7 +53,7 @@ fn main() -> Result<()> { } fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> { - println!("file: {}", file.to_string_lossy()); + let relative_path = file.strip_prefix(config.fs().base())?.to_path_buf(); config .fs() .file_read_to_string(file)? @@ -62,6 +62,7 @@ fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> .map(|(n, line)| { Line::builder() .file(file.to_path_buf()) + .relative_path(relative_path.clone()) .num(n) .value(line.to_owned()) .build() diff --git a/src/model/line.rs b/src/model/line.rs index cae6d4c..60fd971 100644 --- a/src/model/line.rs +++ b/src/model/line.rs @@ -13,6 +13,7 @@ use super::Marker; #[derive(Debug, Builder)] pub struct Line { file: PathBuf, + relative_path: PathBuf, num: usize, value: String, } @@ -45,3 +46,14 @@ impl Line { } } } +impl std::fmt::Display for Line { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!( + f, + "{}#{}:\n {}", + self.relative_path.to_string_lossy(), + self.num, + self.value.trim() + ) + } +} diff --git a/src/model/markers/found.rs b/src/model/markers/found.rs index 74940ce..db4b26a 100644 --- a/src/model/markers/found.rs +++ b/src/model/markers/found.rs @@ -10,3 +10,11 @@ impl FoundMarkers { self.markers.push(marker); } } +impl std::fmt::Display for FoundMarkers { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for marker in self.markers.iter() { + write!(f, "- {marker}")?; + } + Ok(()) + } +} diff --git a/src/model/markers/marker.rs b/src/model/markers/marker.rs index 314793f..e411dfc 100644 --- a/src/model/markers/marker.rs +++ b/src/model/markers/marker.rs @@ -9,3 +9,12 @@ pub enum Marker { Invalid(Line), Valid(Line, String), } +impl std::fmt::Display for Marker { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Marker::Unmarked => Ok(()), + Marker::Invalid(line) => write!(f, "Invalid: {line}"), + Marker::Valid(line, _) => write!(f, "Valid : {line}"), + } + } +}