Skip to content

Commit

Permalink
Add output for machines
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoscaot committed Apr 4, 2023
1 parent 64158cf commit 80eeaad
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
18 changes: 16 additions & 2 deletions schemsearch-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
mod types;
mod json_output;
mod sinks;
mod stderr;

use std::fmt::Debug;
use std::io::Write;
Expand All @@ -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)]
Expand Down Expand Up @@ -139,6 +141,15 @@ fn main() {
.default_value("0")
.value_parser(|s: &str| s.parse::<usize>().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::<u16>().map_err(|e| e.to_string()))
)
.about("Searches for a pattern in a schematic")
.bin_name("schemsearch");

Expand Down Expand Up @@ -246,9 +257,12 @@ fn main() {

ThreadPoolBuilder::new().num_threads(*matches.get_one::<usize>("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::<u16>("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<SearchResult> = schematics.par_iter().progress_with(bar).map(|schem| {
match schem {
Expand Down
4 changes: 2 additions & 2 deletions schemsearch-cli/src/sinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl FromStr for OutputSink {
impl OutputSink {
pub fn output(&self) -> Box<dyn Write> {
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()))
}
}
Expand Down
44 changes: 44 additions & 0 deletions schemsearch-cli/src/stderr.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 80eeaad

Please sign in to comment.