Krolyxon
Commit b93eae4

rmptuib93eae4

add documentation

Author
krolxon
Date
2024-04-23 23:33 +0530
Parent
05c4b17
Commit
b93eae44822168c0dab821a12fa3ecd62ee4a875

+83 −35

4 files changed

  1. M src/app.rs +1 −1
  2. M src/connection.rs +20 −5
  3. M src/list.rs +11 −3
  4. M src/ui.rs +51 −26
M src/app.rs View
@@ -1,4 +1,4 @@
1−use crate::connection::{self, Connection};
1+use crate::connection::Connection;
22 use crate::list::ContentList;
33 use mpd::Client;
44 use std::collections::VecDeque;
M src/connection.rs View
@@ -14,6 +14,7 @@ pub struct Connection {
1414 }
1515
1616 impl Connection {
17+ /// Create a new connection
1718 pub fn new(addrs: &str) -> Result<Self> {
1819 let mut conn = Client::connect(addrs).unwrap();
1920 let songs_filenames: Vec<String> = conn
@@ -30,6 +31,7 @@ impl Connection {
3031 })
3132 }
3233
34+ /// Fzf prompt for selecting song
3335 pub fn play_fzf(&mut self) -> Result<()> {
3436 is_installed("fzf").map_err(|ex| ex)?;
3537
@@ -42,6 +44,7 @@ impl Connection {
4244 Ok(())
4345 }
4446
47+ /// Dmenu prompt for selecting songs
4548 pub fn play_dmenu(&mut self) -> Result<()> {
4649 is_installed("dmenu").map_err(|ex| ex)?;
4750 let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
@@ -60,6 +63,7 @@ impl Connection {
6063 // }
6164 // }
6265
66+ /// push the given song to queue
6367 pub fn push(&mut self, song: &Song) -> Result<()> {
6468 if self.conn.queue().unwrap().is_empty() {
6569 self.conn.push(song).unwrap();
@@ -75,6 +79,7 @@ impl Connection {
7579 Ok(())
7680 }
7781
82+ /// Push all songs of a playlist into queue
7883 pub fn push_playlist(&mut self, playlist: &str) -> Result<()> {
7984 let songs: Vec<Song> = self.conn.playlist(playlist)?;
8085
@@ -88,6 +93,7 @@ impl Connection {
8893 Ok(())
8994 }
9095
96+ /// Given a filename, get instance of Song with only filename
9197 pub fn get_song_with_only_filename(&self, filename: &str) -> Song {
9298 Song {
9399 file: filename.to_string(),
@@ -102,9 +108,12 @@ impl Connection {
102108 }
103109 }
104110
111+ /// get current playing song
105112 pub fn get_current_song(&mut self) -> Option<String> {
106113 self.conn.currentsong().unwrap().unwrap_or_default().title
107114 }
115+
116+ /// Print status to stdout
108117 pub fn status(&mut self) {
109118 let current_song = self.conn.currentsong();
110119 let status = self.conn.status().unwrap();
@@ -121,29 +130,33 @@ impl Connection {
121130 );
122131 }
123132
124− pub fn now_playing(&mut self) -> Option<String> {
125− let song = self.conn.currentsong().unwrap().unwrap_or_default();
133+ /// Gives title of current playing song
134+ pub fn now_playing(&mut self) -> Result<Option<String>> {
135+ let song = self.conn.currentsong()?.unwrap();
126136 if let Some(s) = song.title {
127137 if let Some(a) = song.artist {
128− Some(format!("{} - {}", s, a))
138+ return Ok(Some(format!("{} - {}", s, a)));
129139 } else {
130− Some(s)
140+ return Ok(Some(s));
131141 }
132142 } else {
133− None
143+ return Ok(Some(song.file));
134144 }
135145 }
136146
137147 // Playback controls
148+ /// Pause playback
138149 pub fn pause(&mut self) {
139150 self.conn.pause(true).unwrap();
140151 }
141152
153+ /// Toggles playback
142154 pub fn toggle_pause(&mut self) {
143155 self.conn.toggle_pause().unwrap();
144156 }
145157
146158 // Volume controls
159+ /// Sets the volume
147160 pub fn set_volume(&mut self, u: String) {
148161 let cur = self.conn.status().unwrap().volume;
149162 let sym = u.get(0..1).unwrap();
@@ -156,6 +169,7 @@ impl Connection {
156169 }
157170 }
158171
172+/// Gets the index of the string from the Vector
159173 fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
160174 let mut choice: usize = 0;
161175 if let Some(index) = ss.iter().position(|s| s == selection) {
@@ -165,6 +179,7 @@ fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
165179 choice
166180 }
167181
182+/// Checks if given program is installed in your system
168183 fn is_installed(ss: &str) -> Result<()> {
169184 let output = Command::new("which")
170185 .arg(ss)
M src/list.rs View
@@ -1,20 +1,28 @@
11 #[derive(Debug)]
22 pub struct ContentList<T> {
33 pub list: Vec<T>,
4− pub index: usize
4+ pub index: usize,
55 }
66
77 impl<T> ContentList<T> {
88 pub fn new() -> Self {
99 ContentList {
1010 list: Vec::new(),
11− index: 0
11+ index: 0,
1212 }
1313 }
1414
1515 // Go to next item in list
1616 pub fn next(&mut self) {
17− self.index += 1;
17+ // if self.index < self.list.len() - 1 {
18+ // self.index += 1;
19+ // }
20+
21+ if self.index == self.list.len() - 1 {
22+ self.index = 0;
23+ } else {
24+ self.index += 1;
25+ }
1826 }
1927
2028 /// Go to previous item in list
M src/ui.rs View
@@ -1,4 +1,4 @@
1−use crate::app::App;
1+use crate::app::{App, AppResult};
22 use ratatui::{prelude::*, widgets::*};
33
44 /// Renders the user interface widgets
@@ -8,9 +8,51 @@ pub fn render(app: &mut App, frame: &mut Frame) {
88 // - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
99 // - https://github.com/ratatui-org/ratatui/tree/master/examples
1010
11− // List of songs
11+ // Layout
12+ let main_layout = Layout::default()
13+ .direction(Direction::Vertical)
14+ .constraints(vec![Constraint::Percentage(93), Constraint::Percentage(7)])
15+ .split(frame.size());
16+
17+ let outer_layout = Layout::default()
18+ .direction(Direction::Horizontal)
19+ .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
20+ .split(main_layout[0]);
21+
22+ let inner_layout = Layout::default()
23+ .direction(Direction::Vertical)
24+ .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
25+ .split(outer_layout[1]);
26+
27+ draw_song_list(frame, app, outer_layout[0]);
28+ draw_queue(frame, app, inner_layout[0]);
29+ draw_playlists(frame, app, inner_layout[1]);
30+
31+ // Status
32+ // let song = app
33+ // .conn
34+ // .now_playing().unwrap()
35+ // .unwrap_or_else(|| "No Title Found".to_string());
36+ //
37+ // let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap();
38+ //
39+ // let mut lines = vec![];
40+ // lines.push(Line::from(vec![
41+ // Span::styled("Current: ", Style::default().fg(Color::Red)),
42+ // Span::styled(song, Style::default().fg(Color::Yellow)),
43+ // Span::styled(
44+ // format!("[{}/{}]", elapsed.as_secs(), total.as_secs()),
45+ // Style::default().fg(Color::Yellow),
46+ // ),
47+ // ]));
48+ // let status = Paragraph::new(Text::from(lines))
49+ // .block(Block::default().title("Status").borders(Borders::ALL));
50+ // frame.render_widget(status, main_layout[1]);
51+}
52+
53+/// draws list of songs
54+fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) {
1255 let mut song_state = ListState::default();
13− let size = Rect::new(100, 0, frame.size().width, frame.size().height - 3);
1456 let list = List::new(app.conn.songs_filenames.clone())
1557 .block(Block::default().title("Song List").borders(Borders::ALL))
1658 .highlight_style(Style::new().add_modifier(Modifier::REVERSED))
@@ -19,10 +61,11 @@ pub fn render(app: &mut App, frame: &mut Frame) {
1961
2062 song_state.select(Some(app.song_list.index));
2163 frame.render_stateful_widget(list, size, &mut song_state);
64+}
2265
23− // Play Queue
66+/// draws playing queue
67+fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
2468 let mut queue_state = ListState::default();
25− let size = Rect::new(0, 0, 100, frame.size().height - 25);
2669 let list = List::new(app.play_deque.clone())
2770 .block(Block::default().title("Play Queue").borders(Borders::ALL))
2871 .highlight_style(Style::new().add_modifier(Modifier::REVERSED))
@@ -31,29 +74,11 @@ pub fn render(app: &mut App, frame: &mut Frame) {
3174
3275 app.update_queue();
3376 frame.render_stateful_widget(list, size, &mut queue_state);
77+}
3478
35− // Status
36− // let size = Rect::new(0, frame.size().height - 3, frame.size().width, 3);
37− // let song = app
38− // .conn
39− // .now_playing()
40− // .unwrap_or_else(|| "No Title Found".to_string());
41− //
42− // let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap();
43− //
44− // let mut lines = vec![];
45− // lines.push(Line::from(vec![
46− // Span::styled("Current: ", Style::default().fg(Color::Red)),
47− // Span::styled(song, Style::default().fg(Color::Yellow)),
48− // Span::styled(format!("[{}/{}]", elapsed.as_secs(), total.as_secs()), Style::default().fg(Color::Yellow)),
49− // ]));
50− // let status = Paragraph::new(Text::from(lines))
51− // .block(Block::default().title("Status").borders(Borders::ALL));
52− // frame.render_widget(status, size);
53−
54− // Playlists
79+/// draws all playlists
80+fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) {
5581 let mut state = ListState::default();
56− let size = Rect::new(0, 25, 100, frame.size().height - 25 - 3);
5782 let list = List::new(app.pl_list.list.clone())
5883 .block(Block::default().title("Playlists").borders(Borders::ALL))
5984 .highlight_style(Style::new().add_modifier(Modifier::REVERSED))