improve formatting, use int for setting volume

This commit is contained in:
Crispy 2023-01-20 22:51:58 +01:00
parent e239540a97
commit 6a74191833

View file

@ -16,13 +16,39 @@ fn main() {
struct UIChannel { struct UIChannel {
name: String, name: String,
volume: f32, volume: i32,
internal_volume: Arc<Mutex<f32>>, internal_volume: Arc<Mutex<f32>>,
muted: bool,
}
impl UIChannel {
fn mute(&mut self) {
if self.muted {
self.sync();
} else {
*self.internal_volume.lock().unwrap() = 0.0;
}
self.muted = !self.muted;
}
fn get_vol(&self) -> f32 {
self.volume as f32 / 100.0
}
fn change_vol(&mut self, amt: i32) {
self.muted = false;
self.volume = (self.volume + amt).clamp(0, 200);
self.sync();
}
fn sync(&mut self) {
*self.internal_volume.lock().unwrap() = self.get_vol();
}
} }
struct App { struct App {
channels: Vec<UIChannel>, channels: Vec<UIChannel>,
selected_index: usize, selected: usize,
_stream: OutputStream, _stream: OutputStream,
stream_handle: OutputStreamHandle, stream_handle: OutputStreamHandle,
quit: bool, quit: bool,
@ -34,7 +60,7 @@ impl App {
.expect("Failed to create output stream"); .expect("Failed to create output stream");
Self { Self {
channels: Vec::new(), channels: Vec::new(),
selected_index: 0, selected: 0,
_stream, _stream,
stream_handle, stream_handle,
quit: false, quit: false,
@ -54,8 +80,9 @@ impl App {
let internal_volume = snoud.add_channel(&filename); let internal_volume = snoud.add_channel(&filename);
let ui_channel = UIChannel { let ui_channel = UIChannel {
name: filename, name: filename,
volume: 1.0, volume: 100,
internal_volume, internal_volume,
muted: false,
}; };
self.channels.push(ui_channel); self.channels.push(ui_channel);
} }
@ -72,6 +99,7 @@ impl App {
self.input(); self.input();
} }
terminal::disable_raw_mode().unwrap(); terminal::disable_raw_mode().unwrap();
println!("Exiting");
} }
fn render(&mut self) { fn render(&mut self) {
@ -80,10 +108,19 @@ impl App {
println!("Snoud - ambient sound player\n\r"); println!("Snoud - ambient sound player\n\r");
for (i, channel) in self.channels.iter().enumerate() { for (i, channel) in self.channels.iter().enumerate() {
println!( println!(
"{}{}:\r\n {:3.0}%\r\n", "{selection}{name}:\r\n {volume:3.0}% {status:-<21}\r\n",
if i == self.selected_index { ">" } else { " " }, selection = if i == self.selected { ">" } else { " " },
&channel.name, name = &channel.name,
(channel.volume * 100.0) volume = channel.volume,
status = if channel.muted {
"[Muted]".to_owned()
} else {
format!(
"{:=>width$}",
"|",
width = (channel.volume / 10 + 1) as usize
)
}
); );
} }
} }
@ -103,26 +140,21 @@ impl App {
KeyCode::Char('q') => self.quit = true, KeyCode::Char('q') => self.quit = true,
KeyCode::Up => self.select_prev(), KeyCode::Up => self.select_prev(),
KeyCode::Down => self.select_next(), KeyCode::Down => self.select_next(),
KeyCode::Right => self.set_channel_volume(0.1), KeyCode::Right => self.channels[self.selected].change_vol(10),
KeyCode::Left => self.set_channel_volume(-0.1), KeyCode::Left => self.channels[self.selected].change_vol(-10),
KeyCode::Char(' ' | 'm') => self.channels[self.selected].mute(),
_ => (), _ => (),
} }
} }
fn set_channel_volume(&mut self, delta: f32) {
let channel = self.channels.get_mut(self.selected_index).unwrap();
channel.volume = (channel.volume + delta).clamp(0., 2.);
*channel.internal_volume.lock().unwrap() = channel.volume;
}
fn select_prev(&mut self) { fn select_prev(&mut self) {
self.selected_index = match self.selected_index { self.selected = match self.selected {
0 => self.channels.len() - 1, 0 => self.channels.len() - 1,
n => n - 1, n => n - 1,
}; };
} }
fn select_next(&mut self) { fn select_next(&mut self) {
self.selected_index = (self.selected_index + 1) % self.channels.len(); self.selected = (self.selected + 1) % self.channels.len();
} }
} }