Mercurial > paster
diff http.c @ 10:75cfe3795de3
pasterd: improve time left on index
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 05 Feb 2020 15:57:33 +0100 |
parents | e8f61741aaec |
children | 93f0440d452e |
line wrap: on
line diff
--- a/http.c Wed Feb 05 14:26:19 2020 +0100 +++ b/http.c Wed Feb 05 15:57:33 2020 +0100 @@ -107,7 +107,8 @@ "author", /* /fork only */ "code", /* /fork only */ "private", /* /fork only */ - "languages" + "languages", + "durations" }; static const char *languages[] = { @@ -291,6 +292,17 @@ NULL }; +static const struct { + const char *title; + long long int secs; +} durations[] = { + { "month", PASTE_MONTH }, + { "week", PASTE_WEEK }, + { "day", PASTE_DAY }, + { "hour", PASTE_HOUR }, + { NULL, -1 } +}; + static const char * template(const char *filename) { @@ -318,6 +330,21 @@ return PASTE_MONTH; } +static const char * +ttl(time_t timestamp, long long int duration) +{ + const time_t now = time(NULL); + const long long int left = duration - difftime(now, timestamp); + + if (left < PASTE_HOUR) + return bprintf("%lld minute(s)", left / 60); + if (left < PASTE_DAY) + return bprintf("%lld hour(s)", left / 3600); + + /* Other in days. */ + return bprintf("%lld day(s)", left / 86400); +} + static void render_languages(struct kreq *req, const struct paste *paste) { @@ -333,6 +360,23 @@ } } +static void +render_durations(struct kreq *req, const struct paste *paste) +{ + for (size_t i = 0; durations[i].title != NULL; ++i) { + const char *line; + + if (paste->duration == durations[i].secs) + line = bprintf("<option value=\"%s\" selected>%s</option>", + durations[i].title, durations[i].title); + else + line = bprintf("<option value=\"%s\">%s</option>", + durations[i].title, durations[i].title); + + khttp_puts(req, line); + } +} + static int tmpl_paste(size_t index, void *arg) { @@ -362,8 +406,7 @@ khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No")); break; case 7: - /* TODO: convert time left. */ - khttp_puts(data->req, "TODO"); + khttp_puts(data->req, ttl(paste->timestamp, paste->duration)); break; default: break; @@ -392,7 +435,7 @@ khttp_puts(data->req, paste->language); break; case 4: - khttp_puts(data->req, bprintf("%d", paste->duration)); + khttp_puts(data->req, ttl(paste->timestamp, paste->duration)); break; case 5: khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp))); @@ -451,6 +494,9 @@ case 4: render_languages(data->req, paste); break; + case 5: + render_durations(data->req, paste); + break; default: break; }; @@ -528,7 +574,7 @@ }; const struct ktemplate kt = { .key = tmpl_new_keywords, - .keysz = 5, + .keysz = 6, .cb = tmpl_new, .arg = &data }; @@ -605,7 +651,7 @@ else { const struct ktemplate kt = { .key = tmpl_new_keywords, - .keysz = 5, + .keysz = 6, .cb = tmpl_new, .arg = &data };