summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2021-10-03 22:16:59 -0400
committerRobby Zambito <contact@robbyzambito.me>2021-10-03 22:16:59 -0400
commit8c70e74489507fc57d8d26e850d2fd117ab79fbd (patch)
tree0dc41eb43bcfba908f6b0f17a117ac9029e7d519
parent905d799f527ef60470c0e80e41b720d9b265b90f (diff)
Added support for stdin for todo add
This accepts one line as one task. Also added some comments.
-rw-r--r--src/todo.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/todo.c b/src/todo.c
index c7f416f..e56f8f5 100644
--- a/src/todo.c
+++ b/src/todo.c
@@ -72,6 +72,8 @@ static void process_todo_file(void (*mapper_func)(char *),
}
}
+ // i keeps track of the line in the file we are on.
+ // j is the index of mapped_indices[] that we are currently waiting for.
for (size_t i = 1, j = 0;
(line_len = getline(&line, &line_buff_size, todo_file)) >= 0; i++) {
if (line_len > 5) {
@@ -121,15 +123,18 @@ static void print_list(char *line, ssize_t line_len, FILE *ignored,
void todo_list(void) { process_todo_file(NULL, NULL, 0, print_list, false); }
void todo_add(int argc, char **argv) {
- if (argc < 1) {
- fprintf(stderr, "todo add takes at least 1 argument\n");
- exit(EXIT_FAILURE);
- }
-
open_todo_file("a");
- for (int i = 0; i < argc; i++) {
- fprintf(todo_file, "[ ] %s\n", argv[i]);
+ if (argc < 1) {
+ char *line = NULL;
+ size_t line_buff_size = 0;
+ while (getline(&line, &line_buff_size, stdin) > 0) {
+ fprintf(todo_file, "[ ] %s", line);
+ }
+ } else {
+ for (int i = 0; i < argc; i++) {
+ fprintf(todo_file, "[ ] %s\n", argv[i]);
+ }
}
fclose(todo_file);
@@ -230,6 +235,7 @@ void todo_sort(void) {
pid_t pid = fork();
+ // The same as `LC_ALL=C sort < TODO > .TODO` from a shell.
if (pid == 0) {
open_todo_file("r");
int input_fd = fileno(todo_file);