Krolyxon
Commit 38ebdf9

rmptui38ebdf9

get rid of humantime crate

Author
krolxon
Date
2024-04-29 17:24 +0530
Parent
11d6472
Commit
38ebdf9283dbe4cae7d128e8800b589c7140ba7b

+24 −18

5 files changed

  1. M Cargo.lock +0 −7
  2. M Cargo.toml +0 −1
  3. M README.md +1 −1
  4. M src/app.rs +18 −1
  5. M src/ui.rs +5 −8
M Cargo.lock View
@@ -225,12 +225,6 @@ version = "0.5.0"
225225 source = "registry+https://github.com/rust-lang/crates.io-index"
226226 checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
227227
228−[[package]]
229−name = "humantime"
230−version = "2.1.0"
231−source = "registry+https://github.com/rust-lang/crates.io-index"
232−checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
233−
234228 [[package]]
235229 name = "indoc"
236230 version = "2.0.5"
@@ -392,7 +386,6 @@ version = "0.1.0"
392386 dependencies = [
393387 "clap",
394388 "crossterm",
395− "humantime",
396389 "mpd",
397390 "ratatui",
398391 "rust-fuzzy-search",
M Cargo.toml View
@@ -13,4 +13,3 @@ simple-dmenu = "0.1.0"
1313 ratatui = "0.26.2"
1414 crossterm = "0.27.0"
1515 rust-fuzzy-search = "0.1.1"
16−humantime = "2.1.0"
M README.md View
@@ -37,7 +37,7 @@ A MPD client in Rust
3737 - [x] improvements on queue control
3838 - [x] add to playlists
3939 - [x] search for songs
40−- [x] Humantime format
40+- [x] Human readable time format
4141 - [x] metadata based tree view
4242 - [ ] view playlist
4343 - [ ] change playlist name
M src/app.rs View
@@ -1,3 +1,5 @@
1+use std::time::Duration;
2+
13 use crate::browser::FileBrowser;
24 use crate::connection::Connection;
35 use crate::list::ContentList;
@@ -152,7 +154,9 @@ impl App {
152154 for (i, song) in self.queue_list.list.clone().iter().enumerate() {
153155 if song.contains(&file) {
154156 self.conn.conn.delete(i as u32).unwrap();
155− if self.queue_list.index == self.queue_list.list.len() - 1 && self.queue_list.index != 0 {
157+ if self.queue_list.index == self.queue_list.list.len() - 1
158+ && self.queue_list.index != 0
159+ {
156160 self.queue_list.index -= 1;
157161 }
158162 }
@@ -268,4 +272,17 @@ impl App {
268272 pub fn reset_cursor(&mut self) {
269273 self.cursor_position = 0;
270274 }
275+
276+ /// Given time in seconds, convert it to hh:mm:ss
277+ pub fn format_time(time: Duration) -> String {
278+ let time = time.as_secs();
279+ let h = time / 3600;
280+ let m = (time % 3600) / 60;
281+ let s = (time % 3600) % 60;
282+ if h == 0 {
283+ format!("{:02}:{:02}", m, s)
284+ } else {
285+ format!("{:02}:{:02}:{:02}", h, m, s)
286+ }
287+ }
271288 }
M src/ui.rs View
@@ -77,9 +77,8 @@ fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) {
7777 .collect::<Vec<String>>()
7878 .join("");
7979
80− let time = humantime::format_duration(
81− song.clone().duration.unwrap_or_else(|| Duration::new(0, 0)),
82− );
80+ let time =
81+ App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0)));
8382
8483 let mut status: bool = false;
8584 for sn in app.queue_list.list.iter() {
@@ -188,9 +187,7 @@ fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
188187 .collect::<Vec<String>>()
189188 .join("");
190189
191− let time = humantime::format_duration(
192− song.clone().duration.unwrap_or_else(|| Duration::new(0, 0)),
193− );
190+ let time = App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0)));
194191
195192 if s.contains(&app.conn.current_song.file) {
196193 let row = Row::new(vec![
@@ -353,8 +350,8 @@ fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) {
353350 let duration = if app.conn.total_duration.as_secs() != 0 {
354351 format!(
355352 "[{}/{}]",
356− humantime::format_duration(app.conn.elapsed),
357− humantime::format_duration(app.conn.total_duration)
353+ App::format_time(app.conn.elapsed),
354+ App::format_time(app.conn.total_duration)
358355 )
359356 } else {
360357 "".to_string()