From 2e1fb0a60a671cd609ff603fa5a371341b7e0fe9 Mon Sep 17 00:00:00 2001 From: Mroik Date: Wed, 5 Mar 2025 10:01:11 +0100 Subject: Add capability to pipe in text Can now pipe in text instead of specifying the filename. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 26 ++++++++++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f263c7..18608e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,7 +246,7 @@ checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" [[package]] name = "marika-finger-blaster" -version = "0.0.1" +version = "0.0.2" dependencies = [ "clap", "crossterm", diff --git a/Cargo.toml b/Cargo.toml index f0fcc2b..49d0d32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "marika-finger-blaster" -version = "0.0.1" +version = "0.0.2" edition = "2021" [dependencies] diff --git a/src/main.rs b/src/main.rs index 4803c26..ad66a3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,12 @@ pub mod error; pub mod event; pub mod state; -use std::{error::Error, fs::read_to_string, path::Path}; +use std::{ + error::Error, + fs::read_to_string, + io::{stdin, IsTerminal, Read}, + path::Path, +}; use app::App; use clap::Parser; @@ -36,13 +41,18 @@ fn generate_quotes(path: &Path) -> Result, Box> { #[tokio::main] async fn main() -> Result<(), Box> { - let args = Args::parse(); - let path = Path::new(&args.quote); - let mut quotes = generate_quotes(path).unwrap(); - let mut rng = thread_rng(); - let chosen = rng.gen_range(0..quotes.len()); - let quote = quotes.remove(chosen); - drop(quotes); + let quote = if !stdin().is_terminal() { + let mut b = Vec::new(); + stdin().read_to_end(&mut b).unwrap(); + String::from_utf8(b).unwrap() + } else { + let args = Args::parse(); + let path = Path::new(&args.quote); + let mut quotes = generate_quotes(path).unwrap(); + let mut rng = thread_rng(); + let chosen = rng.gen_range(0..quotes.len()); + quotes.remove(chosen) + }; // TODO Add more options to choose quotes let mut app = App::new("e); -- cgit v1.3