diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 283 |
1 files changed, 150 insertions, 133 deletions
@@ -14,16 +14,17 @@ * 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 <stdio.h> #include <string.h> +#include "common.h" #include "config.h" -#include "note.h" -#include "note_list.h" -#include "path.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 " + char 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" @@ -38,154 +39,170 @@ int get_help() { " given search term.\n" " help Displays this help text\n" " ls,list Lists all notes in note directory.\n"; - cout << out << endl; + printf("%s\n", out); 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 ) { +// 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 + * Config variables */ - // Default path to the note store - string note_path = getenv( "HOME" ); - note_path += "/Documents/Notes"; + char home_path[strlen(getenv("HOME"))]; + strcpy(home_path, getenv("HOME")); - // Default note extension - string note_ext = "mdown"; + /* Default path to the note store */ + char* note_path = NULL; - // A little editor detection - string editor; - if( getenv( "EDITOR" ) ) { - editor = getenv( "EDITOR" ); - } else { - editor = "vi"; - } + /* Default note extension */ + char* note_ext = "mdown"; + + /* A little editor detection */ + char* editor = NULL; /** * 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" ); - } - } + char conf_path[strlen(home_path) + 23]; + strcpy(conf_path, home_path); + strcat(conf_path, "/.config/noteless.conf"); - if( argc == 1 ) { - cout << "\nNo command specified. Printing help text.\n" << endl; - return get_help(); - } + config_t c; + config_new(&c, conf_path); + + // Override where notes are to be stored + // The local version of note path, in case it isn't set in the config + char tmp_note_path[strlen(home_path) + 17]; - // 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(); + if(config_isset(&c, "path") == 1) { + note_path = config_get(&c, "path"); + } else { + strcpy(tmp_note_path, home_path); + strcat(tmp_note_path, "/Documents/Notes"); + note_path = tmp_note_path; } - // 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; + // Override the extension used by the notes + if(config_isset(&c, "extension")) { + note_ext = config_get(&c, "extension"); } - // 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; - } + // Override editor settings + char tmp_editor[128]; + if(config_isset(&c, "editor")) { + editor = config_get(&c, "editor"); + } else { + get_user_editor(tmp_editor); + editor = tmp_editor; } + + + // Verbose variable outputs + printf("Note Path: %s\n", note_path); + printf("Note Ext: %s\n", note_ext); + printf("Conf Path: %s\n", conf_path); + printf("Editor: %s\n", editor); + + config_free(&c); + + // 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; } |