Skip to content

Commit

Permalink
Merge pull request #13 from tsirysndr/fix/state-mutex
Browse files Browse the repository at this point in the history
fix: don't use mutex for the app state
  • Loading branch information
tsirysndr authored Feb 29, 2024
2 parents e510655 + 35b1956 commit 63cbabc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "tunein-cli"
readme = "README.md"
repository = "https://github.com/tsirysndr/tunein-cli"
version = "0.2.0"
version = "0.2.1"

[[bin]]
name = "tunein"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ nix profile install --experimental-features "nix-command flakes" github:tsirysnd
Or download the latest release for your platform [here](https://github.com/tsirysndr/tunein-cli/releases).

## 📦 Downloads
- `Mac`: arm64: [tunein_v0.2.0_aarch64-apple-darwin.tar.gz](https://github.com/tsirysndr/tunein-cli/releases/download/v0.2.0/tunein_v0.2.0_aarch64-apple-darwin.tar.gz) intel: [tunein_v0.2.0_x86_64-apple-darwin.tar.gz](https://github.com/tsirysndr/tunein-cli/releases/download/v0.2.0/tunein_v0.2.0_x86_64-apple-darwin.tar.gz)
- `Linux`: [tunein_v0.2.0_x86_64-unknown-linux-gnu.tar.gz](https://github.com/tsirysndr/tunein-cli/releases/download/v0.2.0/tunein_v0.2.0_x86_64-unknown-linux-gnu.tar.gz)
- `Mac`: arm64: [tunein_v0.2.1_aarch64-apple-darwin.tar.gz](https://github.com/tsirysndr/tunein-cli/releases/download/v0.2.1/tunein_v0.2.1_aarch64-apple-darwin.tar.gz) intel: [tunein_v0.2.1_x86_64-apple-darwin.tar.gz](https://github.com/tsirysndr/tunein-cli/releases/download/v0.2.1/tunein_v0.2.1_x86_64-apple-darwin.tar.gz)
- `Linux`: [tunein_v0.2.1_x86_64-unknown-linux-gnu.tar.gz](https://github.com/tsirysndr/tunein-cli/releases/download/v0.2.1/tunein_v0.2.1_x86_64-unknown-linux-gnu.tar.gz)
## 🚀 Usage
```
USAGE:
Expand Down
45 changes: 22 additions & 23 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
io,
sync::{Arc, Mutex},
time::Duration,
};
use std::{io, time::Duration};

use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{
Expand All @@ -22,21 +18,15 @@ pub struct State {
pub br: String,
}

impl State {
pub fn update(&mut self, state: State) {
*self = state;
}
}

#[derive(Debug, Default)]
pub struct App {
pub state: Arc<Mutex<State>>,
pub state: State,
exit: bool,
}

impl App {
pub fn new() -> Self {
let state = Arc::new(Mutex::new(State::default()));
let state = State::default();
Self { state, exit: false }
}
}
Expand All @@ -50,12 +40,18 @@ impl App {
id: &str,
) -> anyhow::Result<()> {
let new_state = cmd_rx.recv().await.unwrap();
self.state.lock().unwrap().update(new_state);

while !self.exit {
self.state = new_state.clone();

// Get current playing if available, otherwise use state's value
let now_playing = get_currently_playing(id)
.await
.unwrap_or(self.state.lock().unwrap().now_playing.clone());
self.state.lock().unwrap().now_playing = now_playing;
.unwrap_or_else(|_| self.state.now_playing.clone());

// Update state with current playing
self.state.now_playing = now_playing;

terminal.draw(|frame| self.render_frame(frame))?;
self.handle_events()?;
std::thread::sleep(Duration::from_millis(500));
Expand Down Expand Up @@ -85,13 +81,16 @@ impl App {
areas[0],
);

let state = self.state.lock().unwrap();

self.render_line("Station ", &state.name, areas[1], frame);
self.render_line("Now Playing ", &state.now_playing, areas[2], frame);
self.render_line("Genre ", &state.genre, areas[3], frame);
self.render_line("Description ", &state.description, areas[4], frame);
self.render_line("Bitrate ", &format!("{} kbps", &state.br), areas[5], frame);
self.render_line("Station ", &self.state.name, areas[1], frame);
self.render_line("Now Playing ", &self.state.now_playing, areas[2], frame);
self.render_line("Genre ", &self.state.genre, areas[3], frame);
self.render_line("Description ", &self.state.description, areas[4], frame);
self.render_line(
"Bitrate ",
&format!("{} kbps", &self.state.br),
areas[5],
frame,
);
}

fn render_line(&self, label: &str, value: &str, area: Rect, frame: &mut Frame) {
Expand Down

0 comments on commit 63cbabc

Please sign in to comment.