Krolyxon
Commit 87bb8e9

rmptui87bb8e9

remove redundant functions and add comments

Author
krolxon
Date
2024-04-29 23:20 +0530
Parent
c047f63
Commit
87bb8e9473194b47c9f52e49c7fe2b632bd36aef

+34 −34

5 files changed

  1. M src/app.rs +14 −22
  2. M src/browser.rs +4 −1
  3. M src/connection.rs +3 −2
  4. D src/error.rs +0 −6
  5. M src/handler.rs +13 −3
M src/app.rs View
@@ -15,17 +15,18 @@ pub struct App {
1515 /// check if app is running
1616 pub running: bool,
1717 pub conn: Connection,
18− pub queue_list: ContentList<String>,
19− pub pl_list: ContentList<String>,
20− pub selected_tab: SelectedTab,
21− pub browser: FileBrowser,
18+ pub browser: FileBrowser, // Directory browser
19+ pub queue_list: ContentList<String>, // Stores the current playing queue
20+ pub pl_list: ContentList<String>, // Stores list of playlists
21+ pub selected_tab: SelectedTab, // Used to switch between tabs
2222
2323 // Search
24− pub inputmode: InputMode,
25− pub search_input: String,
26− pub cursor_position: usize,
24+ pub inputmode: InputMode, // Defines input mode, Normal or Search
25+ pub search_input: String, // Stores the userinput to be searched
26+ pub cursor_position: usize, // Stores the cursor position
2727
28− // add to playlists
28+ // playlist variables
29+ // used to show playlist popup
2930 pub playlist_popup: bool,
3031 pub append_list: ContentList<String>,
3132 }
@@ -90,12 +91,13 @@ impl App {
9091 });
9192 }
9293
94+ // Rescan the queue into queue_list
9395 pub fn update_queue(&mut self) {
9496 self.queue_list.list.clear();
9597 Self::get_queue(&mut self.conn, &mut self.queue_list.list);
9698 }
9799
98− pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> {
100+ fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> {
99101 let list: Vec<String> = conn.playlists()?.iter().map(|p| p.clone().name).collect();
100102 Ok(list)
101103 }
@@ -110,11 +112,7 @@ impl App {
110112 Ok(list)
111113 }
112114
113− pub fn update_playlist(&mut self) -> AppResult<()> {
114− Self::get_playlist(&mut self.conn.conn)?;
115− Ok(())
116− }
117−
115+ /// Handles the <Space> event key
118116 pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> {
119117 match self.selected_tab {
120118 SelectedTab::DirectoryBrowser => {
@@ -170,6 +168,7 @@ impl App {
170168 Ok(())
171169 }
172170
171+ /// Cycle through tabs
173172 pub fn cycle_tabls(&mut self) {
174173 self.selected_tab = match self.selected_tab {
175174 SelectedTab::Queue => SelectedTab::DirectoryBrowser,
@@ -178,6 +177,7 @@ impl App {
178177 };
179178 }
180179
180+ /// handles the Enter event on the directory browser
181181 pub fn handle_enter(&mut self) -> AppResult<()> {
182182 let browser = &mut self.browser;
183183 let (t, path) = browser.filetree.get(browser.selected).unwrap();
@@ -218,14 +218,6 @@ impl App {
218218 Ok(())
219219 }
220220
221− pub fn search_song(&mut self) -> AppResult<()> {
222− if let Some(filename) = self.conn.get_full_path(&self.search_input) {
223− let song = self.conn.get_song_with_only_filename(&filename);
224− self.conn.push(&song)?;
225− }
226− Ok(())
227− }
228−
229221 // Cursor movements
230222 pub fn move_cursor_left(&mut self) {
231223 let cursor_moved_left = self.cursor_position.saturating_sub(1);
M src/browser.rs View
@@ -6,6 +6,7 @@ use mpd::Song;
66 use crate::{app::AppResult, connection::Connection};
77
88 #[derive(Debug)]
9+/// struct for working with directory browser tab in rmptui
910 pub struct FileBrowser {
1011 pub filetree: Vec<(String, String)>,
1112 pub selected: usize,
@@ -61,9 +62,10 @@ impl FileBrowser {
6162 }
6263 }
6364
65+
66+ // Add metadata
6467 dir_vec.extend(file_vec);
6568 self.filetree = dir_vec;
66−
6769 self.songs.clear();
6870 for (t, song) in self.filetree.iter() {
6971 if t == "file" {
@@ -121,6 +123,7 @@ impl FileBrowser {
121123 // }
122124 }
123125
126+ /// handles going back event
124127 pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
125128 if self.prev_path != "." {
126129 let r = self.path.rfind("/").unwrap();
M src/connection.rs View
@@ -8,6 +8,7 @@ pub type Result<T> = core::result::Result<T, Error>;
88 pub type Error = Box<dyn std::error::Error>;
99
1010 #[derive(Debug)]
11+/// struct storing the mpd Client related stuff
1112 pub struct Connection {
1213 pub conn: Client,
1314 pub songs_filenames: Vec<String>,
@@ -26,7 +27,7 @@ impl Connection {
2627 let mut conn = Client::connect(addrs).unwrap();
2728
2829 let empty_song = Song {
29− file: "No Song playing or in Queue".to_string(),
30+ file: "No Song playing or in Queue".to_string(),
3031 ..Default::default()
3132 };
3233
@@ -86,7 +87,7 @@ impl Connection {
8687 /// Update status
8788 pub fn update_status(&mut self) {
8889 let status = self.conn.status().unwrap();
89− let empty_song = self.get_song_with_only_filename("No Song playing or in Queue");
90+ let empty_song = self.get_song_with_only_filename("No Song playing or in Queue");
9091 let current_song = self
9192 .conn
9293 .currentsong()
D src/error.rs View
@@ -1,6 +0,0 @@
1−#[derive(Debug)]
2−pub enum Error {
3− DmenuNotInstalled,
4− FzfNotInstalled,
5−}
6−
M src/handler.rs View
@@ -1,12 +1,11 @@
11 use crate::browser::FileExtension;
2−use std::{path::Path, time::Duration};
3−
42 use crate::{
53 app::{App, AppResult, SelectedTab},
64 ui::InputMode,
75 };
86 use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
97 use rust_fuzzy_search::{self, fuzzy_search_sorted};
8+use std::{path::Path, time::Duration};
109
1110 pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
1211 if app.inputmode == InputMode::Editing {
@@ -65,6 +64,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
6564 }
6665 }
6766
67+ // Keybind for searching
68+ //
69+ // Keybinds for when the search prompt is visible
6870 match key_event.code {
6971 KeyCode::Esc => {
7072 app.inputmode = InputMode::Normal;
@@ -108,6 +110,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
108110
109111 _ => {}
110112 }
113+
114+ // Playlist popup keybinds
115+ //
116+ // Keybind for when the "append to playlist" popup is visible
111117 } else if app.playlist_popup {
112118 match key_event.code {
113119 KeyCode::Char('q') | KeyCode::Esc => {
@@ -183,6 +189,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
183189 _ => {}
184190 }
185191 } else {
192+ // Global keymaps
193+ //
194+ // Keymaps related to all the tabs
186195 match key_event.code {
187196 // Quit
188197 KeyCode::Char('q') => app.quit(),
@@ -343,7 +352,8 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
343352
344353 // Update MPD database
345354 KeyCode::Char('U') => {
346− app.conn.conn.update()?;
355+ app.conn.conn.rescan()?;
356+ app.browser.update_directory(&mut app.conn)?;
347357 }
348358
349359 // Search for songs