Skip to content

Commit 0b6efe1

Browse files
authored
Merge pull request #32 from lautarodragan/taro/fix-overflow-3
fix: overflow in percentage calculation (alternative using .ratio)
2 parents 11a08a4 + 9f59ae9 commit 0b6efe1

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

src/app.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{
44
thread,
55
time::Duration,
66
};
7-
87
use kronos::gen_funcs;
98
use kronos::music_handler::MusicHandle;
109
use kronos::queue::Queue;
@@ -109,28 +108,18 @@ impl<'a> App<'a> {
109108
}
110109

111110
// if playing and
112-
pub fn song_progress(&mut self) -> u16 {
113-
let progress = || {
114-
let percentage =
115-
(self.music_handle.time_played() * 100) / self.music_handle.song_length();
116-
if percentage >= 100 {
117-
100
118-
} else {
119-
percentage
120-
}
121-
};
122-
111+
pub fn song_progress(&mut self) -> f64 {
123112
// edge case if nothing queued or playing
124113
if self.music_handle.sink_empty() && self.queue_items.is_empty() {
125-
0
114+
0.0
126115

127116
// if something playing, calculate progress
128117
} else if !self.music_handle.sink_empty() {
129-
progress()
118+
f64::clamp(self.music_handle.time_played() as f64 / self.music_handle.song_length() as f64, 0.0, 1.0)
130119
// if nothing playing keep rolling
131120
} else {
132121
self.auto_play();
133-
0
122+
0.0
134123
}
135124
}
136125

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fn music_tab<B: Backend>(f: &mut Frame<B>, app: &mut App, chunks: Rect, cfg: &Co
285285
)
286286
.style(Style::default().fg(cfg.foreground()))
287287
.gauge_style(Style::default().fg(cfg.highlight_background()))
288-
.percent(app.song_progress());
288+
.ratio(app.song_progress());
289289
f.render_widget(playing, queue_playing[1]);
290290
}
291291

0 commit comments

Comments
 (0)