summaryrefslogtreecommitdiff
path: root/src/todo.c
blob: e56f8f527de45f601b2b2a6171a694df58f20af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* This file is a part of todo
 *
 * Copyright (C) 2021  Robby Zambito
 *
 * todo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * todo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#define STREQ(a, b) (strcmp(a, b) == 0)

static FILE *todo_file;

static void open_todo_file(char *mode) {
  errno = 0;
  todo_file = fopen("TODO", mode);
  if (todo_file == NULL) {
    perror("Could not open the TODO file");
    exit(EXIT_FAILURE);
  }
}

/* Assumes the file is open and writeable.
 * Assumes the line already has a newline if needed.
 */
static void write_line_to_file(char *line, ssize_t line_len, FILE *file,
                               size_t ignore) {
  fprintf(file, "%s", line);
}

static void process_todo_file(void (*mapper_func)(char *),
                              size_t *mapped_indices,
                              size_t mapped_indices_length,
                              void (*process_func)(char *line, ssize_t line_len,
                                                   FILE *output, size_t index),
                              bool is_write) {
  char *line = NULL;
  size_t line_buff_size = 0;
  ssize_t line_len = 0;

  FILE *temp_file;

  open_todo_file("r");

  // Instead of writing directly to the TODO file, we write to another file and
  // clobber it at the end.
  if (is_write) {
    errno = 0;
    temp_file = fopen(".TODO", "w");
    if (temp_file == NULL) {
      perror("Could not open temp TODO file");
      exit(EXIT_FAILURE);
    }
  }

  // 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) {
      if (j < mapped_indices_length && i == mapped_indices[j]) {
        j++;
        mapper_func(line);
      }
      process_func(line, line_len, temp_file, i);
    }
  }
  free(line);

  fclose(todo_file);
  if (is_write) {
    fclose(temp_file);

    // Clobber old TODO with new .TODO
    rename(".TODO", "TODO");
  }
}

static void print_strikethrough(char *text) {
  static const char *strike_overlay = "\u0336";
  if (text != NULL) {
    while (*text != '\n') {
      printf("%s%c", strike_overlay, *text);
      text++;
    }
    puts("");
  }
}

static void print_list(char *line, ssize_t line_len, FILE *ignored,
                       size_t index) {
  static const char *bold_text = "\e[1m";
  static const char *regular_text = "\e[0m";

  printf("%s%zu%s ", bold_text, index, regular_text);

  if (line[1] == ' ') {
    printf("%s", line + 4);
  } else if (line[1] == '*') {
    print_strikethrough(line + 4);
  }
}

void todo_list(void) { process_todo_file(NULL, NULL, 0, print_list, false); }

void todo_add(int argc, char **argv) {
  open_todo_file("a");

  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);
}

static int int_compare(const void *a, const void *b) {
  int int_a = *((int *)a);
  int int_b = *((int *)b);

  if (int_a == int_b)
    return 0;
  else if (int_a < int_b)
    return -1;
  else
    return 1;
}

static size_t *parse_indices(int argc, char **argv) {
  size_t *res = calloc(sizeof(size_t), argc);

  for (size_t i = 0; i < argc; i++) {
    errno = 0;
    sscanf(argv[i], "%zu", &(res[i]));
    if (errno != 0 || res[i] < 1) {
      perror("Could not parse index");
      exit(EXIT_FAILURE);
    }
  }

  qsort(res, argc, sizeof(size_t), int_compare);
  return res;
}

static void blank(char *task) { task[0] = '\0'; }

void todo_rm(int argc, char **argv) {
  if (argc < 1) {
    fprintf(stderr, "todo rm takes at least 1 argument\n");
    exit(EXIT_FAILURE);
  }

  size_t *remove_indices = parse_indices(argc, argv);
  process_todo_file(blank, remove_indices, argc, write_line_to_file, true);
  free(remove_indices);
}

static void toggle_done(char *task) {
  if (task[1] == ' ') {
    task[1] = '*';
  } else if (task[1] == '*') {
    task[1] = ' ';
  }
}

void todo_done(int argc, char **argv) {
  if (argc < 1) {
    fprintf(stderr, "todo done takes at least 1 argument\n");
    exit(EXIT_FAILURE);
  }

  size_t *done_indices = parse_indices(argc, argv);
  process_todo_file(toggle_done, done_indices, argc, write_line_to_file, true);
  free(done_indices);
}

static bool todo_raw_is_done = false;
static bool todo_raw_is_todo = false;

static void print_raw(char *line, ssize_t line_len, FILE *ignored,
                      size_t index) {
  if ((todo_raw_is_done && line[1] == '*') ||
      (todo_raw_is_todo && line[1] == ' ')) {
    printf("%s", line);
  }
}

void todo_raw(int argc, char **argv) {
  if (argc > 1) {
    fprintf(stderr, "todo raw takes only 1 argument\n");
    exit(EXIT_FAILURE);
  } else if (argc < 1) {
    fprintf(stderr, "todo raw takes 1 argument (done/todo)\n");
    exit(EXIT_FAILURE);
  }

  todo_raw_is_done = STREQ(argv[0], "done");
  todo_raw_is_todo = STREQ(argv[0], "todo");

  if (!todo_raw_is_done && !todo_raw_is_todo) {
    fprintf(stderr, "todo raw takes 1 argument (done/todo)\n");
    exit(EXIT_FAILURE);
  }

  process_todo_file(NULL, NULL, 0, print_raw, false);
}

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);

    errno = 0;
    FILE *temp_file = fopen(".TODO", "w");
    if (temp_file == NULL) {
      perror("Could not open temp TODO file");
      exit(EXIT_FAILURE);
    }

    int output_fd = fileno(temp_file);

    dup2(input_fd, STDIN_FILENO);
    dup2(output_fd, STDOUT_FILENO);

    setenv("LC_ALL", "C", true);

    char *args[] = {"sort", NULL};
    execvp("sort", args);
  }

  int exit_status = 0;
  wait(&exit_status);
  if (exit_status != 0) {
    exit(EXIT_FAILURE);
  }

  rename(".TODO", "TODO");
}

void todo_help(void) {
  puts("Usage: todo [COMMAND] [ARGUMENTS]\n"
       "Todo is a super fast and simple tasks organizer written in C.\n"
       "Example: todo list\n"
       "Available commands:\n"
       "    - add [TASK/s]\n"
       "        adds new task/s\n"
       "        Example: todo add \"buy carrots\"\n"
       "    - list\n"
       "        lists all tasks\n"
       "        Example: todo list\n"
       "    - done [INDEX]\n"
       "        marks task as done\n"
       "        Example: todo done 2 3 (marks second and third tasks as "
       "completed)\n"
       "    - rm [INDEX]\n"
       "        removes a task\n"
       "        Example: todo rm 4\n"
       "    - sort\n"
       "        sorts completed and uncompleted tasks\n"
       "        Example: todo sort\n"
       "    - raw [todo/done]\n"
       "        prints nothing but done/incompleted tasks in plain text, "
       "useful for scripting\n"
       "        Example: todo raw done\n");
}