diff options
Diffstat (limited to 'caln')
-rw-r--r-- | caln/Makefile | 28 | ||||
-rwxr-xr-x | caln/addevent | 7 | ||||
-rwxr-xr-x | caln/agenda | 5 | ||||
-rwxr-xr-x | caln/caln | bin | 0 -> 21872 bytes | |||
-rw-r--r-- | caln/caln.c | 178 | ||||
-rw-r--r-- | caln/caln.o | bin | 0 -> 14024 bytes | |||
-rwxr-xr-x | caln/delevent | 7 | ||||
-rwxr-xr-x | caln/mkevent | bin | 0 -> 19392 bytes | |||
-rw-r--r-- | caln/mkevent.c | 39 | ||||
-rw-r--r-- | caln/mkevent.o | bin | 0 -> 8416 bytes | |||
-rwxr-xr-x | caln/sortevents | 6 |
11 files changed, 270 insertions, 0 deletions
diff --git a/caln/Makefile b/caln/Makefile new file mode 100644 index 0000000..5b7cd37 --- /dev/null +++ b/caln/Makefile @@ -0,0 +1,28 @@ +CFLAGS = -std=c23 -ggdb +LIBS = -lm + +SRC = caln.c mkevent.c +OBJ = $(SRC:.c=.o) + +all: caln + +.c.o: + cc -c ${CFLAGS} $< + +caln: caln.o mkevent.o + cc caln.o -o caln ${CFLAGS} ${LIBS} + cc mkevent.o -o mkevent ${CFLAGS} ${LIBS} + +install: caln + cp caln mkevent addevent delevent sortevents agenda /usr/local/bin/ + +uninstall: + rm /usr/local/bin/caln + rm /usr/local/bin/mkevent + rm /usr/local/bin/addevent + rm /usr/local/bin/delevent + rm /usr/local/bin/sortevents + rm /usr/local/bin/agenda + +clean: + rm -rf *.o caln mkevent diff --git a/caln/addevent b/caln/addevent new file mode 100755 index 0000000..51dc52e --- /dev/null +++ b/caln/addevent @@ -0,0 +1,7 @@ +#!/bin/sh + +eventfile="${HOME}/.cache/caln" + +touch $eventfile + +(mkevent "$@" || exit 1) >> $eventfile diff --git a/caln/agenda b/caln/agenda new file mode 100755 index 0000000..b9179f0 --- /dev/null +++ b/caln/agenda @@ -0,0 +1,5 @@ +#!/bin/sh + +calnfile="${HOME}/.cache/caln" + +caln < $calnfile diff --git a/caln/caln b/caln/caln Binary files differnew file mode 100755 index 0000000..b01246b --- /dev/null +++ b/caln/caln diff --git a/caln/caln.c b/caln/caln.c new file mode 100644 index 0000000..5c36db9 --- /dev/null +++ b/caln/caln.c @@ -0,0 +1,178 @@ +#include <time.h> +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +#include <stdlib.h> + +#define DATEFMT "%a %d %b %Y" +#define SEP " ~> " + +typedef struct event { + char *name; + time_t time; +} event; + +static bool cal = true; + +static const char *days[] = { + "Su", + "Mo", + "Tu", + "We", + "Th", + "Fr", + "Sa" +}; + +static const char *months[] = { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" +}; + +static const int daysinmonth[] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +static bool +isleap(int year) { + if ((year % 4) == 0 && (year % 100) != 0) + return true; + else if ((year % 400) == 0) + return true; + return false; +} + +static void +print_month(struct tm *t) { + printf(" %s %d\n", months[t->tm_mon], t->tm_year + 1900); +} + +static void +print_days() { + for (int i = 0; i < 7; i++) + printf("%s ", days[i]); + putchar('\n'); +} + +static void +print_padded(int n, int l) { + int i = 1; +loop: + if ((n / i) > 0) { + i *= 10; + l--; + goto loop; + } + + if (l != 0) + printf("%*s", l, " "); + if (l < 0) + return; + + printf("%d", n); +} + +static void +print_month_grid(struct tm *t) { + int day = 1; + int total = daysinmonth[t->tm_mon]; + if (t->tm_mon == 1 && isleap(t->tm_year + 1900)) + total += 1; + + int offset = (t->tm_yday % 7) - 1; + for (int i = 0; i < offset; i++) + printf(" "); + + for (; day < total + 1; day++) { + if (day == t->tm_mday) { + printf("\e[4;34m"); + print_padded(day, 2); + printf("\e[0m"); + } else + print_padded(day, 2); + putchar(' '); + if (((day + offset) % 7) == 0) + putchar('\n'); + } + putchar('\n'); +} + +static void +print_cal(struct tm *t) { + print_month(t); + print_days(); + print_month_grid(t); +} + +static event * +read_event(FILE *f) { + event *e = malloc(sizeof(event)); + char buf[256] = {0}; + if (!fgets(buf, 256, f)) { + free(e); + return NULL; + } + char *time = buf; + char *name = strchr(buf, '|'); + if (!name) { + fprintf(stderr, "invalid event! %s\n", buf); + exit(1); + } + *name = 0; + name++; + + e->name = strdup(name); + e->time = atoll(time); + return e; +} + +static void +print_event(event *e) { + struct tm *t = localtime(&e->time); + char buf[256]; + strftime(buf, 256, DATEFMT, t); + printf("%s" SEP "%s", buf, e->name); +} + +static void +free_event(event *e) { + free(e->name); + free(e); +} + +int +main(int argc, char **argv) { + if (argc > 1) + if (strcmp(argv[1], "-h") == 0) + cal = false; + + time_t ulocal = time(NULL); + struct tm *local = localtime(&ulocal); + + if (cal) + print_cal(local); + + bool dirty = false; + event *e; + while (e = read_event(stdin)) { + if (ulocal + (86400 * 30) > e->time) { + print_event(e); + dirty = true; + } + free_event(e); + } + if (!dirty) + printf("You have nothing upcoming\n"); + + return 0; +} diff --git a/caln/caln.o b/caln/caln.o Binary files differnew file mode 100644 index 0000000..3678ef4 --- /dev/null +++ b/caln/caln.o diff --git a/caln/delevent b/caln/delevent new file mode 100755 index 0000000..eb13b08 --- /dev/null +++ b/caln/delevent @@ -0,0 +1,7 @@ +#!/bin/sh + +calnfile="${HOME}/.cache/caln" + +line=$(grep $@ $calnfile | fzf || exit 1) + +sed -i.bak "/${line}/d" $calnfile diff --git a/caln/mkevent b/caln/mkevent Binary files differnew file mode 100755 index 0000000..57e9742 --- /dev/null +++ b/caln/mkevent diff --git a/caln/mkevent.c b/caln/mkevent.c new file mode 100644 index 0000000..6c3f025 --- /dev/null +++ b/caln/mkevent.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +static void +usage() { + fprintf(stderr, "mkevent \"contents\" Tomorrow | NUM NUM [NUM]\n"); + exit(1); +} + +int +main (int argc, char **argv) { + if (argc < 3) + usage(); + + time_t ut = time(NULL); + struct tm *t = localtime(&ut); + + if ((strcmp(argv[2], "tomorrow") & strcmp(argv[2], "Tomorrow") & strcmp("Tom", argv[2])) == 0) + ut += 86400; + else { + char buf[256] = {0}; + snprintf(buf, 256, "%s %s", + (argv[2]) ? argv[2] : "", + (argv[3]) ? argv[3] : ""); + int day, month; + int ret = sscanf(buf, "%d %d", &day, &month); + + t->tm_mday = day; + t->tm_mon = month - 1; + + if (ret < 2) usage(); + + ut = mktime(t); + } + + printf("%zu|%s\n", ut, argv[1]); +} diff --git a/caln/mkevent.o b/caln/mkevent.o Binary files differnew file mode 100644 index 0000000..f16d483 --- /dev/null +++ b/caln/mkevent.o diff --git a/caln/sortevents b/caln/sortevents new file mode 100755 index 0000000..ebedb3c --- /dev/null +++ b/caln/sortevents @@ -0,0 +1,6 @@ +#!/bin/sh + +calnfile="${HOME}/.cache/caln" + +(sort $calnfile || exit 1) > /tmp/caln.sort +mv /tmp/caln.sort $calnfile |