Skip to content

Commit

Permalink
feat: atomic boolean panicked state
Browse files Browse the repository at this point in the history
  • Loading branch information
dhonus committed Nov 14, 2024
1 parent 2f682ab commit 2dfa4ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Client {
/// Creates a new client with the given base URL
/// If the configuration file does not exist, it will be created with stdin input
///
pub async fn new() -> Self {
pub async fn new(quiet: bool) -> Self {

let config_dir = match config_dir() {
Some(dir) => dir,
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Client {
std::process::exit(1);
}
}
} else {
} else if !quiet {
println!("[OK] Found config file at: {}", config_file.to_str().expect("[!!] Could not convert config path to string"));
}

Expand Down Expand Up @@ -156,7 +156,9 @@ impl Client {
}
};

println!("[OK] Using {} as the server.", server);
if !quiet {
println!("[OK] Using {} as the server.", server);
}

let url: String = String::new() + server + "/Users/authenticatebyname";
let response = http_client
Expand Down
23 changes: 13 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ mod library;
mod search;
use tokio;

use std::io::Write;
use std::{io::stdout, vec};
use std::env;
use std::panic;
use std::sync::atomic::{AtomicBool, Ordering};
// use serde_yaml::Value;
// use std::{collections::HashMap};

Expand Down Expand Up @@ -45,7 +45,7 @@ async fn main() {
)
);

let client = client::Client::new().await;
let client = client::Client::new(false).await;
if client.access_token.is_empty() {
println!("[XX] Failed to authenticate. Exiting...");
return;
Expand All @@ -69,13 +69,13 @@ async fn main() {
}
}

panic::set_hook(Box::new(|info| {
disable_raw_mode().ok();
stdout().flush().ok();
execute!(stdout(), LeaveAlternateScreen).ok();
eprintln!("[XX] (×_×) panik: {}", info);
let panicked = std::sync::Arc::new(AtomicBool::new(false));
let panicked_clone = panicked.clone();

panic::set_hook(Box::new(move |info| {
panicked_clone.store(true, Ordering::SeqCst);
eprintln!("\n[XX] (×_×) panik: {}", info);
eprintln!("[!!] If you think this is a bug, please report it at https://github.com/dhonus/jellyfin-tui/issues");
std::process::exit(1);
}));

let mut app = tui::App::default();
Expand All @@ -90,12 +90,15 @@ async fn main() {

loop {
app.run().await.ok();
app.draw(&mut terminal).await.ok();
if app.exit {
if app.exit || panicked.load(Ordering::SeqCst) {
disable_raw_mode().unwrap();
execute!(stdout(), LeaveAlternateScreen).unwrap();
break;
}
app.draw(&mut terminal).await.ok();
}
if panicked.load(Ordering::SeqCst) {
return;
}
println!("[OK] Exited.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl MpvState {

impl App {
pub async fn init(&mut self, artists: Vec<Artist>) {
let client = client::Client::new().await;
let client = client::Client::new(true).await;
if client.access_token.is_empty() {
panic!("[XX] Failed to authenticate. Exiting...");
}
Expand Down

0 comments on commit 2dfa4ee

Please sign in to comment.