hmm stuff

This commit is contained in:
krolxon 2024-04-24 12:36:33 +05:30
parent b93eae4482
commit bf11d5967f
4 changed files with 62 additions and 47 deletions

View File

@ -12,7 +12,7 @@ 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 play_deque: VecDeque<String>, pub play_deque: ContentList<String>,
pub song_list: ContentList<String>, pub song_list: ContentList<String>,
pub queue_list: ContentList<String>, pub queue_list: ContentList<String>,
pub pl_list: ContentList<String>, pub pl_list: ContentList<String>,
@ -21,10 +21,11 @@ pub struct App {
impl App { impl App {
pub fn builder(addrs: &str) -> AppResult<Self> { pub fn builder(addrs: &str) -> AppResult<Self> {
let mut conn = Connection::new(addrs).unwrap(); let mut conn = Connection::new(addrs).unwrap();
let mut vec: VecDeque<String> = VecDeque::new(); let mut queue = ContentList::new();
let mut pl_list = ContentList::new(); let mut pl_list = ContentList::new();
pl_list.list = Self::get_playlist(&mut conn.conn)?; pl_list.list = Self::get_playlist(&mut conn.conn)?;
Self::get_queue(&mut conn, &mut vec);
Self::get_queue(&mut conn, &mut queue.list);
let mut song_list = ContentList::new(); let mut song_list = ContentList::new();
song_list.list = conn.songs_filenames.clone(); song_list.list = conn.songs_filenames.clone();
@ -32,7 +33,7 @@ impl App {
Ok(Self { Ok(Self {
running: true, running: true,
conn, conn,
play_deque: vec, play_deque: ContentList::new(),
song_list, song_list,
queue_list: ContentList::new(), queue_list: ContentList::new(),
pl_list, pl_list,
@ -45,23 +46,23 @@ impl App {
self.running = false; self.running = false;
} }
pub fn get_queue(conn: &mut Connection, vec: &mut VecDeque<String>) { pub fn get_queue(conn: &mut Connection, vec: &mut Vec<String>) {
conn.conn.queue().unwrap().into_iter().for_each(|x| { conn.conn.queue().unwrap().into_iter().for_each(|x| {
if let Some(title) = x.title { if let Some(title) = x.title {
if let Some(artist) = x.artist { if let Some(artist) = x.artist {
vec.push_back(format!("{} - {}", artist, title)); vec.push(format!("{} - {}", artist, title));
} else { } else {
vec.push_back(title) vec.push(title)
} }
} else { } else {
vec.push_back(x.file) vec.push(x.file)
} }
}); });
} }
pub fn update_queue(&mut self) { pub fn update_queue(&mut self) {
self.play_deque.clear(); self.queue_list.list.clear();
Self::get_queue(&mut self.conn, &mut self.play_deque); Self::get_queue(&mut self.conn, &mut self.queue_list.list);
} }
pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> { pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> {

View File

@ -132,7 +132,7 @@ impl Connection {
/// Gives title of current playing song /// Gives title of current playing song
pub fn now_playing(&mut self) -> Result<Option<String>> { pub fn now_playing(&mut self) -> Result<Option<String>> {
let song = self.conn.currentsong()?.unwrap(); let song = self.conn.currentsong()?.unwrap_or_default();
if let Some(s) = song.title { if let Some(s) = song.title {
if let Some(a) = song.artist { if let Some(a) = song.artist {
return Ok(Some(format!("{} - {}", s, a))); return Ok(Some(format!("{} - {}", s, a)));

View File

@ -19,7 +19,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
} }
KeyCode::Enter | KeyCode::Char('l') => { KeyCode::Enter | KeyCode::Char('l') => {
let song = app.conn.get_song_with_only_filename(app.conn.songs_filenames.get(app.song_list.index).unwrap()); let song = app.conn.get_song_with_only_filename(
app.conn.songs_filenames.get(app.song_list.index).unwrap(),
);
app.conn.push(&song)?; app.conn.push(&song)?;
// app.update_queue(); // app.update_queue();
} }
@ -35,7 +37,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.conn.pause(); app.conn.pause();
} }
// Clearn Queue // Clearn Queue
KeyCode::Char('x') => { KeyCode::Char('x') => {
app.conn.conn.clear()?; app.conn.conn.clear()?;
@ -46,31 +47,39 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.conn.play_dmenu()?; app.conn.play_dmenu()?;
} }
KeyCode::Down=> { KeyCode::Down => {
if key_event.modifiers == KeyModifiers::SHIFT {
app.queue_list.next();
} else {
app.pl_list.next(); app.pl_list.next();
} }
}
KeyCode::Up=> { KeyCode::Up => {
if key_event.modifiers == KeyModifiers::SHIFT {
app.queue_list.prev();
} else {
app.pl_list.prev(); app.pl_list.prev();
} }
KeyCode::Right => {
app.conn.push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
} }
KeyCode::Char('f')=> { KeyCode::Right => {
app.conn
.push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
}
KeyCode::Char('f') => {
// let place = app.conn.conn.status().unwrap().duration; // let place = app.conn.conn.status().unwrap().duration;
let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
let pos: i64 = (pos.as_secs() + 2).try_into().unwrap(); let pos: i64 = (pos.as_secs() + 2).try_into().unwrap();
app.conn.conn.seek(2, pos )?; app.conn.conn.seek(2, pos)?;
} }
KeyCode::Char('b')=> { KeyCode::Char('b') => {
// let place = app.conn.conn.status().unwrap().duration; // let place = app.conn.conn.status().unwrap().duration;
let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
let pos: i64 = (pos.as_secs() - 2).try_into().unwrap(); let pos: i64 = (pos.as_secs() - 2).try_into().unwrap();
app.conn.conn.seek(2, pos )?; app.conn.conn.seek(2, pos)?;
} }
_ => {} _ => {}
} }

View File

@ -1,5 +1,5 @@
use crate::app::{App, AppResult}; use crate::app::{App, AppResult};
use ratatui::{prelude::*, widgets::*}; use ratatui::{prelude::*, widgets::{block::Title, *}};
/// Renders the user interface widgets /// Renders the user interface widgets
pub fn render(app: &mut App, frame: &mut Frame) { pub fn render(app: &mut App, frame: &mut Frame) {
@ -29,32 +29,33 @@ pub fn render(app: &mut App, frame: &mut Frame) {
draw_playlists(frame, app, inner_layout[1]); draw_playlists(frame, app, inner_layout[1]);
// Status // Status
// let song = app let song = app
// .conn .conn
// .now_playing().unwrap() .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_or_default();
//
// let mut lines = vec![]; let mut lines = vec![];
// 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( Span::styled(
// format!("[{}/{}]", elapsed.as_secs(), total.as_secs()), format!("[{}/{}]", elapsed.as_secs(), total.as_secs()),
// Style::default().fg(Color::Yellow), 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".bold().green()).borders(Borders::ALL));
// frame.render_widget(status, main_layout[1]); frame.render_widget(status, main_layout[1]);
} }
/// draws list of songs /// draws list of songs
fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) { fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) {
let mut song_state = ListState::default(); let mut song_state = ListState::default();
let list = List::new(app.conn.songs_filenames.clone()) let list = List::new(app.conn.songs_filenames.clone())
.block(Block::default().title("Song List").borders(Borders::ALL)) .block(Block::default().title("Song List".green().bold()).borders(Borders::ALL))
.highlight_style(Style::new().add_modifier(Modifier::REVERSED)) .highlight_style(Style::new().add_modifier(Modifier::REVERSED))
.highlight_symbol(">>") .highlight_symbol(">>")
.repeat_highlight_symbol(true); .repeat_highlight_symbol(true);
@ -66,21 +67,25 @@ fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) {
/// draws playing queue /// draws playing queue
fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
let mut queue_state = ListState::default(); let mut queue_state = ListState::default();
let list = List::new(app.play_deque.clone()) let title = Block::default().title(Title::from("Play Queue".green().bold())).title("Shift + ▲ ▼ to scroll, Shift + Enter to play".yellow());
.block(Block::default().title("Play Queue").borders(Borders::ALL)) let list = List::new(app.queue_list.list.clone())
.block(title.borders(Borders::ALL))
.highlight_style(Style::new().add_modifier(Modifier::REVERSED)) .highlight_style(Style::new().add_modifier(Modifier::REVERSED))
.highlight_symbol(">>") .highlight_symbol(">>")
.repeat_highlight_symbol(true); .repeat_highlight_symbol(true);
app.update_queue(); app.update_queue();
queue_state.select(Some(app.queue_list.index));
frame.render_stateful_widget(list, size, &mut queue_state); frame.render_stateful_widget(list, size, &mut queue_state);
} }
/// draws all playlists /// draws all playlists
fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) { fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) {
let mut state = ListState::default(); let mut state = ListState::default();
let title = Block::default().title(Title::from("Playlist".green().bold())).title("▲ ▼ to scroll, Use ► to add playlist to queue".yellow());
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(title.borders(Borders::ALL))
.highlight_style(Style::new().add_modifier(Modifier::REVERSED)) .highlight_style(Style::new().add_modifier(Modifier::REVERSED))
.highlight_symbol(">>") .highlight_symbol(">>")
.repeat_highlight_symbol(true); .repeat_highlight_symbol(true);