From 80eeaad5d5df17d6a502ad56c3c992358ad0093a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 4 Apr 2023 22:38:02 +0200 Subject: [PATCH] Add output for machines --- schemsearch-cli/src/main.rs | 18 ++++++++++++-- schemsearch-cli/src/sinks.rs | 4 ++-- schemsearch-cli/src/stderr.rs | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 schemsearch-cli/src/stderr.rs diff --git a/schemsearch-cli/src/main.rs b/schemsearch-cli/src/main.rs index d2955a5..6dbe948 100644 --- a/schemsearch-cli/src/main.rs +++ b/schemsearch-cli/src/main.rs @@ -18,6 +18,7 @@ mod types; mod json_output; mod sinks; +mod stderr; use std::fmt::Debug; use std::io::Write; @@ -40,6 +41,7 @@ use crate::types::SqlSchematicSupplier; use indicatif::*; use schemsearch_files::Schematic; use crate::sinks::{OutputFormat, OutputSink}; +use crate::stderr::MaschineStdErr; fn main() { #[allow(unused_mut)] @@ -139,6 +141,15 @@ fn main() { .default_value("0") .value_parser(|s: &str| s.parse::().map_err(|e| e.to_string())), ) + .arg( + Arg::new("machine") + .help("Output for machines") + .short('m') + .long("machine") + .action(ArgAction::Set) + .default_value("0") + .value_parser(|s: &str| s.parse::().map_err(|e| e.to_string())) + ) .about("Searches for a pattern in a schematic") .bin_name("schemsearch"); @@ -246,9 +257,12 @@ fn main() { ThreadPoolBuilder::new().num_threads(*matches.get_one::("threads").expect("Could not get threads")).build_global().unwrap(); - let bar = ProgressBar::new(schematics.len() as u64); + let bar = ProgressBar::new(schematics.len() as u64); // "maschine" bar.set_style(ProgressStyle::with_template("[{elapsed}, ETA: {eta}] {wide_bar} {pos}/{len} {per_sec}").unwrap()); - //bar.set_draw_target(ProgressDrawTarget::stderr_with_hz(5)); + let term_size = *matches.get_one::("machine").expect("Could not get machine"); + if term_size != 0 { + bar.set_draw_target(ProgressDrawTarget::term_like(Box::new(MaschineStdErr { size: term_size }))) + } let matches: Vec = schematics.par_iter().progress_with(bar).map(|schem| { match schem { diff --git a/schemsearch-cli/src/sinks.rs b/schemsearch-cli/src/sinks.rs index e19a2f8..6c81f59 100644 --- a/schemsearch-cli/src/sinks.rs +++ b/schemsearch-cli/src/sinks.rs @@ -49,8 +49,8 @@ impl FromStr for OutputSink { impl OutputSink { pub fn output(&self) -> Box { match self { - OutputSink::Stdout => Box::new(std::io::stdout().lock()), - OutputSink::Stderr => Box::new(std::io::stderr().lock()), + OutputSink::Stdout => Box::new(std::io::stdout()), + OutputSink::Stderr => Box::new(std::io::stderr()), OutputSink::File(path) => Box::new(BufWriter::new(File::create(path).unwrap())) } } diff --git a/schemsearch-cli/src/stderr.rs b/schemsearch-cli/src/stderr.rs new file mode 100644 index 0000000..8292e0e --- /dev/null +++ b/schemsearch-cli/src/stderr.rs @@ -0,0 +1,44 @@ +use std::fmt::Debug; +use std::io::Write; +use indicatif::TermLike; + +#[derive(Debug)] +pub struct MaschineStdErr { pub(crate) size: u16} + +impl TermLike for MaschineStdErr { + fn width(&self) -> u16 { + self.size + } + + fn move_cursor_up(&self, _: usize) -> std::io::Result<()> { + Ok(()) + } + + fn move_cursor_down(&self, _: usize) -> std::io::Result<()> { + Ok(()) + } + + fn move_cursor_right(&self, _: usize) -> std::io::Result<()> { + Ok(()) + } + + fn move_cursor_left(&self, _: usize) -> std::io::Result<()> { + Ok(()) + } + + fn write_line(&self, s: &str) -> std::io::Result<()> { + writeln!(std::io::stderr(), "{}", s) + } + + fn write_str(&self, s: &str) -> std::io::Result<()> { + write!(std::io::stderr(), "{}", s) + } + + fn clear_line(&self) -> std::io::Result<()> { + Ok(()) + } + + fn flush(&self) -> std::io::Result<()> { + std::io::stderr().flush() + } +} \ No newline at end of file