//! Small pieces of HTML shared by every page: icons, dates, sizes and the page layout.
use crate::templates;
pub(crate) fn icon(name: &str) -> String {
let path = match name {
"dir" => r#""#,
"file" => r#""#,
"branch" => {
r#""#
}
"tag" => r#""#,
"book" => {
r#""#
}
"code" => r#""#,
"commits" => r#""#,
"chevron" => r#""#,
"up" => r#""#,
"module" => r#""#,
_ => "",
};
format!(r#""#)
}
/// "3 hours ago" for recent times, the calendar date (as git printed it) after a month.
pub(crate) fn ago(stamp: &str, date: &str) -> String {
let Ok(then) = stamp.parse::() else { return date.to_string() };
let span = gitv_core::unix_now().saturating_sub(then);
let unit = |n: u64, word: &str| format!("{n} {word}{} ago", if n == 1 { "" } else { "s" });
match span {
0..60 => "just now".into(),
60..3600 => unit(span / 60, "minute"),
3600..86400 => unit(span / 3600, "hour"),
86400..2_592_000 => unit(span / 86400, "day"),
_ => date.to_string(),
}
}
/// A commit subject for display; an empty one would leave an invisible link.
pub(crate) fn subject_or_placeholder(subject: &str) -> &str {
if subject.trim().is_empty() { "(no message)" } else { subject }
}
pub(crate) fn plural(count: usize, one: &str, many: &str) -> String {
format!("{count} {}", if count == 1 { one } else { many })
}
pub(crate) fn size(bytes: usize) -> String {
match bytes {
0..1024 => plural(bytes, "byte", "bytes"),
1024..1_048_576 => format!("{:.1} KB", bytes as f64 / 1024.0),
_ => format!("{:.1} MB", bytes as f64 / 1_048_576.0),
}
}
/// The page for a render that failed on the server's side: a bug, not a bad link.
pub(crate) fn view_failed(signed_in: bool) -> String {
templates::Notice {
title: "Something went wrong",
signed_in,
heading: "This page couldn’t be shown",
message: "Something went wrong on the server. Try again; if it keeps happening, the operator can see why in the log.",
code: None,
link: Some(("/", "Back to repositories")),
}
.to_string()
}
/// The page for anything that isn't there, or a malformed link.
pub(crate) fn view_missing(signed_in: bool) -> String {
templates::Notice {
title: "Not found",
signed_in,
heading: "Nothing here",
message: "That repository, branch, file or commit doesn’t exist, or the link is malformed.",
code: None,
link: Some(("/", "Back to repositories")),
}
.to_string()
}