summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-08-15 21:04:20 -0400
committerJune McEnroe <june@causal.agency>2022-08-15 21:04:20 -0400
commit04a1a4cf0a469393f38c71ebe91ad02fceb855cf (patch)
tree2f67250d69e00e1556cf8c8215312ad161665934 /bin
parentAllow names with prefixes of months and days (diff)
downloadsrc-04a1a4cf0a469393f38c71ebe91ad02fceb855cf.tar.gz
src-04a1a4cf0a469393f38c71ebe91ad02fceb855cf.zip
Load dates from ~/.config/when/dates
Diffstat (limited to 'bin')
-rw-r--r--bin/man1/when.114
-rw-r--r--bin/when.y33
2 files changed, 45 insertions, 2 deletions
diff --git a/bin/man1/when.1 b/bin/man1/when.1
index 205b2d81..64f51cae 100644
--- a/bin/man1/when.1
+++ b/bin/man1/when.1
@@ -68,6 +68,14 @@ A number of months.
 A number of years.
 .El
 .
+.Sh FILES
+The file
+.Pa $XDG_CONFIG_HOME/when/dates
+or
+.Pa ~/.config/when/dates
+is read before any other expressions,
+if it exists.
+.
 .Sh EXAMPLES
 .Bl -tag -width "Dec 25 - ."
 .It Ic Dec 25 - \&.
@@ -77,3 +85,9 @@ The date next Friday.
 .It Ic \&. + 2w
 Your last day at work.
 .El
+.Pp
+Checking a milestone:
+.Bd -literal -offset indent
+$ echo 'hrt = oct 15 2021' >> ~/.config/when/dates
+$ when -hrt
+.Ed
diff --git a/bin/when.y b/bin/when.y
index 83b9efdc..69ccc343 100644
--- a/bin/when.y
+++ b/bin/when.y
@@ -18,6 +18,9 @@
 
 #include <ctype.h>
 #include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
@@ -162,7 +165,10 @@ static void setDate(const char *name, struct tm date) {
 	dates.len++;
 }
 
+static bool silent;
+
 static void printDate(struct tm date) {
+	if (silent) return;
 	printf(
 		"%.3s %.3s %d %d\n",
 		Days[date.tm_wday], Months[date.tm_mon],
@@ -171,6 +177,7 @@ static void printDate(struct tm date) {
 }
 
 static void printScalar(struct tm scalar) {
+	if (silent) return;
 	if (scalar.tm_year) printf("%dy ", scalar.tm_year);
 	if (scalar.tm_mon) printf("%dm ", scalar.tm_mon);
 	if (scalar.tm_mday % 7) {
@@ -287,6 +294,30 @@ static int yylex(void) {
 }
 
 int main(int argc, char *argv[]) {
+	size_t cap = 0;
+	char *line = NULL;
+
+	char path[PATH_MAX];
+	const char *configHome = getenv("XDG_CONFIG_HOME");
+	if (configHome) {
+		snprintf(path, sizeof(path), "%s/when/dates", configHome);
+	} else {
+		snprintf(path, sizeof(path), "%s/.config/when/dates", getenv("HOME"));
+	}
+
+	FILE *file = fopen(path, "r");
+	if (file) {
+		silent = true;
+		while (0 < getline(&line, &cap, file)) {
+			input = line;
+			yyparse();
+		}
+		fclose(file);
+		silent = false;
+	} else if (errno != ENOENT) {
+		err(EX_CONFIG, "%s", path);
+	}
+
 	if (argc > 1) {
 		input = argv[1];
 		return yyparse();
@@ -296,8 +327,6 @@ int main(int argc, char *argv[]) {
 	printDate(date);
 	printf("\n");
 
-	char *line = NULL;
-	size_t cap = 0;
 	while (0 < getline(&line, &cap, stdin)) {
 		if (line[0] == '\n') continue;