diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d023ac1 --- /dev/null +++ b/src/main.c @@ -0,0 +1,191 @@ +/** + * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net> + * + * Noteless 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. + * + * Noteless 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 noteless. If not, see <http://www.gnu.org/licenses/>. + */ +using namespace std; +#include <stdlib.h> +#include <string.h> +#include "config.h" +#include "note.h" +#include "note_list.h" +#include "path.h" + +int get_help() { + string out = "\nNoteless provides a simple command line interface for note " + "taking.\n\n" + "What makes this different than simply using a command line text editor is\n" + "there is no need to type paths or file extensions. It handles all of the\n" + "file management without having to change directories. It can also perform\n" + "search operations without having to write a long command.\n\n" + "[1mArguments[0m\n" + " cat <note> Outputs the specified note's contents verbatim.\n" + " new <note> Creates the specified note and opens for editing.\n" + " edit <note> Opens the specified note for editing.\n" + " find <term> Performs a case-insensitive search of all notes for the\n" + " rm <note> Deletes the specified note.\n" + " given search term.\n" + " help Displays this help text\n" + " ls,list Lists all notes in note directory.\n"; + cout << out << endl; + return 0; +} + +/** + * Prints names of all notes + */ +void list_notes( note_list list ) { + // List notes + vector<string> names = list.enum_names(); + cout << endl; + for( int i = 0; i < names.size(); i++ ) { + cout << "* " << names[i] << endl; + } + cout << endl; +} + +/** + * Searches all note contents for the specified text + */ +int search_notes( note_list list, string term ) { + vector<string> matches = list.find( true, term ); + for( int i = 0; i < matches.size(); i++ ) { + cout << matches[i] << endl; + } + if( matches.size() == 0 ) { + return 1; + } + return 0; +} + +int cat_note( note_list list, string name ) { + vector<string> body; + if( list.cat_note( name, &body ) == 0 ) { + // If list returns true, the note exists + for( int i = 0; i < body.size(); i++ ) { + cout << body[i] << endl; + } + } else { + // List cat_note returned false. Note doesn't exist + cout << "Note " << name << " could not be found." << endl; + return 1; + } + return 0; +} + +int main( int argc, char** argv ) { + /** + * Config variables definitions + */ + // Default path to the note store + string note_path = getenv( "HOME" ); + note_path += "/Documents/Notes"; + + // Default note extension + string note_ext = "mdown"; + + // A little editor detection + string editor; + if( getenv( "EDITOR" ) ) { + editor = getenv( "EDITOR" ); + } else { + editor = "vi"; + } + + /** + * Config file overrides + */ + string conf_path = getenv( "HOME" ); + conf_path += "/.config/noteless.conf"; + path pconf( conf_path ); + config conf(); + if( pconf.exists() ) { + config conf( conf_path.c_str() ); + + // Override where notes are to be stored + if( conf.isset( "path" ) ) { + note_path = conf.get( "path" ); + } + + // Override the extension used by the notes + if( conf.isset( "extension" ) ) { + note_ext = conf.get( "extension" ); + } + + // Override editor settings + if( conf.isset( "editor" ) ) { + editor = conf.get( "editor" ); + } + } + + if( argc == 1 ) { + cout << "\nNo command specified. Printing help text.\n" << endl; + return get_help(); + } + + // If the init command was passed, we want to init before checking if the + // note path exists, otherwise the error will display and the store will + // never be initialized. + if( string( argv[1] ) == "init" ) { + path p( note_path ); + return p.create(); + } + + // Check to make sure the note path exists + path p( note_path ); + if( ! p.exists() ) { + string out = + "\nThe note store path '" + p.out() + "' does not exist.\n\n" + "If this is your first time running noteless, please run " + "'[1mnoteless init[0m' to\n" + "create the note store here.\n\n" + "Otherwise, please verify the path variable in your configuration."; + cout << out << endl; + return 1; + } + + // Create new list + note_list list( note_path, note_ext ); + + for( int i = 1; i < argc; i++ ) { + string arg = argv[i]; + if( arg == "ls" || arg == "list" ) { + list_notes( list ); + } else if( arg == "new" ) { + string name = argv[i + 1]; + return list.create( editor, name ); + } else if( arg == "edit" ) { + string name = argv[i + 1]; + return list.edit( editor, name ); + } else if( arg == "rm" ) { + string name = argv[i + 1]; + return list.rm( name ); + } else if( arg == "find" ) { + string term = argv[i + 1]; + return search_notes( list, term ); + } else if( arg == "cat" ) { + string name = argv[i + 1]; + return cat_note( list, name ); + } else if( arg == "help" ) { + return get_help(); + } else if( list.find_note_id( arg ) != -1 ) { + // Try to open the note if it exists + return list.edit( editor, arg ); + } else { + cout << "Error: Unknown command or note name '" << arg << "'." << endl; + return 1; + } + } + return 0; +} |