add documentation

This commit is contained in:
krolxon 2024-04-23 23:33:17 +05:30
parent 05c4b17bc0
commit b93eae4482
4 changed files with 84 additions and 36 deletions

View File

@ -1,4 +1,4 @@
use crate::connection::{self, Connection};
use crate::connection::Connection;
use crate::list::ContentList;
use mpd::Client;
use std::collections::VecDeque;

View File

@ -14,6 +14,7 @@ pub struct Connection {
}
impl Connection {
/// Create a new connection
pub fn new(addrs: &str) -> Result<Self> {
let mut conn = Client::connect(addrs).unwrap();
let songs_filenames: Vec<String> = conn
@ -30,6 +31,7 @@ impl Connection {
})
}
/// Fzf prompt for selecting song
pub fn play_fzf(&mut self) -> Result<()> {
is_installed("fzf").map_err(|ex| ex)?;
@ -42,6 +44,7 @@ impl Connection {
Ok(())
}
/// Dmenu prompt for selecting songs
pub fn play_dmenu(&mut self) -> Result<()> {
is_installed("dmenu").map_err(|ex| ex)?;
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<()> {
if self.conn.queue().unwrap().is_empty() {
self.conn.push(song).unwrap();
@ -75,6 +79,7 @@ impl Connection {
Ok(())
}
/// Push all songs of a playlist into queue
pub fn push_playlist(&mut self, playlist: &str) -> Result<()> {
let songs: Vec<Song> = self.conn.playlist(playlist)?;
@ -88,6 +93,7 @@ impl Connection {
Ok(())
}
/// Given a filename, get instance of Song with only filename
pub fn get_song_with_only_filename(&self, filename: &str) -> Song {
Song {
file: filename.to_string(),
@ -102,9 +108,12 @@ impl Connection {
}
}
/// get current playing song
pub fn get_current_song(&mut self) -> Option<String> {
self.conn.currentsong().unwrap().unwrap_or_default().title
}
/// Print status to stdout
pub fn status(&mut self) {
let current_song = self.conn.currentsong();
let status = self.conn.status().unwrap();
@ -121,29 +130,33 @@ impl Connection {
);
}
pub fn now_playing(&mut self) -> Option<String> {
let song = self.conn.currentsong().unwrap().unwrap_or_default();
/// Gives title of current playing song
pub fn now_playing(&mut self) -> Result<Option<String>> {
let song = self.conn.currentsong()?.unwrap();
if let Some(s) = song.title {
if let Some(a) = song.artist {
Some(format!("{} - {}", s, a))
return Ok(Some(format!("{} - {}", s, a)));
} else {
Some(s)
return Ok(Some(s));
}
} else {
None
return Ok(Some(song.file));
}
}
// Playback controls
/// Pause playback
pub fn pause(&mut self) {
self.conn.pause(true).unwrap();
}
/// Toggles playback
pub fn toggle_pause(&mut self) {
self.conn.toggle_pause().unwrap();
}
// Volume controls
/// Sets the volume
pub fn set_volume(&mut self, u: String) {
let cur = self.conn.status().unwrap().volume;
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 {
let mut choice: usize = 0;
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
}
/// Checks if given program is installed in your system
fn is_installed(ss: &str) -> Result<()> {
let output = Command::new("which")
.arg(ss)

View File

@ -1,20 +1,28 @@
#[derive(Debug)]
pub struct ContentList<T> {
pub list: Vec<T>,
pub index: usize
pub index: usize,
}
impl<T> ContentList<T> {
pub fn new() -> Self {
ContentList {
list: Vec::new(),
index: 0
index: 0,
}
}
// Go to next item in list
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

View File

@ -1,4 +1,4 @@
use crate::app::App;
use crate::app::{App, AppResult};
use ratatui::{prelude::*, 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://github.com/ratatui-org/ratatui/tree/master/examples
// List of songs
let mut song_state = ListState::default();
let size = Rect::new(100, 0, frame.size().width, frame.size().height - 3);
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);
// Layout
let main_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![Constraint::Percentage(93), Constraint::Percentage(7)])
.split(frame.size());
song_state.select(Some(app.song_list.index));
frame.render_stateful_widget(list, size, &mut song_state);
let outer_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
.split(main_layout[0]);
// Play Queue
let mut queue_state = ListState::default();
let size = Rect::new(0, 0, 100, frame.size().height - 25);
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);
let inner_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
.split(outer_layout[1]);
app.update_queue();
frame.render_stateful_widget(list, size, &mut queue_state);
draw_song_list(frame, app, outer_layout[0]);
draw_queue(frame, app, inner_layout[0]);
draw_playlists(frame, app, inner_layout[1]);
// Status
// let size = Rect::new(0, frame.size().height - 3, frame.size().width, 3);
// let song = app
// .conn
// .now_playing()
// .now_playing().unwrap()
// .unwrap_or_else(|| "No Title Found".to_string());
//
// 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![
// Span::styled("Current: ", Style::default().fg(Color::Red)),
// 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))
// .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 size = Rect::new(0, 25, 100, frame.size().height - 25 - 3);
let list = List::new(app.pl_list.list.clone())
.block(Block::default().title("Playlists").borders(Borders::ALL))
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))