diff options
author | stuebinm | 2024-04-03 22:22:38 +0200 |
---|---|---|
committer | stuebinm | 2024-04-03 23:36:33 +0200 |
commit | 0567f916d4365c8dc0be99d194fe6d157befbc81 (patch) | |
tree | 8e1123ae8112abab0f3726da75bec2c08787ce0e /src/status_reporter.rs | |
parent | 48534f8c321cb33190a3cc80a9c364ffbf68c878 (diff) |
very basic query language
Diffstat (limited to 'src/status_reporter.rs')
-rw-r--r-- | src/status_reporter.rs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/status_reporter.rs b/src/status_reporter.rs new file mode 100644 index 0000000..c8faee4 --- /dev/null +++ b/src/status_reporter.rs @@ -0,0 +1,93 @@ +use std::sync::Mutex; + +struct StatusReportData { + files: usize, + items: usize, + total_files: usize, + total_items: usize, + changed_items: usize, + last_file: String, + last_item: String, +} + +impl StatusReportData { + fn print(&self, clear: bool) { + if clear { + print!("\x1b[1F\x1b[2K\x1b[1F\x1b[2K"); + } + println!("{}/{} files ({})", self.files, self.total_files, self.last_file); + println!("{}/{} ({}) items ({})", self.items, self.total_items, + self.changed_items, self.last_item); + } +} + +pub struct StatusReport(Mutex<StatusReportData>); + +impl StatusReport { + pub fn new(total_files: usize, total_items: usize) -> Self { + Self(Mutex::new(StatusReportData { + files: 0, + items: 0, + total_files, + total_items, + changed_items: 0, + last_file: "".to_string(), + last_item: "".to_string(), + })) + } + + pub fn enter_file(&self, f: &str) { + let mut m = self.0.lock().unwrap(); + m.files += 1; + m.last_file = f.to_string(); + m.print(m.files > 1 || m.items >= 1); + } + + fn enter_item(&self, i: String) { + let mut m = self.0.lock().unwrap(); + m.items += 1; + m.last_item = i; + m.print(m.files >= 1 || m.items > 1); + } + + pub fn update_item(&self, i: String) { + let mut m = self.0.lock().unwrap(); + m.last_item = i; + m.print(true); + } + + pub fn changed_item(&self) { + let mut m = self.0.lock().unwrap(); + m.changed_items += 1; + m.print(true); + } + + fn skip_items(&self, i: usize) { + let mut m = self.0.lock().unwrap(); + m.items += i; + m.print(m.files >= 1 || m.items >= 1); + } +} + +struct StatusPart<'a>(&'a StatusReport, usize); + +impl<'a> StatusPart<'a> { + fn enter_item(&mut self, i: String) { + self.0.enter_item(i); + self.1 -= 1; + } + + fn update_item(&mut self, i: String) { + self.0.update_item(i); + } + + fn changed_item(&mut self) { + self.0.changed_item(); + } +} + +impl<'a> Drop for StatusPart<'a> { + fn drop(&mut self) { + self.0.skip_items(self.1); + } +} |