summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Abbott <george@gabbott.dev>2025-01-26 11:37:47 +0000
committerGeorge Abbott <george@gabbott.dev>2025-01-26 11:37:47 +0000
commit52c4d3793e09bb95f154880fd1372333f94f66cd (patch)
tree0cbee2f7a27fc55f89da4125dc3a9eb513b06c4f
parent3f62410c524fd647c083f7225251e3b09808c6bb (diff)
wv
-rw-r--r--scripts/wv/wv.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/scripts/wv/wv.cpp b/scripts/wv/wv.cpp
new file mode 100644
index 0000000..f70afe5
--- /dev/null
+++ b/scripts/wv/wv.cpp
@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdnoreturn.h>
+
+struct common_state {};
+
+enum class editmode {
+ readonly,
+ edit,
+};
+
+
+common_state* common_state_new(void);
+void ideas(void)
+{
+ struct common_state *common = common_state_new();
+ system(common_state_get_wv_ideas(common));
+}
+void stats(void)
+{
+}
+void ls(long n) {}
+void ls_html(void)
+{
+ common_state *common = common_state_new();
+ // TODO: implement, by using dirent and iterating over the dir.
+ // Return the HTML, organised by date (reading the first line of each file)
+ // and with the word count. This is a trivially parallelizable problem.
+}
+void per_day(void) {}
+void per_month(void) {}
+void readedit(enum editmode em, long n) {}
+
+
+noreturn void usage(void)
+{
+ printf("wv\n");
+ printf("Flags:\n");
+ printf("Commands:\n");
+ printf(" If no command passed, open the next wv entry for writing.\n");
+ printf(" help Display this help message.\n");
+ printf(" ideas Edit the `ideas` file.\n");
+ printf(" stats Display entries, wordcount and average wordcount.\n");
+ printf(" ls (N) Display the (N most recent) entries.\n");
+ printf(" ls-html Display the entries as HTML.\n");
+ printf(" per-day The amount of entries written per day.\n");
+ printf(" per-month The amount of entries written per month.\n");
+ printf(" read ENT Read an entry without being able to edit it.\n");
+ printf(" ENT Edit the entry number.\n");
+ exit(0);
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 1)
+ {
+ fprintf(stderr, "too few arguments\n");
+ exit(-1);
+ }
+
+ const char *cmd = argv[1];
+ if (!strcmp(cmd, "help"))
+ usage();
+ else if (!strcmp(cmd, "ideas"))
+ ideas();
+ else if (!strcmp(cmd, "stats"))
+ stats();
+ else if (!strcmp(cmd, "ls"))
+ {
+ if (argc == 2)
+ {
+ ls(LONG_MAX);
+ }
+ else
+ {
+ long n = strtol(cmd[2], NULL, 10);
+ if (n <= 0)
+ {
+ fprintf(stderr, "Must provide a positive integer to ls, provided %s\n", cmd[2]);
+ exit(-1);
+ }
+ ls(n);
+ }
+ }
+ else if (!strcmp(cmd, "ls-html"))
+ ls_html();
+ else if (!strcmp(cmd, "per-day"))
+ per_day();
+ else if (!strcmp(cmd, "per-month"))
+ per_month();
+ else
+ {
+ const char *ent;
+ enum editmode em;
+
+ if (!strcmp(cmd, "read") || !strcmp(cmd, "edit"))
+ {
+ if (argc == 2)
+ {
+ fprintf(stderr, "%s command must have entry number\n", cmd);
+ exit(-1);
+ }
+
+ em = !strcmp(cmd, "read") ? editmode::readonly : editmode::edit;
+ ent = argv[2];
+ }
+ else
+ {
+ em = em_edit;
+ ent = argv[1];
+ }
+
+ long n = strtol(ent, NULL, 10);
+ if (n < 0)
+ {
+ fprintf(stderr, "Entry must be a positive number, instead received %s\n", ent);
+ exit(-1);
+ }
+
+ readedit(em, n);
+ }
+}