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