summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: a2c5bb7ad91c7b1136504c06a5bebbae4c9fc8c9 (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 
   25 void get_help() {
   26   string out = "Noteless provides a simple command line interface for note"
   27   "taking.\n\n"
   28   "What makes this different than simply using a command line text editor is "
   29   "there is no need to type paths or file extensions. It handles all of the "
   30   "file management without having to changedirectories.\n\n"
   31   "Arguments\n"
   32   " help         Displays this help text\n"
   33   " ls,list      Lists all notes in note directory.\n"
   34   " find <term>  Performs a case-insensitive search of all notes for the given"
   35   "                       search term.\n";
   36   cout << out << endl;
   37 }
   38 
   39 /**
   40  * Prints names of all notes
   41  */
   42 void list_notes( note_list list ) {
   43   // List notes
   44   vector<string> names = list.enum_names();
   45   for( int i = 0; i < names.size(); i++ ) {
   46     cout << names[i] << endl;
   47   }
   48 }
   49 
   50 /**
   51  * Searches all note contents for the specified text
   52  */
   53 void search_notes( note_list list, string term ) {
   54     vector<string> matches = list.find( true, term );
   55     for( int i = 0; i < matches.size(); i++ ) {
   56       cout << matches[i] << endl;
   57     }
   58 }
   59 
   60 /**
   61  * Helper function to determine which editor to use. Order or priority is
   62  * config file, environmental variable, default.
   63  *
   64  * @return string Path to the editor binary
   65  */
   66 string get_editor( config c ) {
   67   // Which editor to use
   68   if( c.isset( "editor" ) ) {
   69     // Check the config file
   70     return c.get( "editor" );
   71   } else if( getenv( "EDITOR" ) ) {
   72     // Get the enviro variable
   73     return getenv( "EDITOR" );
   74   }
   75   // Default to vim if nothing else works
   76   return "vim";
   77 }
   78 
   79 int main( int argc, char** argv ) {
   80   // Load the config file
   81   string conf_path = getenv( "HOME" );
   82   conf_path += "/.config/noteless.conf";
   83   config conf( conf_path.c_str() );
   84 
   85   /**
   86    * Config variables definitions
   87    */
   88   string note_path;
   89   string note_ext;
   90   string editor;
   91 
   92   // Path where notes are to be stored
   93   if( conf.isset( "path" ) ) {
   94     note_path = conf.get( "path" );
   95   } else {
   96     cout << "Error: Config variable \"path\" is unset." << endl;
   97     return 1;
   98   }
   99 
  100   // Set the extension used by the notes
  101   if( conf.isset( "extension" ) ) {
  102     note_ext = conf.get( "extension" );
  103   } else {
  104     cout << "Error: Config variable \"extension\" is unset." << endl;
  105     return 1;
  106   }
  107 
  108   // Set editor
  109   editor = get_editor( conf );
  110 
  111   // Create new list
  112   note_list list( note_path, note_ext );
  113 
  114   for( int i = 0; i < argc; i++ ) {
  115     string arg = argv[i];
  116     if( arg == "ls" || arg == "list" ) {
  117       list_notes( list );
  118     } else if( arg == "edit" ) {
  119       string name = argv[i + 1];
  120       list.edit( editor, name );
  121     } else if( arg == "find" ) {
  122       string term = argv[i + 1];
  123       search_notes( list, term );
  124     } else if( arg == "help" ) {
  125       get_help();
  126     }
  127   }
  128   return 0;
  129 }

Generated by cgit