Krolyxon
Commit f7894f1

rmptuif7894f1

add toggle mute keymap

Author
krolxon
Date
2024-06-11 21:49 +0530
Parent
08c4f41
Commit
f7894f173adcf59e6b768c7a40222956b927d6ec

+36 −1

4 files changed

  1. M README.md +1 −0
  2. M src/app.rs +1 −1
  3. M src/connection.rs +17 −0
  4. M src/event/handler.rs +17 −0
M README.md View
@@ -15,6 +15,7 @@ rmptui is a minimal tui mpd client made with rust.
1515 | `p` | Toggle pause |
1616 | `+`/`=` | Increase volume |
1717 | `-` | Decrease volume |
18+| `m` | Toggle Mute |
1819 | `D` | Get dmenu prompt |
1920 | `j`/`Down` | Scroll down |
2021 | `k`/`Up` | Scroll up |
M src/app.rs View
@@ -429,7 +429,7 @@ impl App {
429429 }
430430 }
431431
432− pub fn handle_mouse_left_click(&mut self, x: u16, y: u16) -> AppResult<()> {
432+ pub fn handle_mouse_left_click(&mut self, _x: u16, _y: u16) -> AppResult<()> {
433433 Ok(())
434434 }
435435 }
M src/connection.rs View
@@ -5,6 +5,12 @@ use mpd::{Client, State};
55 use simple_dmenu::dmenu;
66 use std::time::Duration;
77
8+#[derive(Debug)]
9+pub enum VolumeStatus {
10+ Muted(i8),
11+ Unmuted,
12+}
13+
814 #[derive(Debug)]
915 /// struct storing the mpd Client related stuff
1016 pub struct Connection {
@@ -16,6 +22,7 @@ pub struct Connection {
1622 pub current_song: Song,
1723 pub stats: mpd::Stats,
1824 pub status: mpd::Status,
25+ pub volume_status: VolumeStatus,
1926 }
2027
2128 impl Connection {
@@ -41,6 +48,13 @@ impl Connection {
4148 .currentsong()
4249 .unwrap_or_else(|_| Some(empty_song.clone()))
4350 .unwrap_or(empty_song);
51+
52+ let volume_status = if status.volume == 0 {
53+ VolumeStatus::Muted(status.volume)
54+ } else {
55+ VolumeStatus::Unmuted
56+ };
57+
4458 Ok(Self {
4559 conn,
4660 songs_filenames,
@@ -50,6 +64,7 @@ impl Connection {
5064 current_song,
5165 stats,
5266 status,
67+ volume_status,
5368 })
5469 }
5570
@@ -207,6 +222,7 @@ impl Connection {
207222 pub fn inc_volume(&mut self, v: i8) {
208223 let cur = self.status.volume;
209224 if cur + v <= 100 {
225+ self.volume_status = VolumeStatus::Unmuted;
210226 self.conn.volume(cur + v).unwrap();
211227 }
212228 }
@@ -215,6 +231,7 @@ impl Connection {
215231 pub fn dec_volume(&mut self, v: i8) {
216232 let cur = self.status.volume;
217233 if cur - v >= 0 {
234+ self.volume_status = VolumeStatus::Unmuted;
218235 self.conn.volume(cur - v).unwrap();
219236 }
220237 }
M src/event/handler.rs View
@@ -1,5 +1,6 @@
11 use crate::{
22 app::{App, AppResult, SelectedTab},
3+ connection::VolumeStatus,
34 ui::InputMode,
45 };
56 use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind};
@@ -138,6 +139,22 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
138139 app.conn.update_status();
139140 }
140141
142+ // Toggle Mute
143+ KeyCode::Char('m') => {
144+ match app.conn.volume_status {
145+ VolumeStatus::Muted(v) => {
146+ app.conn.conn.volume(v)?;
147+ app.conn.volume_status = VolumeStatus::Unmuted;
148+ }
149+ VolumeStatus::Unmuted => {
150+ let current_volume = app.conn.status.volume;
151+ app.conn.conn.volume(0)?;
152+ app.conn.volume_status = VolumeStatus::Muted(current_volume);
153+ }
154+ }
155+ app.conn.update_status();
156+ }
157+
141158 // Update MPD database
142159 KeyCode::Char('U') => {
143160 app.conn.conn.rescan()?;