Skip to content

Commit

Permalink
use AtomicBool
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabus1184 committed Nov 16, 2023
1 parent 4f1abb6 commit b130052
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod status;
mod tabs;

use std::{
sync::{mpsc, Arc, Mutex, RwLock},
sync::{atomic::AtomicBool, mpsc, Arc, Mutex, RwLock},
time::Duration,
};

Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn tui<'a>(
enable_raw_mode()?;
terminal.clear()?;

let running = Mutex::new(true);
let running = Arc::new(AtomicBool::new(true));
let mut tabs = Tabs::new(
vec![
(
Expand All @@ -79,7 +79,7 @@ pub fn tui<'a>(
),
("Fancy stuff ✨ ", Box::new(Fancy::new(player.clone()))),
],
&running,
running.clone(),
);

let usage = Status::new(player.clone());
Expand All @@ -99,7 +99,7 @@ pub fn tui<'a>(
tabs.input(&event::read()?)?;
}

if !*running.lock().unwrap() {
if !running.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/tui/tabs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Mutex;
use std::sync::{atomic::AtomicBool, Arc};

use crossterm::event::{Event, KeyCode, KeyEvent};
use log::trace;
Expand All @@ -15,11 +15,11 @@ use super::Tui;
pub struct Tabs<'a> {
pub selected: usize,
pub tabs: Vec<(&'static str, Box<dyn Tui + 'a>)>,
running: &'a Mutex<bool>,
running: Arc<AtomicBool>,
}

impl<'a> Tabs<'a> {
pub fn new(tabs: Vec<(&'static str, Box<dyn Tui + 'a>)>, running: &'a Mutex<bool>) -> Self {
pub fn new(tabs: Vec<(&'static str, Box<dyn Tui + 'a>)>, running: Arc<AtomicBool>) -> Self {
Self {
selected: 0,
tabs,
Expand Down Expand Up @@ -86,8 +86,8 @@ impl Tui for Tabs<'_> {
self.selected = (self.selected.wrapping_sub(1)) % self.tabs.len();
}
KeyCode::Char('q') => {
trace!("locking player");
*self.running.lock().unwrap() = false;
self.running
.store(false, std::sync::atomic::Ordering::Relaxed);
}
_ => {
let content = self.tabs.get_mut(self.selected).expect("Tab not found");
Expand Down

0 comments on commit b130052

Please sign in to comment.