aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/node.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/routes/ui/node.rs')
-rw-r--r--server/src/routes/ui/node.rs46
1 files changed, 34 insertions, 12 deletions
diff --git a/server/src/routes/ui/node.rs b/server/src/routes/ui/node.rs
index ebd21db..3b30c57 100644
--- a/server/src/routes/ui/node.rs
+++ b/server/src/routes/ui/node.rs
@@ -36,7 +36,7 @@ use jellycommon::{
Chapter, MediaInfo, Node, NodeID, NodeKind, PeopleGroup, Rating, SourceTrackKind, Visibility,
};
use rocket::{get, serde::json::Json, Either, State};
-use std::sync::Arc;
+use std::{fmt::Write, sync::Arc};
/// This function is a stub and only useful for use in the uri! macro.
#[get("/n/<id>")]
@@ -325,22 +325,44 @@ pub fn aspect_class(kind: NodeKind) -> &'static str {
}
}
-pub fn format_duration(mut d: f64) -> String {
+pub fn format_duration(d: f64) -> String {
+ format_duration_mode(d, false)
+}
+pub fn format_duration_long(d: f64) -> String {
+ format_duration_mode(d, true)
+}
+fn format_duration_mode(mut d: f64, long_units: bool) -> String {
let mut s = String::new();
let sign = if d > 0. { "" } else { "-" };
d = d.abs();
- for (unit, k) in [("h", 60. * 60.), ("m", 60.), ("s", 1.)] {
- let mut h = 0;
- // TODO dont iterate like that. can be a simple rem and div
- while d > k {
- d -= k;
- h += 1;
- }
- if h > 0 {
- s += &format!("{h}{unit}")
+ for (short, long, k) in [
+ ("d", "day", 60. * 60. * 24.),
+ ("h", "hour", 60. * 60.),
+ ("m", "minute", 60.),
+ ("s", "second", 1.),
+ ] {
+ let h = (d / k).floor();
+ d -= h * k;
+ if h > 0. {
+ if long_units {
+ // TODO breaks if seconds is zero
+ write!(
+ s,
+ "{}{h} {long}{}{}",
+ if k != 1. { "" } else { " and " },
+ if h != 1. { "s" } else { "" },
+ if k > 60. { ", " } else { "" },
+ )
+ .unwrap();
+ } else {
+ write!(s, "{h}{short} ").unwrap();
+ }
}
}
- format!("{sign}{s}")
+ format!("{sign}{}", s.trim())
+}
+pub fn format_size(size: u64) -> String {
+ humansize::format_size(size, humansize::DECIMAL)
}
pub trait DatabaseNodeUserDataExt {