remove redundant functions and add comments
This commit is contained in:
parent
c047f63cd1
commit
87bb8e9473
36
src/app.rs
36
src/app.rs
|
|
@ -15,17 +15,18 @@ pub struct App {
|
||||||
/// check if app is running
|
/// check if app is running
|
||||||
pub running: bool,
|
pub running: bool,
|
||||||
pub conn: Connection,
|
pub conn: Connection,
|
||||||
pub queue_list: ContentList<String>,
|
pub browser: FileBrowser, // Directory browser
|
||||||
pub pl_list: ContentList<String>,
|
pub queue_list: ContentList<String>, // Stores the current playing queue
|
||||||
pub selected_tab: SelectedTab,
|
pub pl_list: ContentList<String>, // Stores list of playlists
|
||||||
pub browser: FileBrowser,
|
pub selected_tab: SelectedTab, // Used to switch between tabs
|
||||||
|
|
||||||
// Search
|
// Search
|
||||||
pub inputmode: InputMode,
|
pub inputmode: InputMode, // Defines input mode, Normal or Search
|
||||||
pub search_input: String,
|
pub search_input: String, // Stores the userinput to be searched
|
||||||
pub cursor_position: usize,
|
pub cursor_position: usize, // Stores the cursor position
|
||||||
|
|
||||||
// add to playlists
|
// playlist variables
|
||||||
|
// used to show playlist popup
|
||||||
pub playlist_popup: bool,
|
pub playlist_popup: bool,
|
||||||
pub append_list: ContentList<String>,
|
pub append_list: ContentList<String>,
|
||||||
}
|
}
|
||||||
|
|
@ -90,12 +91,13 @@ impl App {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rescan the queue into queue_list
|
||||||
pub fn update_queue(&mut self) {
|
pub fn update_queue(&mut self) {
|
||||||
self.queue_list.list.clear();
|
self.queue_list.list.clear();
|
||||||
Self::get_queue(&mut self.conn, &mut self.queue_list.list);
|
Self::get_queue(&mut self.conn, &mut self.queue_list.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> {
|
fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> {
|
||||||
let list: Vec<String> = conn.playlists()?.iter().map(|p| p.clone().name).collect();
|
let list: Vec<String> = conn.playlists()?.iter().map(|p| p.clone().name).collect();
|
||||||
Ok(list)
|
Ok(list)
|
||||||
}
|
}
|
||||||
|
|
@ -110,11 +112,7 @@ impl App {
|
||||||
Ok(list)
|
Ok(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_playlist(&mut self) -> AppResult<()> {
|
/// Handles the <Space> event key
|
||||||
Self::get_playlist(&mut self.conn.conn)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> {
|
pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> {
|
||||||
match self.selected_tab {
|
match self.selected_tab {
|
||||||
SelectedTab::DirectoryBrowser => {
|
SelectedTab::DirectoryBrowser => {
|
||||||
|
|
@ -170,6 +168,7 @@ impl App {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Cycle through tabs
|
||||||
pub fn cycle_tabls(&mut self) {
|
pub fn cycle_tabls(&mut self) {
|
||||||
self.selected_tab = match self.selected_tab {
|
self.selected_tab = match self.selected_tab {
|
||||||
SelectedTab::Queue => SelectedTab::DirectoryBrowser,
|
SelectedTab::Queue => SelectedTab::DirectoryBrowser,
|
||||||
|
|
@ -178,6 +177,7 @@ impl App {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// handles the Enter event on the directory browser
|
||||||
pub fn handle_enter(&mut self) -> AppResult<()> {
|
pub fn handle_enter(&mut self) -> AppResult<()> {
|
||||||
let browser = &mut self.browser;
|
let browser = &mut self.browser;
|
||||||
let (t, path) = browser.filetree.get(browser.selected).unwrap();
|
let (t, path) = browser.filetree.get(browser.selected).unwrap();
|
||||||
|
|
@ -218,14 +218,6 @@ impl App {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_song(&mut self) -> AppResult<()> {
|
|
||||||
if let Some(filename) = self.conn.get_full_path(&self.search_input) {
|
|
||||||
let song = self.conn.get_song_with_only_filename(&filename);
|
|
||||||
self.conn.push(&song)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cursor movements
|
// Cursor movements
|
||||||
pub fn move_cursor_left(&mut self) {
|
pub fn move_cursor_left(&mut self) {
|
||||||
let cursor_moved_left = self.cursor_position.saturating_sub(1);
|
let cursor_moved_left = self.cursor_position.saturating_sub(1);
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use mpd::Song;
|
||||||
use crate::{app::AppResult, connection::Connection};
|
use crate::{app::AppResult, connection::Connection};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
/// struct for working with directory browser tab in rmptui
|
||||||
pub struct FileBrowser {
|
pub struct FileBrowser {
|
||||||
pub filetree: Vec<(String, String)>,
|
pub filetree: Vec<(String, String)>,
|
||||||
pub selected: usize,
|
pub selected: usize,
|
||||||
|
|
@ -61,9 +62,10 @@ impl FileBrowser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Add metadata
|
||||||
dir_vec.extend(file_vec);
|
dir_vec.extend(file_vec);
|
||||||
self.filetree = dir_vec;
|
self.filetree = dir_vec;
|
||||||
|
|
||||||
self.songs.clear();
|
self.songs.clear();
|
||||||
for (t, song) in self.filetree.iter() {
|
for (t, song) in self.filetree.iter() {
|
||||||
if t == "file" {
|
if t == "file" {
|
||||||
|
|
@ -121,6 +123,7 @@ impl FileBrowser {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// handles going back event
|
||||||
pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
|
pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
|
||||||
if self.prev_path != "." {
|
if self.prev_path != "." {
|
||||||
let r = self.path.rfind("/").unwrap();
|
let r = self.path.rfind("/").unwrap();
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ pub type Result<T> = core::result::Result<T, Error>;
|
||||||
pub type Error = Box<dyn std::error::Error>;
|
pub type Error = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
/// struct storing the mpd Client related stuff
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
pub conn: Client,
|
pub conn: Client,
|
||||||
pub songs_filenames: Vec<String>,
|
pub songs_filenames: Vec<String>,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Error {
|
|
||||||
DmenuNotInstalled,
|
|
||||||
FzfNotInstalled,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
use crate::browser::FileExtension;
|
use crate::browser::FileExtension;
|
||||||
use std::{path::Path, time::Duration};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{App, AppResult, SelectedTab},
|
app::{App, AppResult, SelectedTab},
|
||||||
ui::InputMode,
|
ui::InputMode,
|
||||||
};
|
};
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
use rust_fuzzy_search::{self, fuzzy_search_sorted};
|
use rust_fuzzy_search::{self, fuzzy_search_sorted};
|
||||||
|
use std::{path::Path, time::Duration};
|
||||||
|
|
||||||
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||||
if app.inputmode == InputMode::Editing {
|
if app.inputmode == InputMode::Editing {
|
||||||
|
|
@ -65,6 +64,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keybind for searching
|
||||||
|
//
|
||||||
|
// Keybinds for when the search prompt is visible
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
app.inputmode = InputMode::Normal;
|
app.inputmode = InputMode::Normal;
|
||||||
|
|
@ -108,6 +110,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Playlist popup keybinds
|
||||||
|
//
|
||||||
|
// Keybind for when the "append to playlist" popup is visible
|
||||||
} else if app.playlist_popup {
|
} else if app.playlist_popup {
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
KeyCode::Char('q') | KeyCode::Esc => {
|
KeyCode::Char('q') | KeyCode::Esc => {
|
||||||
|
|
@ -183,6 +189,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Global keymaps
|
||||||
|
//
|
||||||
|
// Keymaps related to all the tabs
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
// Quit
|
// Quit
|
||||||
KeyCode::Char('q') => app.quit(),
|
KeyCode::Char('q') => app.quit(),
|
||||||
|
|
@ -343,7 +352,8 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||||
|
|
||||||
// Update MPD database
|
// Update MPD database
|
||||||
KeyCode::Char('U') => {
|
KeyCode::Char('U') => {
|
||||||
app.conn.conn.update()?;
|
app.conn.conn.rescan()?;
|
||||||
|
app.browser.update_directory(&mut app.conn)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for songs
|
// Search for songs
|
||||||
|
|
|
||||||
Reference in New Issue