summaryrefslogtreecommitdiff
path: root/src/todo.c
blob: 8d6334fe5949d04cd4f2b055359fda7bd34600b0 (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
/* 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 <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static FILE *todo_file;

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

static void apply_func_to_line(void (*func)(char *), size_t index) {
  char *line = NULL;
  size_t line_len = 0;

  errno = 0;
  todo_file = fopen("TODO", "r");
  if (todo_file == NULL) {
    perror("Could not open the TODO file");
    exit(EXIT_FAILURE);
  }

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

  for (size_t i = 1; getline(&line, &line_len, todo_file) >= 0; i++) {
    if (i == index) {
      func(line);
    }
    fprintf(temp_file, "%s", line);
    free(line);
  }

  fclose(todo_file);
  fclose(temp_file);

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

void todo_list(void) {
  static const char *bold_text = "\e[1m";
  static const char *regular_text = "\e[0m";

  char *line = NULL;
  size_t line_len = 0;

  errno = 0;
  todo_file = fopen("TODO", "r");
  if (todo_file == NULL) {
    if (errno == ENOENT) {
      // If the file doesn't exist, the list is simply empty
      exit(EXIT_SUCCESS);
    } else {
      perror("Could not open TODO file");
      exit(EXIT_FAILURE);
    }
  }

  for (size_t i = 0; getline(&line, &line_len, todo_file) >= 0; i++) {
    // Remove trailing newline by setting it to NULL char.
    line[strcspn(line, "\n")] = '\0';

    if (line_len > 5) {
      // Print the index in bold
      printf("%s%zu%s ", bold_text, i + 1, regular_text);

      // Print the task with a strikethrough if completed
      if (line[1] == ' ') {
        puts(line + 4);
      } else if (line[1] == '*') {
        puts_strikethrough(line + 4);
      }
    }

    free(line);
  }

  fclose(todo_file);
}

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

  errno = 0;
  todo_file = fopen("TODO", "a");
  if (todo_file == NULL) {
    perror("Could not open the TODO file");
    exit(EXIT_FAILURE);
  }

  for (int i = 0; i < argc; i++) {
    fprintf(todo_file, "[ ] %s\n", argv[i]);
  }

  fclose(todo_file);
}

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_index = -1;

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

  apply_func_to_line(blank, remove_index);
}

static void done(char *task) { 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_index = -1;

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

  apply_func_to_line(done, done_index);
}

void todo_raw(int argc, char **argv) {
  /*open_todo_file();*/
  puts("raw");
  fclose(todo_file);
}

void todo_sort(void) {
  /*open_todo_file();*/
  puts("sort");
  fclose(todo_file);
}

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