From 8c70e74489507fc57d8d26e850d2fd117ab79fbd Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 3 Oct 2021 22:16:59 -0400 Subject: Added support for stdin for todo add This accepts one line as one task. Also added some comments. --- src/todo.c | 20 +++++++++++++------- 1 file 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); -- cgit v1.2.3