From df54cc7484a6e1937bd5317c9c287ff98d24691a Mon Sep 17 00:00:00 2001 From: Laurens Miers Date: Tue, 27 May 2025 18:11:05 +0200 Subject: [PATCH] feat: reading input + show prompt --- src/main.rs | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 69e2bf7..6935ba0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use clap::Parser; - +use std::fs::File; +use std::io::{self, BufRead, Write}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -9,12 +10,43 @@ struct Args { script: Option, } -fn main() { +fn run(lines: io::Lines>) -> () { + for line in lines { + println!("Line: {}", line.unwrap()); + } +} + +fn run_file(script: String) -> Result<(), io::Error> { + let file = File::open(script)?; + run(io::BufReader::new(file).lines()); + + Ok(()) +} + +fn run_prompt() -> Result<(), io::Error> { + loop { + print!("> "); + io::stdout().flush()?; + + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer)?; + if buffer.len() == 0 { + break; + } + println!("input: '{}', {}", buffer, buffer.len()); + } + + Ok(()) +} + +fn main() -> Result<(), Box> { let args = Args::parse(); if let Some(script) = args.script { - println!("Run script! {}", script); + run_file(script)?; } else { - println!("Run Prompt"); + run_prompt()?; } + + Ok(()) }