1 /**
2 * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net>
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 <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include "common.h"
21 #include "config.h"
22 #include "note_list.h"
23 // #include "note.h"
24 // #include "path.h"
25
26 /**
27 * Prints the standard help text
28 */
29 void get_help() {
30 char out[] = "\nNoteless provides a simple command line interface for note "
31 "taking.\n\n"
32 "What makes this different than simply using a command line text editor is\n"
33 "there is no need to type paths or file extensions. It handles all of the\n"
34 "file management without having to change directories. It can also perform\n"
35 "search operations without having to write a long command.\n\n"
36 "[1mArguments[0m\n"
37 " cat <note> Outputs the specified note's contents verbatim.\n"
38 " new <note> Creates the specified note and opens for editing.\n"
39 " edit <note> Opens the specified note for editing.\n"
40 " find <term> Performs a case-insensitive search of all notes for the\n"
41 " rm <note> Deletes the specified note.\n"
42 " given search term.\n"
43 " help Displays this help text\n"
44 " ls,list Lists all notes in note directory.\n";
45 printf("%s\n", out);
46 }
47
48
49 /**
50 * Prints names of all notes
51 *
52 * @param list Note list whos names to enumerate
53 */
54 void list_notes(note_list_t* list) {
55 // List notes
56 for(int i = 0; i < list->count; i++) {
57 char name[128];
58 strcpy(name, list->names[i]);
59 strip_extension(name);
60 printf("* %s\n", name);
61 }
62 }
63
64
65 /**
66 * Searches all note contents for the specified text
67 */
68 // int search_notes( note_list list, string term ) {
69 // vector<string> matches = list.find( true, term );
70 // for( int i = 0; i < matches.size(); i++ ) {
71 // cout << matches[i] << endl;
72 // }
73 // if( matches.size() == 0 ) {
74 // return 1;
75 // }
76 // return 0;
77 // }
78
79
80 int main(int argc, char* argv[]) {
81 // Print helptext if no commands specified
82 if(argc == 1) {
83 printf("\nNo command specified. Printing help text.\n");
84 get_help();
85 return 1;
86 }
87
88 /**
89 * Config variables
90 */
91 char home_path[strlen(getenv("HOME"))];
92 strcpy(home_path, getenv("HOME"));
93
94 /* Default path to the note store */
95 char* note_path = NULL;
96
97 /* Default note extension */
98 char* note_ext = "mdown";
99
100 /* A little editor detection */
101 char* editor = NULL;
102
103 /**
104 * Config file overrides
105 */
106 char conf_path[strlen(home_path) + 23];
107 strcpy(conf_path, home_path);
108 strcat(conf_path, "/.config/noteless.conf");
109
110 config_t c;
111 int config_status = config_new(&c, conf_path);
112 if(config_status == -1) {
113 printf("Config file could not be found.\n");
114 }
115
116 // Override where notes are to be stored
117 // The local version of note path, in case it isn't set in the config
118 char tmp_note_path[strlen(home_path) + 17];
119
120 if(config_isset(&c, "path") == 1) {
121 note_path = config_get(&c, "path");
122 } else {
123 strcpy(tmp_note_path, home_path);
124 strcat(tmp_note_path, "/Documents/Notes");
125 note_path = tmp_note_path;
126 }
127
128 // Override the extension used by the notes
129 if(config_isset(&c, "extension")) {
130 note_ext = config_get(&c, "extension");
131 }
132
133 // Override editor settings
134 char tmp_editor[128];
135 if(config_isset(&c, "editor")) {
136 editor = config_get(&c, "editor");
137 } else {
138 get_user_editor(tmp_editor);
139 editor = tmp_editor;
140 }
141
142 // // If the init command was passed, we want to init before checking if the
143 // // note path exists, otherwise the error will display and the store will
144 // // never be initialized.
145 // if( string( argv[1] ) == "init" ) {
146 // path p( note_path );
147 // return p.create();
148 // }
149
150 // // Check to make sure the note path exists
151 // path p( note_path );
152 // if( ! p.exists() ) {
153 // string out =
154 // "\nThe note store path '" + p.out() + "' does not exist.\n\n"
155 // "If this is your first time running noteless, please run "
156 // "'[1mnoteless init[0m' to\n"
157 // "create the note store here.\n\n"
158 // "Otherwise, please verify the path variable in your configuration.";
159 // cout << out << endl;
160 // return 1;
161 // }
162
163 note_list_t list;
164
165 note_list_new(&list, note_path, note_ext);
166
167 if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
168 list_notes(&list);
169 } else if(strcmp(argv[1], "new") == 0) {
170 int create_status = note_list_create_note(&list, editor, argv[2]);
171 // Notify user that a note already exists by the specified name
172 if(create_status == 1) {
173 printf("A note by the name %s already exists.\n", argv[2]);
174 return create_status;
175 }
176 } else if(strcmp(argv[1], "edit") == 0) {
177 return note_list_edit(&list, editor, argv[2]);
178 } else if(strcmp(argv[1], "rm") == 0) {
179 int rm_status = note_list_rm_note(&list, argv[2]);
180 if(rm_status == 0) {
181 printf("Note matching \"%s\" deleted successfully\n", argv[2]);
182 } else if(rm_status == 1) {
183 printf("Error: There are no notes matching \"%s\".\n", argv[2]);
184 } else {
185 printf("Error: Failed deleting note matching \"%s\".\n", argv[2]);
186 }
187 return rm_status;
188 } else if(strcmp(argv[1], "find") == 0) {
189 return note_list_search(&list, argv[2], 1);
190 } else if(strcmp(argv[1], "cat") == 0) {
191 return note_list_cat_note(&list, argv[2]);
192 } else if(strcmp(argv[1], "help") == 0) {
193 get_help();
194 } else if(note_list_get_note_id(&list, argv[1]) != -1 ) {
195 // Try to open the note if it exists
196 return note_list_edit(&list, editor, argv[1]);
197 } else {
198 printf("Error: Unknown command or note name '%s'.\n", argv[1]);
199 return 1;
200 }
201
202 // DANGER WILL ROBINSON!!!
203
204 // Clean up
205 note_list_free(&list);
206 config_free(&c);
207
208 return 0;
209 }
|