From 0e090d2402edc417066b158c8ea7427bdf5cb78c Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Fri, 15 Nov 2024 15:28:11 -0500 Subject: [PATCH] Finished TP --- Session1/TP2/Text/src/main.rs | 112 ++++++++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 11 deletions(-) diff --git a/Session1/TP2/Text/src/main.rs b/Session1/TP2/Text/src/main.rs index e2db97d..dbeda81 100644 --- a/Session1/TP2/Text/src/main.rs +++ b/Session1/TP2/Text/src/main.rs @@ -1,4 +1,5 @@ -use std::io::{self, Write}; +use std::fs::File; +use std::io::{self, BufRead, Write}; use std::path::Path; fn main() { @@ -10,7 +11,10 @@ fn main() { print!("\x1b[2J\x1b[H"); println!("========== Justify some text =========="); println!(" A) Set file name (Name: {})", file_name); - println!(" B) Set column amount between 50 and 120 (Amount: {})", col_amount); + println!( + " B) Set column amount between 50 and 120 (Amount: {})", + col_amount + ); println!(" C) Justify and print text"); println!(" D) Justify and save text"); println!(" Quit (any other key)"); @@ -31,10 +35,10 @@ fn main() { col_amount = set_col_amount(); } "c" => { - print_justified_text(); + print_justified_text(&file_name, col_amount); } "d" => { - save_justified_text(); + save_justified_text(&file_name, col_amount); } _ => { run = false; @@ -56,9 +60,8 @@ fn set_file_name() -> String { .expect("Failed to read from stdin"); path = path.trim().to_string(); - if Path::new(&path).exists() { - path_iscorrect = true; - } else { + path_iscorrect = Path::new(&path).exists(); + if !path_iscorrect { println!("The specified file does not exist. Please try again."); } } @@ -67,11 +70,98 @@ fn set_file_name() -> String { } fn set_col_amount() -> i8 { - 8 + let mut input_text: String; + let mut col_amount: i8 = 0; + let mut amount_iscorrect: bool = false; + + while !amount_iscorrect { + input_text = String::new(); + print!("Enter the desired column amount: "); + let _ = io::stdout().flush(); + io::stdin() + .read_line(&mut input_text) + .expect("failed to read from stdin"); + + match input_text.trim().parse::() { + Ok(i) => { + col_amount = i; + amount_iscorrect = col_amount >= 50 && col_amount <= 120; + if !amount_iscorrect { + println!("The specified amount was not between 50 and 120. Please try again."); + } + } + Err(..) => println!( + "input is not an integer: {}. Please try again.", + input_text.trim() + ), + }; + } + + return col_amount; } -fn print_justified_text() {} +fn print_justified_text(filename: &str, col_amount: i8) { + if let Ok(lines) = read_lines(filename) { + for line in lines.flatten() { + let justified_text = justify_text(&line, col_amount); + println!("{}", justified_text); + } + } else { + println!("Failed to read the file."); + } +} -fn save_justified_text() {} +fn save_justified_text(filename: &str, col_amount: i8) { + let output_file = "output.txt"; + let mut file = File::create(output_file).expect("Failed to create output file."); -fn load_justify_text() {} + if let Ok(lines) = read_lines(filename) { + for line in lines.flatten() { + let justified_text = justify_text(&line, col_amount); + writeln!(file, "{}", justified_text).expect("Failed to write to output file."); + } + println!("Written to {}", output_file); + } +} + +fn justify_text(text: &str, col_amount: i8) -> String { + if text.len() >= col_amount as usize { + return text.to_string(); + } + if text == "" { + return text.to_string(); + } + + let words: Vec<&str> = text.split_whitespace().collect(); + + if words.len() == 1 { + return format!("{:(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +}