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
22 #include "note_list.h"
23 #include "note.h"
24 #include "config.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* list) {
55 note* n;
56
57 while((n = note_list_read(list)) != NULL) {
58 printf("* %s\n", n->name);
59 }
60 }
61
62
63 /**
64 * Sets up environment variables.
65 * Handles loading in configuration defaults before loading the config file
66 * values if one is present.
67 */
68 void config_setup(config_t* c) {
69 // Get the home dir
70 char home_path[strlen(getenv("HOME"))];
71 strcpy(home_path, getenv("HOME"));
72
73 // Set default note path
74 char tmp_note_path[strlen(home_path) + 17];
75 strcpy(tmp_note_path, home_path);
76 strcat(tmp_note_path, "/Documents/Notes");
77 config_set(c, "note_path", tmp_note_path);
78
79 // Set the default note extension
80 config_set(c, "extension", "mdown");
81
82 // Set editor default
83 char tmp_editor[128];
84 get_user_editor(tmp_editor);
85 config_set(c, "editor", tmp_editor);
86
87 // Assemble the path to the conf file
88 char conf_path[strlen(home_path) + 23];
89 strcpy(conf_path, home_path);
90 strcat(conf_path, "/.config/noteless.conf");
91 // Load the actual config file to override any defaults
92 config_load(c, conf_path);
93 }
94
95
96 int main(int argc, char* argv []) {
97 // Print helptext if no commands specified
98 if(argc == 1) {
99 printf("\nNo command specified. Printing help text.\n");
100 get_help();
101 return 1;
102 }
103
104 config_t c;
105 note_list list;
106
107 //
108 // Config setup
109 //
110 config_setup(&c);
111
112 note_list_new(
113 &list,
114 config_get(&c, "note_path"),
115 config_get(&c, "extension")
116 );
117
118 if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
119 list_notes(&list);
120 } else if(strcmp(argv[1], "new") == 0) {
121 int create_status = note_list_create_note(&list, config_get(&c, "editor"), argv[2]);
122 // Notify user that a note already exists by the specified name
123 if(create_status == 1) {
124 printf("A note by the name %s already exists.\n", argv[2]);
125 return create_status;
126 }
127 } else if(strcmp(argv[1], "edit") == 0) {
128 return note_list_edit(&list, config_get(&c, "editor"), argv[2]);
129 } else if(strcmp(argv[1], "rm") == 0) {
130 int rm_status = note_list_rm_note(&list, argv[2]);
131 if(rm_status == 0) {
132 printf("Note matching \"%s\" deleted successfully\n", argv[2]);
133 } else if(rm_status == 1) {
134 printf("Error: There are no notes matching \"%s\".\n", argv[2]);
135 } else {
136 printf("Error: Failed deleting note matching \"%s\".\n", argv[2]);
137 }
138 return rm_status;
139 } else if(strcmp(argv[1], "find") == 0) {
140 return note_list_search(&list, argv[2], 1);
141 } else if(strcmp(argv[1], "cat") == 0) {
142 return note_list_cat_note(&list, argv[2]);
143 } else if(strcmp(argv[1], "help") == 0) {
144 get_help();
145 } else {
146 int err = note_list_edit(&list, config_get(&c, "editor"), argv[1]);
147 if(err == 1) {
148 printf("%s is also not a valid command.\n", argv[1]);
149 return 1;
150 }
151 }
152
153 // Clean up
154 note_list_free(&list);
155 config_free(&c);
156
157 return 0;
158 }
|