1 /**
2 * Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
3 *
4 * Noteless is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Noteless is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with noteless. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <dirent.h>
20 #include <string.h>
21 #include <time.h>
22 #include <errno.h>
23
24 #include "note_list.h"
25 #include "note.h"
26 #include "config.h"
27
28 /**
29 * Prints the standard help text
30 */
31 void get_help() {
32 char out[] = "\nNoteless provides a simple command line interface for note "
33 "taking.\n\n"
34 "What makes this different than simply using a command line text editor is\n"
35 "there is no need to type paths or file extensions. It handles all of the\n"
36 "file management without having to change directories. It can also perform\n"
37 "search operations without having to write a long command.\n\n"
38 "[1mArguments[0m\n"
39 " cat <note> Outputs the specified note's contents verbatim.\n"
40 " new <note> Creates the specified note and opens for editing.\n"
41 " edit <note> Opens the specified note for editing.\n"
42 " find <term> Performs a case-insensitive search of all notes for the\n"
43 " rm <note> Deletes the specified note.\n"
44 " given search term.\n"
45 " help Displays this help text\n"
46 " ls,list Lists all notes in note directory.\n";
47 printf("%s\n", out);
48 }
49
50
51 /**
52 * Prints names of all notes, sorted by last modified date
53 *
54 * @param list Note list whos names to enumerate
55 */
56 void list_notes(note_list* list) {
57 note* n;
58 int i = 0;
59 int count = 0;
60 note list_preload[256];
61 char modtime[256];
62
63 // Load note structs into an array
64 while((n = note_list_read(list)) != NULL) {
65 note_cpy(&list_preload[count], n);
66 count++;
67 }
68
69 // Sort the preloaded list
70 qsort(list_preload, count, sizeof(note), note_date_cmp);
71
72 // Output from oldest to newest
73 for(i = 0; i < count; i++) {
74 strftime(modtime, 256, "%F %T", localtime(&list_preload[i].mtime));
75 strip_extension(list_preload[i].name);
76 printf("%s %s\n", modtime, list_preload[i].name);
77 }
78 }
79
80
81 /**
82 * Sets up environment variables.
83 * Handles loading in configuration defaults before loading the config file
84 * values if one is present.
85 */
86 void config_setup(config_t* c) {
87 // Get the home dir
88 char home_path[strlen(getenv("HOME"))];
89 strcpy(home_path, getenv("HOME"));
90
91 // Set default note path
92 char tmp_note_path[strlen(home_path) + 17];
93 strcpy(tmp_note_path, home_path);
94 strcat(tmp_note_path, "/Documents/Notes");
95 config_set(c, "note_path", tmp_note_path);
96
97 // Set the default note extension
98 config_set(c, "extension", "mdown");
99
100 // Set editor default
101 char tmp_editor[128];
102 get_user_editor(tmp_editor);
103 config_set(c, "editor", tmp_editor);
104
105 // Assemble the path to the conf file
106 char conf_path[strlen(home_path) + 23];
107 strcpy(conf_path, home_path);
108 strcat(conf_path, "/.config/noteless.conf");
109 // Load the actual config file to override any defaults
110 config_load(c, conf_path);
111 }
112
113
114 int main(int argc, char* argv []) {
115 // Print helptext if no commands specified
116 if(argc == 1) {
117 printf("\nNo command specified. Printing help text.\n");
118 get_help();
119 return 1;
120 }
121
122 config_t c;
123 note_list list;
124
125 //
126 // Config setup
127 //
128 config_setup(&c);
129
130 note_list_new(
131 &list,
132 config_get(&c, "note_path"),
133 config_get(&c, "extension")
134 );
135
136 if(errno != 0) { return 1; }
137
138 if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
139 list_notes(&list);
140 } else if(strcmp(argv[1], "new") == 0) {
141 int create_status = note_list_create_note(&list, config_get(&c, "editor"), argv[2]);
142 // Notify user that a note already exists by the specified name
143 if(create_status == 1) {
144 printf("A note by the name %s already exists.\n", argv[2]);
145 return create_status;
146 }
147 } else if(strcmp(argv[1], "edit") == 0) {
148 return note_list_edit(&list, config_get(&c, "editor"), argv[2]);
149 } else if(strcmp(argv[1], "rm") == 0) {
150 int rm_status = note_list_rm_note(&list, argv[2]);
151 if(rm_status == 0) {
152 printf("Note matching \"%s\" deleted successfully\n", argv[2]);
153 } else if(rm_status == 1) {
154 printf("Error: There are no notes matching \"%s\".\n", argv[2]);
155 } else {
156 printf("Error: Failed deleting note matching \"%s\".\n", argv[2]);
157 }
158 return rm_status;
159 } else if(strcmp(argv[1], "find") == 0) {
160 return note_list_search(&list, argv[2], 1);
161 } else if(strcmp(argv[1], "cat") == 0) {
162 return note_list_cat_note(&list, argv[2]);
163 } else if(strcmp(argv[1], "help") == 0) {
164 get_help();
165 } else {
166 int err = note_list_edit(&list, config_get(&c, "editor"), argv[1]);
167 if(err == 1) {
168 printf("%s is also not a valid command.\n", argv[1]);
169 return 1;
170 }
171 }
172
173 // Clean up
174 note_list_free(&list);
175 config_free(&c);
176
177 return 0;
178 }
|