From 621c8274bc87f8c85e4e4783807a5a7d300a2fb5 Mon Sep 17 00:00:00 2001 From: srdusr <99972264+srdusr@users.noreply.github.com> Date: Fri, 18 Jul 2025 01:02:00 +0200 Subject: Fixed not being able to read the sentences.txt file, fixed linter diagnostics messages with test file, better handling of exiting the game and going back to terminal by sending ANSI escape sequences to clear terminal --- src/main.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 1312443..6fbd3e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use rand::Rng; use std::{ fs, io::{self}, + path::Path, sync::{Arc, Mutex}, time::Instant, }; @@ -106,6 +107,10 @@ impl App { // Function to read sentences from a file fn read_sentences(filename: &str) -> Result, io::Error> { + if !Path::new(filename).exists() { + return Err(io::Error::new(io::ErrorKind::NotFound, "File not found")); + } + let contents = fs::read_to_string(filename)?; let sentences: Vec = contents.lines().map(|s| s.to_string()).collect(); Ok(sentences) @@ -190,16 +195,19 @@ async fn input_handler(event: KeyEvent, app: &mut App, _event_tx: Arc> } // Include test module +#[cfg(test)] mod test; -// Main function #[tokio::main] async fn main() -> Result<(), io::Error> { // Enable raw mode for terminal input enable_raw_mode()?; + // Print current working directory + println!("Current working directory: {:?}", std::env::current_dir()); + // Create a new instance of the App - let mut app = App::new().expect("Error initializing app"); + let mut app = App::new().unwrap(); // Initialize the terminal backend let stdout = io::stdout(); @@ -276,13 +284,24 @@ async fn main() -> Result<(), io::Error> { } } - // Cleanup: Show cursor, disable raw mode, and clear terminal + // Cleanup: Show cursor, disable raw mode, and clear only the game UI terminal.show_cursor()?; disable_raw_mode()?; - terminal.clear()?; + let size = terminal.backend().size().unwrap(); + let chunks = Layout::default() + .direction(Direction::Vertical) + .margin(2) + .constraints([ + Constraint::Min(3), + Constraint::Percentage(70), + Constraint::Min(3), + ]) + .split(size); + terminal + .draw(|f| f.render_widget(Paragraph::new("").alignment(Alignment::Center), chunks[1]))?; - // Additional clear to remove any leftover characters - println!(); + // Manually clear the terminal before exiting + println!("\x1B[2J\x1B[1;1H"); Ok(()) } -- cgit v1.2.3