GITv
master
html.rs81 lines, 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! 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#"<path d="M2 4.5c0-.6.4-1 1-1h3.2l1.4 1.5H13c.6 0 1 .4 1 1v6.5c0 .6-.4 1-1 1H3c-.6 0-1-.4-1-1z"/>"#,
        "file" => r#"<path d="M4 1.8h5.3L12.5 5v9.2H4z"/><path d="M9 1.8v3.5h3.5"/>"#,
        "branch" => {
            r#"<circle cx="4.5" cy="3.5" r="1.6"/><circle cx="4.5" cy="12.5" r="1.6"/><circle cx="11.5" cy="5" r="1.6"/><path d="M4.5 5.1v5.8M11.5 6.6c0 2.6-2.2 3.3-7 4.3"/>"#
        }
        "tag" => r#"<path d="M2 2.5h5.3l6.2 6.2-4.8 4.8-6.7-6.7z"/><circle cx="5" cy="5.5" r="1"/>"#,
        "book" => {
            r#"<path d="M2.5 3h4c.8 0 1.5.7 1.5 1.5V13c0-.6-.5-1-1-1h-4.5zM13.5 3h-4c-.8 0-1.5.7-1.5 1.5V13c0-.6.5-1 1-1h4.5z"/>"#
        }
        "code" => r#"<path d="M5.5 4.5 2 8l3.5 3.5M10.5 4.5 14 8l-3.5 3.5"/>"#,
        "commits" => r#"<circle cx="8" cy="8" r="2.4"/><path d="M1.5 8h4.1M10.4 8h4.1"/>"#,
        "chevron" => r#"<path d="M4.5 6.5 8 10l3.5-3.5"/>"#,
        "up" => r#"<path d="M8 13V3.5M4 7.5l4-4 4 4"/>"#,
        "module" => r#"<rect x="2.5" y="2.5" width="11" height="11" rx="2"/><path d="M5.5 8h5M8 5.5v5"/>"#,
        _ => "",
    };
    format!(r#"<svg class="ic" viewBox="0 0 16 16" aria-hidden="true">{path}</svg>"#)
}

/// "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::<u64>() 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()
}