tangled
alpha
login
or
join now
tsiry-sandratraina.com
/
tunein-cli
6
fork
atom
Browse and listen to thousands of radio stations across the globe right from your terminal ๐ ๐ป ๐ตโจ
radio
rust
tokio
web-radio
command-line-tool
tui
6
fork
atom
overview
issues
pulls
pipelines
app: refactor: pull event actions to functions
ishbosamiya
7 months ago
477ec0f8
4751cc35
+64
-58
1 changed file
expand all
collapse all
unified
split
src
app.rs
+64
-58
src/app.rs
···
434
434
) -> Result<bool, io::Error> {
435
435
let mut quit = false;
436
436
437
437
-
let play = |graph: &mut GraphConfig| {
438
438
-
graph.pause = false;
439
439
-
sink_cmd_tx
440
440
-
.send(SinkCommand::Play)
441
441
-
.expect("receiver never dropped");
442
442
-
};
443
443
-
444
444
-
let pause = |graph: &mut GraphConfig| {
445
445
-
graph.pause = true;
446
446
-
sink_cmd_tx
447
447
-
.send(SinkCommand::Pause)
448
448
-
.expect("receiver never dropped");
449
449
-
};
450
450
-
451
451
-
let toggle_play_pause = |graph: &mut GraphConfig| {
452
452
-
graph.pause = !graph.pause;
453
453
-
let sink_cmd = if graph.pause {
454
454
-
SinkCommand::Pause
455
455
-
} else {
456
456
-
SinkCommand::Play
457
457
-
};
458
458
-
sink_cmd_tx.send(sink_cmd).expect("receiver never dropped");
459
459
-
};
460
460
-
461
461
-
let lower_volume = || {
462
462
-
let mut state = state.lock().unwrap();
463
463
-
state.volume.change_volume(-1.0);
464
464
-
sink_cmd_tx
465
465
-
.send(SinkCommand::SetVolume(state.volume.volume_ratio()))
466
466
-
.expect("receiver never dropped");
467
467
-
};
468
468
-
469
469
-
let raise_volume = || {
470
470
-
let mut state = state.lock().unwrap();
471
471
-
state.volume.change_volume(1.0);
472
472
-
sink_cmd_tx
473
473
-
.send(SinkCommand::SetVolume(state.volume.volume_ratio()))
474
474
-
.expect("receiver never dropped");
475
475
-
};
476
476
-
477
477
-
let mute_volume = || {
478
478
-
let mut state = state.lock().unwrap();
479
479
-
state.volume.toggle_mute();
480
480
-
sink_cmd_tx
481
481
-
.send(SinkCommand::SetVolume(state.volume.volume_ratio()))
482
482
-
.expect("receiver never dropped");
483
483
-
};
484
484
-
485
437
if let Event::Key(key) = event {
486
438
if let KeyModifiers::CONTROL = key.modifiers {
487
439
match key.code {
···
500
452
KeyCode::Up => {
501
453
// inverted to act as zoom
502
454
update_value_f(&mut self.graph.scale, 0.01, magnitude, 0.0..10.0);
503
503
-
raise_volume();
455
455
+
raise_volume(&state, sink_cmd_tx);
504
456
}
505
457
KeyCode::Down => {
506
458
// inverted to act as zoom
507
459
update_value_f(&mut self.graph.scale, -0.01, magnitude, 0.0..10.0);
508
508
-
lower_volume();
460
460
+
lower_volume(&state, sink_cmd_tx);
509
461
}
510
462
KeyCode::Right => update_value_i(
511
463
&mut self.graph.samples,
···
522
474
0..self.graph.width * 2,
523
475
),
524
476
KeyCode::Char('q') => quit = true,
525
525
-
KeyCode::Char(' ') => toggle_play_pause(&mut self.graph),
477
477
+
KeyCode::Char(' ') => toggle_play_pause(&mut self.graph, sink_cmd_tx),
526
478
KeyCode::Char('s') => self.graph.scatter = !self.graph.scatter,
527
479
KeyCode::Char('h') => self.graph.show_ui = !self.graph.show_ui,
528
480
KeyCode::Char('r') => self.graph.references = !self.graph.references,
529
529
-
KeyCode::Char('m') => mute_volume(),
481
481
+
KeyCode::Char('m') => mute_volume(&state, sink_cmd_tx),
530
482
KeyCode::Esc => {
531
483
self.graph.samples = self.graph.width;
532
484
self.graph.scale = 1.;
···
552
504
}
553
505
}
554
506
KeyCode::Media(media_key_code) => match media_key_code {
555
555
-
MediaKeyCode::Play => play(&mut self.graph),
556
556
-
MediaKeyCode::Pause => pause(&mut self.graph),
557
557
-
MediaKeyCode::PlayPause => toggle_play_pause(&mut self.graph),
507
507
+
MediaKeyCode::Play => play(&mut self.graph, sink_cmd_tx),
508
508
+
MediaKeyCode::Pause => pause(&mut self.graph, sink_cmd_tx),
509
509
+
MediaKeyCode::PlayPause => toggle_play_pause(&mut self.graph, sink_cmd_tx),
558
510
MediaKeyCode::Stop => {
559
511
quit = true;
560
512
}
561
561
-
MediaKeyCode::LowerVolume => lower_volume(),
562
562
-
MediaKeyCode::RaiseVolume => raise_volume(),
563
563
-
MediaKeyCode::MuteVolume => mute_volume(),
513
513
+
MediaKeyCode::LowerVolume => lower_volume(&state, sink_cmd_tx),
514
514
+
MediaKeyCode::RaiseVolume => raise_volume(&state, sink_cmd_tx),
515
515
+
MediaKeyCode::MuteVolume => mute_volume(&state, sink_cmd_tx),
564
516
MediaKeyCode::TrackNext
565
517
| MediaKeyCode::TrackPrevious
566
518
| MediaKeyCode::Reverse
···
635
587
)
636
588
.style(Style::default().fg(cfg.labels_color))
637
589
}
590
590
+
591
591
+
/// Play music.
592
592
+
fn play(graph: &mut GraphConfig, sink_cmd_tx: &UnboundedSender<SinkCommand>) {
593
593
+
graph.pause = false;
594
594
+
sink_cmd_tx
595
595
+
.send(SinkCommand::Play)
596
596
+
.expect("receiver never dropped");
597
597
+
}
598
598
+
599
599
+
/// Pause music.
600
600
+
fn pause(graph: &mut GraphConfig, sink_cmd_tx: &UnboundedSender<SinkCommand>) {
601
601
+
graph.pause = true;
602
602
+
sink_cmd_tx
603
603
+
.send(SinkCommand::Pause)
604
604
+
.expect("receiver never dropped");
605
605
+
}
606
606
+
607
607
+
/// Toggle between play and pause.
608
608
+
fn toggle_play_pause(graph: &mut GraphConfig, sink_cmd_tx: &UnboundedSender<SinkCommand>) {
609
609
+
graph.pause = !graph.pause;
610
610
+
let sink_cmd = if graph.pause {
611
611
+
SinkCommand::Pause
612
612
+
} else {
613
613
+
SinkCommand::Play
614
614
+
};
615
615
+
sink_cmd_tx.send(sink_cmd).expect("receiver never dropped");
616
616
+
}
617
617
+
618
618
+
/// Lower the volume.
619
619
+
fn lower_volume(state: &Arc<Mutex<State>>, sink_cmd_tx: &UnboundedSender<SinkCommand>) {
620
620
+
let mut state = state.lock().unwrap();
621
621
+
state.volume.change_volume(-1.0);
622
622
+
sink_cmd_tx
623
623
+
.send(SinkCommand::SetVolume(state.volume.volume_ratio()))
624
624
+
.expect("receiver never dropped");
625
625
+
}
626
626
+
627
627
+
/// Raise the volume.
628
628
+
fn raise_volume(state: &Arc<Mutex<State>>, sink_cmd_tx: &UnboundedSender<SinkCommand>) {
629
629
+
let mut state = state.lock().unwrap();
630
630
+
state.volume.change_volume(1.0);
631
631
+
sink_cmd_tx
632
632
+
.send(SinkCommand::SetVolume(state.volume.volume_ratio()))
633
633
+
.expect("receiver never dropped");
634
634
+
}
635
635
+
636
636
+
/// Mute the volume.
637
637
+
fn mute_volume(state: &Arc<Mutex<State>>, sink_cmd_tx: &UnboundedSender<SinkCommand>) {
638
638
+
let mut state = state.lock().unwrap();
639
639
+
state.volume.toggle_mute();
640
640
+
sink_cmd_tx
641
641
+
.send(SinkCommand::SetVolume(state.volume.volume_ratio()))
642
642
+
.expect("receiver never dropped");
643
643
+
}