format time with hours
[nihav-player.git] / sndplay / src / main.rs
index 66def2b05250b0fe3f3fe5a32aa330e12c3fafec..176a6d189d8e55b3821ca9c747aa17513ad4b81d 100644 (file)
@@ -193,10 +193,15 @@ fn format_time(ms: u64) -> String {
     let s = ms / 1000;
     let ds = (ms % 1000) / 100;
     let (min, s) = (s / 60, s % 60);
-    if min == 0 {
-        format!("{}.{}", s, ds)
+    let (h, min) = (min / 60, min % 60);
+    if h == 0 {
+        if min == 0 {
+            format!("{}.{}", s, ds)
+        } else {
+            format!("{}:{:02}.{}", min, s, ds)
+        }
     } else {
-        format!("{}:{:02}.{}", min, s, ds)
+        format!("{}:{:02}:{:02}.{}", h, min, s, ds)
     }
 }
 
@@ -335,15 +340,18 @@ impl Player {
         if !self.paused {
             device.resume();
         }
+        let mut no_display = false;
         'main: loop {
             let cur_time = decoder.samplepos.saturating_sub(u64::from(device.size() / 2 / u32::from(dst_info.channels)));
             let full_ms = cur_time * 1000 / u64::from(arate);
             let timestr = format_time(full_ms);
             let disp_vol = if self.mute { 0 } else { self.volume };
-            if !self.debug {
-                print!("> {} / {}   {}%       \r", timestr, duration_str, disp_vol);
-            } else {
-                print!("> {} / {}   |{}| {}%       \r", timestr, duration_str, device.size(), disp_vol);
+            if !no_display {
+                if !self.debug {
+                    print!("> {} / {}   {}%       \r", timestr, duration_str, disp_vol);
+                } else {
+                    print!("> {} / {}   |{}| {}%       \r", timestr, duration_str, device.size(), disp_vol);
+                }
             }
             std::io::stdout().flush().unwrap();
             if device.size() < underfill_limit && !self.paused && refill_limit < (1 << 20) {
@@ -403,6 +411,20 @@ impl Player {
                             device.resume();
                         }
                     },
+                    Command::Seek(seek_time) => {
+                        device.pause();
+                        device.clear();
+                        let _ret = decoder.seek(seek_time);
+                        while !eof && device.size() < refill_limit {
+                            eof = decoder.refill(&device);
+                        }
+                        if eof {
+                            break 'main;
+                        }
+                        if !self.paused {
+                            device.resume();
+                        }
+                    },
                     Command::Quit => {
                         device.pause();
                         self.ended = true;
@@ -449,8 +471,22 @@ impl Player {
                     Command::Debug => {
                         self.debug = !self.debug;
                     },
+                    Command::PauseDisplay  => {
+                        no_display = true;
+                        if !self.paused {
+                            device.pause();
+                        }
+                    },
+                    Command::ResumeDisplay => {
+                        no_display = false;
+                        if !self.paused {
+                            device.resume();
+                        }
+                    },
                 };
-                print!("\r{:60}\r", ' ');
+                if !no_display {
+                    print!("\r{:60}\r", ' ');
+                }
             }
             thread::sleep(Duration::from_millis(200));
         }