From f909b20c540ea99dddfd04b8439ee35b8dd703b8 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 27 Oct 2021 17:10:42 +0530 Subject: allow stdin input to statix-single, vim plugin --- bin/src/main.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'bin/src/main.rs') diff --git a/bin/src/main.rs b/bin/src/main.rs index 4063621..9e9c8ac 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -4,7 +4,7 @@ mod fix; mod lint; mod traits; -use std::io; +use std::io::{self, BufRead}; use crate::{ err::{FixErr, SingleFixErr, StatixErr}, @@ -55,15 +55,26 @@ fn _main() -> Result<(), StatixErr> { } } } + // FIXME: this block nasty, configure in/out streams in `impl Single` maybe SubCommand::Single(single_config) => { - let path = single_config.target; - let src = std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)?; + let src = if let Some(path) = &single_config.target { + std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)? + } else { + io::stdin().lock().lines().map(|l| l.unwrap()).collect::>().join("\n") + }; + + let path_id = if let Some(path) = &single_config.target { + path.display().to_string() + } else { + "".to_owned() + }; + let (line, col) = single_config.position; let single_fix_result = fix::single(line, col, &src)?; if single_config.diff_only { let text_diff = TextDiff::from_lines(src.as_str(), &single_fix_result.src); - let old_file = format!("{}", path.display()); - let new_file = format!("{} [fixed]", path.display()); + let old_file = format!("{}", path_id); + let new_file = format!("{} [fixed]", path_id); println!( "{}", text_diff @@ -71,9 +82,11 @@ fn _main() -> Result<(), StatixErr> { .context_radius(4) .header(&old_file, &new_file) ); - } else { + } else if let Some(path) = single_config.target { std::fs::write(&path, &*single_fix_result.src) .map_err(SingleFixErr::InvalidPath)?; + } else { + print!("{}", &*single_fix_result.src) } } } -- cgit v1.2.3