summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 9a6712e0c1938f439be8a8325a310fa72c166ad8 (plain)
    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 using namespace std;
   18 #include <iostream>
   19 #include <stdlib.h>
   20 #include <string>
   21 #include "lib/config.h"
   22 #include "lib/note.h"
   23 #include "lib/note_list.h"
   24 #include "lib/path.h"
   25 
   26 int get_help() {
   27   string 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   "Arguments\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   cout << out << endl;
   43   return 0;
   44 }
   45 
   46 /**
   47  * Prints names of all notes
   48  */
   49 void list_notes( note_list list ) {
   50   // List notes
   51   vector<string> names = list.enum_names();
   52   cout << endl;
   53   for( int i = 0; i < names.size(); i++ ) {
   54     cout << "* " << names[i] << endl;
   55   }
   56   cout << endl;
   57 }
   58 
   59 /**
   60  * Searches all note contents for the specified text
   61  */
   62 int search_notes( note_list list, string term ) {
   63     vector<string> matches = list.find( true, term );
   64     for( int i = 0; i < matches.size(); i++ ) {
   65       cout << matches[i] << endl;
   66     }
   67     if( matches.size() == 0 ) {
   68       return 1;
   69     }
   70     return 0;
   71 }
   72 
   73 int cat_note( note_list list, string name ) {
   74     vector<string> body;
   75     if( list.cat_note( name, &body ) == 0 ) {
   76       // If list returns true, the note exists
   77       for( int i = 0; i < body.size(); i++ ) {
   78         cout << body[i] << endl;
   79       }
   80     } else {
   81       // List cat_note returned false. Note doesn't exist
   82       cout << "Note " << name << " could not be found." << endl;
   83       return 1;
   84     }
   85     return 0;
   86 }
   87 
   88 int main( int argc, char** argv ) {
   89   /**
   90    * Config variables definitions
   91    */
   92   // Default path to the note store
   93   string note_path = getenv( "HOME" );
   94   note_path += "/Documents/Notes";
   95 
   96   // Default note extension
   97   string note_ext = "mdown";
   98 
   99   // A little editor detection
  100   string editor;
  101   if( getenv( "EDITOR" ) ) {
  102     editor = getenv( "EDITOR" );
  103   } else {
  104     editor = "vi";
  105   }
  106 
  107   /**
  108    * Config file overrides
  109    */
  110   string conf_path = getenv( "HOME" );
  111   conf_path += "/.config/noteless.conf";
  112   path pconf( conf_path );
  113   config conf();
  114   if( pconf.exists() ) {
  115     config conf( conf_path.c_str() );
  116 
  117     // Override where notes are to be stored
  118     if( conf.isset( "path" ) ) {
  119       note_path = conf.get( "path" );
  120     }
  121 
  122     // Override the extension used by the notes
  123     if( conf.isset( "extension" ) ) {
  124       note_ext = conf.get( "extension" );
  125     }
  126 
  127     // Override editor settings
  128     if( conf.isset( "editor" ) ) {
  129       editor = conf.get( "editor" );
  130     }
  131   }
  132 
  133   if( argc == 1 ) {
  134     cout << "\nNo command specified. Printing help text.\n" << endl;
  135     return get_help();
  136   }
  137 
  138   // If the init command was passed, we want to init before checking if the
  139   // note path exists, otherwise the error will display and the store will
  140   // never be initialized.
  141   if( string( argv[1] ) == "init" ) {
  142     path p( note_path );
  143     return p.create();
  144   }
  145 
  146   // Check to make sure the note path exists
  147   path p( note_path );
  148   if( ! p.exists() ) {
  149     string out =
  150     "\nThe note store path '" + p.out() + "' does not exist.\n\n"
  151     "If this is your first time running noteless, please run "
  152     "'noteless init' to\n"
  153     "create the note store here.\n\n"
  154     "Otherwise, please verify the path variable in your configuration.";
  155     cout << out << endl;
  156     return 1;
  157   }
  158 
  159   // Create new list
  160   note_list list( note_path, note_ext );
  161 
  162   for( int i = 1; i < argc; i++ ) {
  163     string arg = argv[i];
  164     if( arg == "ls" || arg == "list" ) {
  165       list_notes( list );
  166     } else if( arg == "new" ) {
  167       string name = argv[i + 1];
  168       return list.create( editor, name );
  169     } else if( arg == "edit" ) {
  170       string name = argv[i + 1];
  171       return list.edit( editor, name );
  172     } else if( arg == "rm" ) {
  173       string name = argv[i + 1];
  174       return list.rm( name );
  175     } else if( arg == "find" ) {
  176       string term = argv[i + 1];
  177       return search_notes( list, term );
  178     } else if( arg == "cat" ) {
  179       string name = argv[i + 1];
  180       return cat_note( list, name );
  181     } else if( arg == "help" ) {
  182       return get_help();
  183     } else if( list.find_note_id( arg ) != -1 ) {
  184       // Try to open the note if it exists
  185       return list.edit( editor, arg );
  186     } else {
  187       cout << "Error: Unknown command or note name '" << arg << "'." << endl;
  188       return 1;
  189     }
  190   }
  191   return 0;
  192 }

Generated by cgit