diff options
| author | srdusr <trevorgray@srdusr.com> | 2024-03-08 19:36:57 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2024-03-08 19:39:20 +0200 |
| commit | e7f21de466d0bf37a2c629426122e703b980402c (patch) | |
| tree | 6483ba21bf1d3f7f99e032b44a8a2150263a2a64 | |
| parent | b86386dd5c57c490218e50dbaea4fd90c9375539 (diff) | |
| download | typerpunk-e7f21de466d0bf37a2c629426122e703b980402c.tar.gz typerpunk-e7f21de466d0bf37a2c629426122e703b980402c.zip | |
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
| -rw-r--r-- | src/main.rs | 31 |
1 files changed, 25 insertions, 6 deletions
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<Vec<String>, 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<String> = 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<Mutex<()>> } // 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(()) } |
