[OCaml] Mobile-friendly clone of cgit.
feat show relative 'ago' time in commit list items
Add a time_ago helper that computes human-readable relative timestamps (e.g. '3 days ago', '2 months ago'). Commit list items now display the timestamp and summary left-aligned with the relative time right-aligned. Uses flexbox layout with text overflow ellipsis on the left content. Applies to both the summary and commits pages.
Changed files
lib/static/styles.css
@@ -165,7 +165,10 @@
165
165
}
166
166
167
167
div#main ul li a {
168
Removed:
display: block;
168
Added:
display: flex;
169
Added:
align-items: center;
170
Added:
justify-content: space-between;
171
Added:
gap: 1em;
169
172
color: white;
170
173
text-decoration: none;
171
174
padding: 0.75em;
@@ -177,6 +180,25 @@
177
180
background-color: white;
178
181
color: black;
179
182
text-decoration: revert;
183
Added:
}
184
Added:
185
Added:
.commit-left {
186
Added:
flex: 1;
187
Added:
min-width: 0;
188
Added:
overflow: hidden;
189
Added:
text-overflow: ellipsis;
190
Added:
white-space: nowrap;
191
Added:
}
192
Added:
193
Added:
.commit-ago {
194
Added:
flex-shrink: 0;
195
Added:
font-size: 0.85em;
196
Added:
color: #999;
197
Added:
white-space: nowrap;
198
Added:
}
199
Added:
200
Added:
div#main a:hover .commit-ago {
201
Added:
color: #555;
180
202
}
181
203
182
204
h1 {
lib/views/repo.ml
@@ -39,14 +39,54 @@
39
39
Printf.sprintf "%04d-%02d-%02d %02d:%02d" (tm.tm_year + 1900)
40
40
(tm.tm_mon + 1) tm.tm_mday tm.tm_hour tm.tm_min
41
41
in
42
Added:
let time_ago (date, _) =
43
Added:
let commit_time = Int64.to_float date in
44
Added:
let now = Unix.time () in
45
Added:
let diff = now -. commit_time in
46
Added:
let seconds = int_of_float diff in
47
Added:
if seconds < 60 then "just now"
48
Added:
else
49
Added:
let minutes = seconds / 60 in
50
Added:
if minutes < 60 then
51
Added:
Printf.sprintf "%d minute%s ago" minutes
52
Added:
(if minutes = 1 then "" else "s")
53
Added:
else
54
Added:
let hours = minutes / 60 in
55
Added:
if hours < 24 then
56
Added:
Printf.sprintf "%d hour%s ago" hours (if hours = 1 then "" else "s")
57
Added:
else
58
Added:
let days = hours / 24 in
59
Added:
if days < 30 then
60
Added:
Printf.sprintf "%d day%s ago" days (if days = 1 then "" else "s")
61
Added:
else
62
Added:
let months = days / 30 in
63
Added:
if months < 12 then
64
Added:
Printf.sprintf "%d month%s ago" months
65
Added:
(if months = 1 then "" else "s")
66
Added:
else
67
Added:
let years = months / 12 in
68
Added:
Printf.sprintf "%d year%s ago" years
69
Added:
(if years = 1 then "" else "s")
70
Added:
in
42
71
let timestamp_span =
43
72
HTML.(
44
73
span [ class_ "timestamp" ] [ txt "%s" (timestamp commit.author.date) ])
45
74
in
46
75
let summary = commit_summary commit.message in
47
76
let description = if summary = "" then HTML.null [] else txt " %s" summary in
77
Added:
let ago_span =
78
Added:
HTML.(
79
Added:
span [ class_ "commit-ago" ] [ txt "%s" (time_ago commit.author.date) ])
80
Added:
in
48
81
let route = Routes.Commit (repo, commit.hash) in
49
Removed:
let node = HTML.null [ timestamp_span; description ] in
82
Added:
let node =
83
Added:
HTML.(
84
Added:
null
85
Added:
[
86
Added:
span [ class_ "commit-left" ] [ timestamp_span; description ];
87
Added:
ago_span;
88
Added:
])
89
Added:
in
50
90
HTML.li [] [ Routes.link_to route node ]
51
91
52
92
let li_of_entry repo (entry : Resolvers.Entry.t) =