add documentation
This commit is contained in:
parent
05c4b17bc0
commit
b93eae4482
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::connection::{self, Connection};
|
use crate::connection::Connection;
|
||||||
use crate::list::ContentList;
|
use crate::list::ContentList;
|
||||||
use mpd::Client;
|
use mpd::Client;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ pub struct Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
|
/// Create a new connection
|
||||||
pub fn new(addrs: &str) -> Result<Self> {
|
pub fn new(addrs: &str) -> Result<Self> {
|
||||||
let mut conn = Client::connect(addrs).unwrap();
|
let mut conn = Client::connect(addrs).unwrap();
|
||||||
let songs_filenames: Vec<String> = conn
|
let songs_filenames: Vec<String> = conn
|
||||||
|
|
@ -30,6 +31,7 @@ impl Connection {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fzf prompt for selecting song
|
||||||
pub fn play_fzf(&mut self) -> Result<()> {
|
pub fn play_fzf(&mut self) -> Result<()> {
|
||||||
is_installed("fzf").map_err(|ex| ex)?;
|
is_installed("fzf").map_err(|ex| ex)?;
|
||||||
|
|
||||||
|
|
@ -42,6 +44,7 @@ impl Connection {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Dmenu prompt for selecting songs
|
||||||
pub fn play_dmenu(&mut self) -> Result<()> {
|
pub fn play_dmenu(&mut self) -> Result<()> {
|
||||||
is_installed("dmenu").map_err(|ex| ex)?;
|
is_installed("dmenu").map_err(|ex| ex)?;
|
||||||
let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
|
let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
|
||||||
|
|
@ -60,6 +63,7 @@ impl Connection {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
/// push the given song to queue
|
||||||
pub fn push(&mut self, song: &Song) -> Result<()> {
|
pub fn push(&mut self, song: &Song) -> Result<()> {
|
||||||
if self.conn.queue().unwrap().is_empty() {
|
if self.conn.queue().unwrap().is_empty() {
|
||||||
self.conn.push(song).unwrap();
|
self.conn.push(song).unwrap();
|
||||||
|
|
@ -75,6 +79,7 @@ impl Connection {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Push all songs of a playlist into queue
|
||||||
pub fn push_playlist(&mut self, playlist: &str) -> Result<()> {
|
pub fn push_playlist(&mut self, playlist: &str) -> Result<()> {
|
||||||
let songs: Vec<Song> = self.conn.playlist(playlist)?;
|
let songs: Vec<Song> = self.conn.playlist(playlist)?;
|
||||||
|
|
||||||
|
|
@ -88,6 +93,7 @@ impl Connection {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Given a filename, get instance of Song with only filename
|
||||||
pub fn get_song_with_only_filename(&self, filename: &str) -> Song {
|
pub fn get_song_with_only_filename(&self, filename: &str) -> Song {
|
||||||
Song {
|
Song {
|
||||||
file: filename.to_string(),
|
file: filename.to_string(),
|
||||||
|
|
@ -102,9 +108,12 @@ impl Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// get current playing song
|
||||||
pub fn get_current_song(&mut self) -> Option<String> {
|
pub fn get_current_song(&mut self) -> Option<String> {
|
||||||
self.conn.currentsong().unwrap().unwrap_or_default().title
|
self.conn.currentsong().unwrap().unwrap_or_default().title
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Print status to stdout
|
||||||
pub fn status(&mut self) {
|
pub fn status(&mut self) {
|
||||||
let current_song = self.conn.currentsong();
|
let current_song = self.conn.currentsong();
|
||||||
let status = self.conn.status().unwrap();
|
let status = self.conn.status().unwrap();
|
||||||
|
|
@ -121,29 +130,33 @@ impl Connection {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn now_playing(&mut self) -> Option<String> {
|
/// Gives title of current playing song
|
||||||
let song = self.conn.currentsong().unwrap().unwrap_or_default();
|
pub fn now_playing(&mut self) -> Result<Option<String>> {
|
||||||
|
let song = self.conn.currentsong()?.unwrap();
|
||||||
if let Some(s) = song.title {
|
if let Some(s) = song.title {
|
||||||
if let Some(a) = song.artist {
|
if let Some(a) = song.artist {
|
||||||
Some(format!("{} - {}", s, a))
|
return Ok(Some(format!("{} - {}", s, a)));
|
||||||
} else {
|
} else {
|
||||||
Some(s)
|
return Ok(Some(s));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
return Ok(Some(song.file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Playback controls
|
// Playback controls
|
||||||
|
/// Pause playback
|
||||||
pub fn pause(&mut self) {
|
pub fn pause(&mut self) {
|
||||||
self.conn.pause(true).unwrap();
|
self.conn.pause(true).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Toggles playback
|
||||||
pub fn toggle_pause(&mut self) {
|
pub fn toggle_pause(&mut self) {
|
||||||
self.conn.toggle_pause().unwrap();
|
self.conn.toggle_pause().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Volume controls
|
// Volume controls
|
||||||
|
/// Sets the volume
|
||||||
pub fn set_volume(&mut self, u: String) {
|
pub fn set_volume(&mut self, u: String) {
|
||||||
let cur = self.conn.status().unwrap().volume;
|
let cur = self.conn.status().unwrap().volume;
|
||||||
let sym = u.get(0..1).unwrap();
|
let sym = u.get(0..1).unwrap();
|
||||||
|
|
@ -156,6 +169,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the index of the string from the Vector
|
||||||
fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
|
fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
|
||||||
let mut choice: usize = 0;
|
let mut choice: usize = 0;
|
||||||
if let Some(index) = ss.iter().position(|s| s == selection) {
|
if let Some(index) = ss.iter().position(|s| s == selection) {
|
||||||
|
|
@ -165,6 +179,7 @@ fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
|
||||||
choice
|
choice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if given program is installed in your system
|
||||||
fn is_installed(ss: &str) -> Result<()> {
|
fn is_installed(ss: &str) -> Result<()> {
|
||||||
let output = Command::new("which")
|
let output = Command::new("which")
|
||||||
.arg(ss)
|
.arg(ss)
|
||||||
|
|
|
||||||
14
src/list.rs
14
src/list.rs
|
|
@ -1,20 +1,28 @@
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ContentList<T> {
|
pub struct ContentList<T> {
|
||||||
pub list: Vec<T>,
|
pub list: Vec<T>,
|
||||||
pub index: usize
|
pub index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ContentList<T> {
|
impl<T> ContentList<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
ContentList {
|
ContentList {
|
||||||
list: Vec::new(),
|
list: Vec::new(),
|
||||||
index: 0
|
index: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go to next item in list
|
// Go to next item in list
|
||||||
pub fn next(&mut self) {
|
pub fn next(&mut self) {
|
||||||
self.index += 1;
|
// if self.index < self.list.len() - 1 {
|
||||||
|
// self.index += 1;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if self.index == self.list.len() - 1 {
|
||||||
|
self.index = 0;
|
||||||
|
} else {
|
||||||
|
self.index += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Go to previous item in list
|
/// Go to previous item in list
|
||||||
|
|
|
||||||
79
src/ui.rs
79
src/ui.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::app::App;
|
use crate::app::{App, AppResult};
|
||||||
use ratatui::{prelude::*, widgets::*};
|
use ratatui::{prelude::*, widgets::*};
|
||||||
|
|
||||||
/// Renders the user interface widgets
|
/// Renders the user interface widgets
|
||||||
|
|
@ -8,35 +8,30 @@ pub fn render(app: &mut App, frame: &mut Frame) {
|
||||||
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
|
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
|
||||||
// - https://github.com/ratatui-org/ratatui/tree/master/examples
|
// - https://github.com/ratatui-org/ratatui/tree/master/examples
|
||||||
|
|
||||||
// List of songs
|
// Layout
|
||||||
let mut song_state = ListState::default();
|
let main_layout = Layout::default()
|
||||||
let size = Rect::new(100, 0, frame.size().width, frame.size().height - 3);
|
.direction(Direction::Vertical)
|
||||||
let list = List::new(app.conn.songs_filenames.clone())
|
.constraints(vec![Constraint::Percentage(93), Constraint::Percentage(7)])
|
||||||
.block(Block::default().title("Song List").borders(Borders::ALL))
|
.split(frame.size());
|
||||||
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
|
||||||
.highlight_symbol(">>")
|
|
||||||
.repeat_highlight_symbol(true);
|
|
||||||
|
|
||||||
song_state.select(Some(app.song_list.index));
|
let outer_layout = Layout::default()
|
||||||
frame.render_stateful_widget(list, size, &mut song_state);
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
|
.split(main_layout[0]);
|
||||||
|
|
||||||
// Play Queue
|
let inner_layout = Layout::default()
|
||||||
let mut queue_state = ListState::default();
|
.direction(Direction::Vertical)
|
||||||
let size = Rect::new(0, 0, 100, frame.size().height - 25);
|
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
let list = List::new(app.play_deque.clone())
|
.split(outer_layout[1]);
|
||||||
.block(Block::default().title("Play Queue").borders(Borders::ALL))
|
|
||||||
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
|
||||||
.highlight_symbol(">>")
|
|
||||||
.repeat_highlight_symbol(true);
|
|
||||||
|
|
||||||
app.update_queue();
|
draw_song_list(frame, app, outer_layout[0]);
|
||||||
frame.render_stateful_widget(list, size, &mut queue_state);
|
draw_queue(frame, app, inner_layout[0]);
|
||||||
|
draw_playlists(frame, app, inner_layout[1]);
|
||||||
|
|
||||||
// Status
|
// Status
|
||||||
// let size = Rect::new(0, frame.size().height - 3, frame.size().width, 3);
|
|
||||||
// let song = app
|
// let song = app
|
||||||
// .conn
|
// .conn
|
||||||
// .now_playing()
|
// .now_playing().unwrap()
|
||||||
// .unwrap_or_else(|| "No Title Found".to_string());
|
// .unwrap_or_else(|| "No Title Found".to_string());
|
||||||
//
|
//
|
||||||
// let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap();
|
// let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap();
|
||||||
|
|
@ -45,15 +40,45 @@ pub fn render(app: &mut App, frame: &mut Frame) {
|
||||||
// lines.push(Line::from(vec![
|
// lines.push(Line::from(vec![
|
||||||
// Span::styled("Current: ", Style::default().fg(Color::Red)),
|
// Span::styled("Current: ", Style::default().fg(Color::Red)),
|
||||||
// Span::styled(song, Style::default().fg(Color::Yellow)),
|
// Span::styled(song, Style::default().fg(Color::Yellow)),
|
||||||
// Span::styled(format!("[{}/{}]", elapsed.as_secs(), total.as_secs()), Style::default().fg(Color::Yellow)),
|
// Span::styled(
|
||||||
|
// format!("[{}/{}]", elapsed.as_secs(), total.as_secs()),
|
||||||
|
// Style::default().fg(Color::Yellow),
|
||||||
|
// ),
|
||||||
// ]));
|
// ]));
|
||||||
// let status = Paragraph::new(Text::from(lines))
|
// let status = Paragraph::new(Text::from(lines))
|
||||||
// .block(Block::default().title("Status").borders(Borders::ALL));
|
// .block(Block::default().title("Status").borders(Borders::ALL));
|
||||||
// frame.render_widget(status, size);
|
// frame.render_widget(status, main_layout[1]);
|
||||||
|
}
|
||||||
|
|
||||||
// Playlists
|
/// draws list of songs
|
||||||
|
fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) {
|
||||||
|
let mut song_state = ListState::default();
|
||||||
|
let list = List::new(app.conn.songs_filenames.clone())
|
||||||
|
.block(Block::default().title("Song List").borders(Borders::ALL))
|
||||||
|
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
||||||
|
.highlight_symbol(">>")
|
||||||
|
.repeat_highlight_symbol(true);
|
||||||
|
|
||||||
|
song_state.select(Some(app.song_list.index));
|
||||||
|
frame.render_stateful_widget(list, size, &mut song_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// draws playing queue
|
||||||
|
fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
|
||||||
|
let mut queue_state = ListState::default();
|
||||||
|
let list = List::new(app.play_deque.clone())
|
||||||
|
.block(Block::default().title("Play Queue").borders(Borders::ALL))
|
||||||
|
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
||||||
|
.highlight_symbol(">>")
|
||||||
|
.repeat_highlight_symbol(true);
|
||||||
|
|
||||||
|
app.update_queue();
|
||||||
|
frame.render_stateful_widget(list, size, &mut queue_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// draws all playlists
|
||||||
|
fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) {
|
||||||
let mut state = ListState::default();
|
let mut state = ListState::default();
|
||||||
let size = Rect::new(0, 25, 100, frame.size().height - 25 - 3);
|
|
||||||
let list = List::new(app.pl_list.list.clone())
|
let list = List::new(app.pl_list.list.clone())
|
||||||
.block(Block::default().title("Playlists").borders(Borders::ALL))
|
.block(Block::default().title("Playlists").borders(Borders::ALL))
|
||||||
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
||||||
|
|
|
||||||
Reference in New Issue