commit 46c296fb5b58130b951038f24e9ab36f7aa1e776
parent ee911b44d6ea7ae2ccb98ce62f009dc8a07d0309
Author: hhvn <dev@hhvn.uk>
Date: Tue, 30 Nov 2021 18:12:15 +0000
config.c ui.c hirc.1.header: implement timestamps
Diffstat:
M | config.c | | | 54 | +++++++++++++++++++++++++++++++++--------------------- |
M | hirc.1.header | | | 3 | +++ |
M | ui.c | | | 34 | ++++++++++++++++++++++++++++++++-- |
3 files changed, 68 insertions(+), 23 deletions(-)
diff --git a/config.c b/config.c
@@ -14,8 +14,8 @@ static int config_buflist_location(long num);
static int config_buflist_width(long num);
static int config_nickcolour_self(long num);
static int config_nickcolour_range(long a, long b);
-static int config_divider(long num);
-static int config_format(char *str);
+static int config_redrawl(long num);
+static int config_redraws(char *str);
char *valname[] = {
[Val_string] = "a string",
@@ -150,89 +150,101 @@ struct Config config[] = {
"Message to send on /part", NULL}},
{"divider.toggle", 1, Val_bool,
.num = 1,
- .numhandle = config_divider,
+ .numhandle = config_redrawl,
.description = {
"Turn divider on/off", NULL}},
{"divider.margin", 1, Val_nzunsigned,
.num = 15,
- .numhandle = config_divider,
+ .numhandle = config_redrawl,
.description = {
"Number of columns on the left of the divider", NULL}},
{"divider.string", 1, Val_string,
.str = " ",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"String to be used as divider", NULL}},
+ {"timestamp.toggle", 1, Val_bool,
+ .num = 1,
+ .numhandle = config_redrawl,
+ .description = {
+ "Turn on/off timestamps", NULL}},
+ {"format.ui.timestamp", 1, Val_string,
+ .str = "%{c:92}${time}%{o} ",
+ .strhandle = config_redraws,
+ .description = {
+ "Format of timestamps",
+ "Only shown if timestamp.toggle is on."
+ "This format is special as it is included in others.", NULL}},
{"format.ui.topic", 1, Val_string,
.str = "%{c:99,89}${topic}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of topic at top of main window", NULL}},
{"format.ui.error", 1, Val_string,
.str = "%{c:28}%{b}${3} %{b}(at ${1}:${2})",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_ERROR messages", NULL}},
{"format.ui.misc", 1, Val_string,
.str = "${1}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_UI messages", NULL}},
{"format.ui.connectlost", 1, Val_string,
.str = "Connection to ${1} (${2}:${3}) lost: ${4}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_CONNECTLOST messages", NULL}},
{"format.ui.connecting", 1, Val_string,
.str = "Connecting to ${1}:${2}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_CONNECTING messages", NULL}},
{"format.ui.connected", 1, Val_string,
.str = "Connection to ${1} established",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_CONNECTED messages", NULL}},
{"format.ui.lookupfail", 1, Val_string,
.str = "Failed to lookup ${2}: ${4}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_LOOKUPFAIL messages", NULL}},
{"format.ui.connectfail", 1, Val_string,
.str = "Failed to connect to ${2}:${3}: ${4}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_CONNECTFAIL messages", NULL}},
#ifndef TLS
{"format.ui.tlsnotcompiled", 1, Val_string,
.str = "TLS not compiled into hirc",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of SELF_TLSNOTCOMPILED messages", NULL}},
#endif /* TLS */
{"format.privmsg", 1, Val_string,
.str = "${nick}%{=}${2}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of messages", NULL}},
{"format.join", 1, Val_string,
.str = "%{b}%{c:44}+%{o}%{=}${nick} (${ident}@${host})",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of JOIN messages", NULL}},
{"format.quit", 1, Val_string,
.str = "%{b}%{c:40}<%{o}%{=}${nick} (${ident}@${host}): ${1}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of QUIT messages", NULL}},
{"format.part", 1, Val_string,
.str = "%{b}%{c:40}-%{o}%{=}${nick} (${ident}@${host}): ${2}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of PART messages", NULL}},
{"format.other", 1, Val_string,
.str = "${raw}",
- .strhandle = config_format,
+ .strhandle = config_redraws,
.description = {
"Format of other messages without formats", NULL}},
{NULL},
@@ -504,13 +516,13 @@ config_nickcolour_range(long a, long b) {
}
static int
-config_format(char *str) {
+config_redraws(char *str) {
ui_redraw();
return 1;
}
static int
-config_divider(long num) {
+config_redrawl(long num) {
windows[Win_main].refresh = 1;
return 1;
}
diff --git a/hirc.1.header b/hirc.1.header
@@ -71,6 +71,9 @@ Selected server, or server the message was from.
.It ${n}
Where n is a non-zero positive integer.
This selects a paramater of a message.
+.It ${time}
+May also be written as ${time:format} where format is passed to
+.Xr strftime 3 "."
.El
For example, the message:
diff --git a/ui.c b/ui.c
@@ -799,11 +799,12 @@ ui_select(struct Server *server, struct Channel *channel) {
char *
ui_format(char *format, struct History *hist) {
static char ret[8192];
+ static int nots = 0;
int rc, escape, pn, i;
int rhs = 0;
int divider = 0;
char **params;
- char *tmp, *p;
+ char *tmp, *p, *ts;
char colourbuf[2][3];
char printformat[64];
enum {
@@ -831,6 +832,8 @@ ui_format(char *format, struct History *hist) {
{NULL, NULL},
};
+ /* ${time} is implemented specially so doesn't appear in list */
+
subs[sub_channel].val = selected.channel ? selected.channel->name : NULL;
subs[sub_topic].val = selected.channel ? selected.channel->topic : NULL;
subs[sub_server].val = selected.server ? selected.server->name : NULL;
@@ -859,6 +862,14 @@ ui_format(char *format, struct History *hist) {
params++;
}
+ if (!nots && hist && config_getl("timestamp.toggle")) {
+ nots = 1;
+ ts = strdup(ui_format(config_gets("format.ui.timestamp"), hist));
+ nots = 0;
+ } else {
+ ts = "";
+ }
+
for (escape = 0, rc = 0; format && *format && rc < sizeof(ret); ) {
if (!escape && *format == '$' && *(format+1) == '{' && strchr(format, '}')) {
escape = 0;
@@ -875,6 +886,18 @@ ui_format(char *format, struct History *hist) {
}
}
+ if (hist && tmp && strncmp(tmp, "time:", strlen("time:")) == 0 || strcmp(tmp, "time") == 0) {
+ tmp = strtok(tmp, ":");
+ tmp = strtok(NULL, ":");
+
+ if (!tmp)
+ rc += strftime(&ret[rc], sizeof(ret) - rc, "%H:%M", gmtime(&hist->timestamp));
+ else
+ rc += strftime(&ret[rc], sizeof(ret) - rc, tmp, gmtime(&hist->timestamp));
+ format = strchr(format, '}') + 1;
+ continue;
+ }
+
for (i=0; subs[i].name; i++) {
if (strcmp_n(subs[i].name, tmp) == 0) {
if (subs[i].val)
@@ -993,12 +1016,19 @@ ui_format(char *format, struct History *hist) {
}
ret[rc] = '\0';
- if (divider && !rhs) {
+ if (!nots && divider && !rhs) {
snprintf(printformat, sizeof(printformat), "%%%lds%%s%%s", config_getl("divider.margin"));
tmp = strdup(ret);
rc = snprintf(ret, sizeof(ret), printformat, "", config_gets("divider.string"), tmp);
free(tmp);
}
+ tmp = strdup(ret);
+ rc = snprintf(ret, sizeof(ret), "%s%s", ts, tmp);
+ free(tmp);
+
+ if (ts[0] != '\0')
+ free(ts);
+
return ret;
}