handle seek forwards/backwords

This commit is contained in:
krolxon 2024-04-24 13:56:53 +05:30
parent 3f6b2bfefd
commit 044101b290
3 changed files with 63 additions and 42 deletions

View File

@ -10,7 +10,7 @@ pub type Error = Box<dyn std::error::Error>;
pub struct Connection {
pub conn: Client,
pub songs_filenames: Vec<String>,
// pub state: String,
pub state: String,
}
impl Connection {
@ -27,7 +27,7 @@ impl Connection {
Ok(Self {
conn,
songs_filenames,
// state: "Stopped".to_string(),
state: "Stopped".to_string(),
})
}
@ -55,13 +55,14 @@ impl Connection {
Ok(())
}
// pub fn update_state(&mut self) {
// match self.conn.status().unwrap().state {
// State::Stop => self.state = "Stopped".to_string(),
// State::Play => self.state = "Playing".to_string(),
// State::Pause => self.state = "Paused".to_string(),
// }
// }
pub fn update_state(&mut self) -> String {
match self.conn.status().unwrap().state {
State::Stop => self.state = "Stopped".to_string(),
State::Play => self.state = "Playing".to_string(),
State::Pause => self.state = "Paused".to_string(),
}
self.state.clone()
}
/// push the given song to queue
pub fn push(&mut self, song: &Song) -> Result<()> {
@ -136,7 +137,7 @@ impl Connection {
let song = self.conn.currentsong()?.unwrap_or_default();
if let Some(s) = song.title {
if let Some(a) = song.artist {
return Ok(Some(format!("{} - {}", s, a)));
return Ok(Some(format!("\"{}\" By {}", a, s)));
} else {
return Ok(Some(s));
}

View File

@ -1,3 +1,5 @@
use std::time::Duration;
use crate::app::{App, AppResult};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
@ -69,17 +71,17 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
}
KeyCode::Char('f') => {
// let place = app.conn.conn.status().unwrap().duration;
let place = app.conn.conn.status().unwrap().song.unwrap().pos;
let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
let pos: i64 = (pos.as_secs() + 2).try_into().unwrap();
app.conn.conn.seek(2, pos)?;
let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
app.conn.conn.seek(place, pos)?;
}
KeyCode::Char('b') => {
// let place = app.conn.conn.status().unwrap().duration;
let place = app.conn.conn.status().unwrap().song.unwrap().pos;
let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
let pos: i64 = (pos.as_secs() - 2).try_into().unwrap();
app.conn.conn.seek(2, pos)?;
let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
app.conn.conn.seek(place, pos)?;
}
_ => {}
}

View File

@ -1,5 +1,8 @@
use crate::app::{App, AppResult};
use ratatui::{prelude::*, widgets::{block::Title, *}};
use ratatui::{
prelude::*,
widgets::{block::Title, *},
};
/// Renders the user interface widgets
pub fn render(app: &mut App, frame: &mut Frame) {
@ -27,35 +30,18 @@ pub fn render(app: &mut App, frame: &mut Frame) {
draw_song_list(frame, app, outer_layout[0]);
draw_queue(frame, app, inner_layout[0]);
draw_playlists(frame, app, inner_layout[1]);
// Status
let song = app
.conn
.now_playing().unwrap()
.unwrap_or_else(|| "No Title Found".to_string());
let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap_or_default();
let mut lines = vec![];
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),
),
]));
let status = Paragraph::new(Text::from(lines))
.block(Block::default().title("Status".bold().green()).borders(Borders::ALL));
frame.render_widget(status, main_layout[1]);
draw_progress_bar(frame, app, main_layout[1]);
}
/// 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".green().bold()).borders(Borders::ALL))
.block(
Block::default()
.title("Song List".green().bold())
.borders(Borders::ALL),
)
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
.highlight_symbol(">>")
.repeat_highlight_symbol(true);
@ -67,7 +53,9 @@ fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) {
/// draws playing queue
fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
let mut queue_state = ListState::default();
let title = Block::default().title(Title::from("Play Queue".green().bold())).title("Shift + ▲ ▼ to scroll, Shift + Enter to play".yellow());
let title = Block::default()
.title(Title::from("Play Queue".green().bold()))
.title("Shift + ▲ ▼ to scroll, Shift + Enter to play".yellow());
let list = List::new(app.queue_list.list.clone())
.block(title.borders(Borders::ALL))
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
@ -83,7 +71,9 @@ fn draw_queue(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 title = Block::default().title(Title::from("Playlist".green().bold())).title("▲ ▼ to scroll, Use ► to add playlist to queue".yellow());
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())
.block(title.borders(Borders::ALL))
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
@ -93,3 +83,31 @@ fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) {
state.select(Some(app.pl_list.index));
frame.render_stateful_widget(list, size, &mut state);
}
// Progress Bar
fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) {
let song = app
.conn
.now_playing()
.unwrap()
.unwrap_or_else(|| "No Title Found".to_string());
let state = &app.conn.state;
// let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap_or_default();
let title = Block::default()
.title(Title::from(format!("{}: ", state).red().bold()))
.title(Title::from(song.green().bold()));
let progress_bar = LineGauge::default()
.block(title.borders(Borders::ALL))
.gauge_style(
Style::default()
.fg(Color::LightBlue)
.bg(Color::Gray)
.add_modifier(Modifier::BOLD),
)
.line_set(symbols::line::THICK)
.ratio(0.2);
frame.render_widget(progress_bar, size);
}