Skip to content

Commit

Permalink
improvement: use helper function for terminal cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matteodev8 committed Jan 24, 2025
1 parent 23eaf08 commit 808042c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::tui::app::App;

mod tui;
mod net;
mod util;

fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
Expand All @@ -32,7 +33,6 @@ fn main() -> Result<()> {
let app = App::default();
app.run(&mut terminal, host);

stdout().execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
util::terminal::cleanup();
Ok(())
}
9 changes: 3 additions & 6 deletions src/tui/app.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::io::stdout;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use std::net::TcpStream;
use std::sync::mpsc::{channel};

use crossterm::{event, ExecutableCommand};
use crossterm::event;
use crossterm::event::{Event, KeyCode};
use crossterm::terminal::{disable_raw_mode, LeaveAlternateScreen};
use ratatui::backend::Backend;
use ratatui::style::{Color, Style};
use ratatui::Terminal;
Expand All @@ -16,7 +14,7 @@ use stblib::stbchat::net::{IncomingPacketStream, OutgoingPacketStream};

use tui_textarea::TextArea;

use crate::net;
use crate::{net, util};
use crate::tui::app::AppEvent::SendMessage;

#[derive(Default, Clone)]
Expand Down Expand Up @@ -82,8 +80,7 @@ impl App {
let stream = match TcpStream::connect(host) {
Ok(tcp_stream) => tcp_stream,
Err(_) => {
stdout().execute(LeaveAlternateScreen).unwrap();
disable_raw_mode().unwrap();
util::terminal::cleanup();
eprintln!("Server {}:{} is unreachable. Check if the server is online.", self.address, self.port);
std::process::exit(1);
}
Expand Down
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod terminal;
8 changes: 8 additions & 0 deletions src/util/terminal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::io::stdout;
use crossterm::ExecutableCommand;
use crossterm::terminal::{disable_raw_mode, LeaveAlternateScreen};

pub fn cleanup() {
stdout().execute(LeaveAlternateScreen).unwrap();
disable_raw_mode().unwrap();
}

0 comments on commit 808042c

Please sign in to comment.