diff options
author | Akshay <[email protected]> | 2021-10-27 12:40:42 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-27 12:43:23 +0100 |
commit | f909b20c540ea99dddfd04b8439ee35b8dd703b8 (patch) | |
tree | cc9a60108f975419bf2621262739071a5f0a1be6 /bin/src/main.rs | |
parent | ed1ee66b06add4c22e4922ffa762f097355c7431 (diff) |
allow stdin input to statix-single, vim pluginv0.2.2
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r-- | bin/src/main.rs | 25 |
1 files changed, 19 insertions, 6 deletions
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; | |||
4 | mod lint; | 4 | mod lint; |
5 | mod traits; | 5 | mod traits; |
6 | 6 | ||
7 | use std::io; | 7 | use std::io::{self, BufRead}; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
10 | err::{FixErr, SingleFixErr, StatixErr}, | 10 | err::{FixErr, SingleFixErr, StatixErr}, |
@@ -55,15 +55,26 @@ fn _main() -> Result<(), StatixErr> { | |||
55 | } | 55 | } |
56 | } | 56 | } |
57 | } | 57 | } |
58 | // FIXME: this block nasty, configure in/out streams in `impl Single` maybe | ||
58 | SubCommand::Single(single_config) => { | 59 | SubCommand::Single(single_config) => { |
59 | let path = single_config.target; | 60 | let src = if let Some(path) = &single_config.target { |
60 | let src = std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)?; | 61 | std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)? |
62 | } else { | ||
63 | io::stdin().lock().lines().map(|l| l.unwrap()).collect::<Vec<String>>().join("\n") | ||
64 | }; | ||
65 | |||
66 | let path_id = if let Some(path) = &single_config.target { | ||
67 | path.display().to_string() | ||
68 | } else { | ||
69 | "<unknown>".to_owned() | ||
70 | }; | ||
71 | |||
61 | let (line, col) = single_config.position; | 72 | let (line, col) = single_config.position; |
62 | let single_fix_result = fix::single(line, col, &src)?; | 73 | let single_fix_result = fix::single(line, col, &src)?; |
63 | if single_config.diff_only { | 74 | if single_config.diff_only { |
64 | let text_diff = TextDiff::from_lines(src.as_str(), &single_fix_result.src); | 75 | let text_diff = TextDiff::from_lines(src.as_str(), &single_fix_result.src); |
65 | let old_file = format!("{}", path.display()); | 76 | let old_file = format!("{}", path_id); |
66 | let new_file = format!("{} [fixed]", path.display()); | 77 | let new_file = format!("{} [fixed]", path_id); |
67 | println!( | 78 | println!( |
68 | "{}", | 79 | "{}", |
69 | text_diff | 80 | text_diff |
@@ -71,9 +82,11 @@ fn _main() -> Result<(), StatixErr> { | |||
71 | .context_radius(4) | 82 | .context_radius(4) |
72 | .header(&old_file, &new_file) | 83 | .header(&old_file, &new_file) |
73 | ); | 84 | ); |
74 | } else { | 85 | } else if let Some(path) = single_config.target { |
75 | std::fs::write(&path, &*single_fix_result.src) | 86 | std::fs::write(&path, &*single_fix_result.src) |
76 | .map_err(SingleFixErr::InvalidPath)?; | 87 | .map_err(SingleFixErr::InvalidPath)?; |
88 | } else { | ||
89 | print!("{}", &*single_fix_result.src) | ||
77 | } | 90 | } |
78 | } | 91 | } |
79 | } | 92 | } |