From c7b771747f33f4b195fa2c75e413530f63d1d8d2 Mon Sep 17 00:00:00 2001 From: Max Niederman Date: Mon, 1 May 2023 20:08:32 -0700 Subject: [PATCH] feat: add support for reading from stdin --- src/main.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 176c1bd..6921358 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,11 +61,20 @@ impl Opt { fn gen_contents(&self) -> Option> { match &self.contents { Some(path) => { - let file = fs::File::open(path).expect("Error reading language file."); - let lines: Vec = io::BufReader::new(file) - .lines() - .filter_map(Result::ok) - .collect(); + let lines: Vec = if path.as_os_str() == "-" { + std::io::stdin() + .lock() + .lines() + .filter_map(Result::ok) + .collect() + } else { + let file = fs::File::open(path).expect("Error reading language file."); + io::BufReader::new(file) + .lines() + .filter_map(Result::ok) + .collect() + }; + Some(lines.iter().map(String::from).collect()) } None => { @@ -169,8 +178,11 @@ impl State { fn main() -> crossterm::Result<()> { let opt = Opt::from_args(); - let config = opt.config(); + if opt.debug { + dbg!(&opt); + } + let config = opt.config(); if opt.debug { dbg!(&config); }