hirc

IRC client
Log | Files | Refs

commit b3f68efb4df18f49f4e3574cfb6818c32c566a9b
parent 8e986686041870db8acea72d6ffae25b2d877a50
Author: hhvn <dev@hhvn.uk>
Date:   Fri, 25 Feb 2022 20:35:41 +0000

Time formatting with %{time:f,s} as opposed to only for ${time}

Diffstat:
Mhirc.1.header | 6+++++-
Msrc/config.c | 2+-
Msrc/ui.c | 47++++++++++++++++++++++++++++++++---------------
3 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/hirc.1.header b/hirc.1.header @@ -75,6 +75,9 @@ Pad string s to n length, using formats. .It %{split:n,c,s} Split string s using character c, and print nth element. +.It %{time:f,s} +If string s is a unix timestamp, format it by passing f to +.Xr strftime 3 "." .El Certain variables may also be used in formats: @@ -103,7 +106,8 @@ This selects a parameter of a message. Where n is a non-zero positive integer. This selects all parameters following n. .It ${time} -May also be written as ${time:format} where format is passed to +Unix timestamp of a message in string form. +Should most likely be used with the %{time:f,s} styling. .Xr strftime 3 "." .El diff --git a/src/config.c b/src/config.c @@ -196,7 +196,7 @@ struct Config config[] = { .description = { "Turn on/off timestamps", NULL}}, {"format.ui.timestamp", 1, Val_string, - .str = "%{c:92}${time}%{o} ", + .str = "%{c:92}%{time:%H:%M,${time}}%{o} ", .strhandle = config_redraws, .description = { "Format of timestamps", diff --git a/src/ui.c b/src/ui.c @@ -1231,7 +1231,7 @@ ui_format(struct Window *window, char *format, struct History *hist) { struct Nick *nick; size_t rc, pc; int escape, i; - long pn; + long long pn; int rhs = 0; int divider = 0; char **params; @@ -1239,6 +1239,7 @@ ui_format(struct Window *window, char *format, struct History *hist) { char *ts, *save; char colourbuf[2][3]; char chs[2]; + size_t len; enum { sub_raw, sub_cmd, @@ -1248,6 +1249,7 @@ ui_format(struct Window *window, char *format, struct History *hist) { sub_channel, sub_topic, sub_server, + sub_time, }; struct { char *name; @@ -1261,6 +1263,7 @@ ui_format(struct Window *window, char *format, struct History *hist) { [sub_channel] = {"channel", NULL}, [sub_topic] = {"topic", NULL}, [sub_server] = {"server", NULL}, + [sub_time] = {"time", NULL}, {NULL, NULL}, }; @@ -1287,6 +1290,10 @@ ui_format(struct Window *window, char *format, struct History *hist) { } } + len = snprintf(subs[sub_time].val, 0, "%lld", (long long)hist->timestamp) + 1; + subs[sub_time].val = emalloc(len); + snprintf(subs[sub_time].val, len, "%lld", (long long)hist->timestamp); + params = hist->params; subs[sub_cmd].val = *params; params++; @@ -1343,19 +1350,6 @@ ui_format(struct Window *window, char *format, struct History *hist) { } } - if (hist && content && strncmp(content, "time:", strlen("time:")) == 0 || strcmp(content, "time") == 0) { - /* This always continues, so okay to modify content */ - content = strtok(content, ":"); - content = strtok(NULL, ":"); - - if (!content) - rc += strftime(&ret[rc], sizeof(ret) - rc, "%H:%M", gmtime(&hist->timestamp)); - else - rc += strftime(&ret[rc], sizeof(ret) - rc, content, gmtime(&hist->timestamp)); - format = strchr(format, '}') + 1; - continue; - } - for (i=0; subs[i].name; i++) { if (strcmp_n(subs[i].name, content) == 0) { if (subs[i].val) @@ -1457,7 +1451,7 @@ ui_format(struct Window *window, char *format, struct History *hist) { break; } - /* pad, nick and split must then continue as they modify content */ + /* pad, nick, split and time, must then continue as they modify content */ if (strncmp(content, "pad:", strlen("pad:")) == 0 && strchr(content, ',')) { pn = strtol(content + strlen("pad:"), NULL, 10); content = estrdup(ui_format_get_content(strchr(format+2+strlen("pad:"), ',') + 1, 1)); @@ -1475,6 +1469,27 @@ ui_format(struct Window *window, char *format, struct History *hist) { continue; } + if (strncmp(content, "time:", strlen("time:")) == 0 && strchr(content, ',')) { + content = estrdup(ui_format_get_content(strchr(format+2+strlen("time:"), ',') + 1, 1)); + save = estrdup(ret); + recursive = 1; + p = estrdup(ui_format(NULL, content, hist)); + recursive = 0; + memcpy(ret, save, rc); + pn = strtoll(p, NULL, 10); + + free(p); + p = struntil(format+2+strlen("time:"), ','); + + rc += strftime(&ret[rc], sizeof(ret) - rc, p, gmtime((time_t *)&pn)); + format = strchr(format+2+strlen("time:"), ',') + strlen(content) + 2; + + free(content); + free(save); + /* don't free p */ + continue; + } + /* second comma ptr - second comma ptr = distance. * If the distance is 2, then there is one non-comma char between. */ p = strchr(content, ','); @@ -1610,6 +1625,8 @@ ui_format(struct Window *window, char *format, struct History *hist) { } } + if (subs[sub_time].val) + free(subs[sub_time].val); if (ts[0] != '\0') free(ts);