summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9ee10f922e0f12195bff1ed18efc73d2b4ad0b56 (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 #include <stdlib.h>
   18 #include <stdio.h>
   19 #include <string.h>
   20 #include "common.h"
   21 #include "config.h"
   22 #include "note_list.h"
   23 // #include "note.h"
   24 // #include "path.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   "Arguments\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_t* list) {
   55   // List notes
   56   for(int i = 0; i < list->count; i++) {
   57     printf("* %s\n", list->names[i]);
   58   }
   59 }
   60 
   61 
   62 /**
   63  * Searches all note contents for the specified text
   64  */
   65 // int search_notes( note_list list, string term ) {
   66 //     vector<string> matches = list.find( true, term );
   67 //     for( int i = 0; i < matches.size(); i++ ) {
   68 //       cout << matches[i] << endl;
   69 //     }
   70 //     if( matches.size() == 0 ) {
   71 //       return 1;
   72 //     }
   73 //     return 0;
   74 // }
   75 
   76 
   77 int main(int argc, char* argv[]) {
   78   // Print helptext if no commands specified
   79   if(argc == 1) {
   80     printf("\nNo command specified. Printing help text.\n");
   81     get_help();
   82     return 1;
   83   }
   84 
   85   /**
   86    * Config variables
   87    */
   88   char home_path[strlen(getenv("HOME"))];
   89   strcpy(home_path, getenv("HOME"));
   90 
   91   /* Default path to the note store */
   92   char* note_path = NULL;
   93 
   94   /* Default note extension */
   95   char* note_ext = "mdown";
   96 
   97   /* A little editor detection */
   98   char* editor = NULL;
   99 
  100   /**
  101    * Config file overrides
  102    */
  103   char conf_path[strlen(home_path) + 23];
  104   strcpy(conf_path, home_path);
  105   strcat(conf_path, "/.config/noteless.conf");
  106 
  107   config_t c;
  108   int config_status = config_new(&c, conf_path);
  109   if(config_status == -1) { 
  110     printf("Config file could not be found.\n");
  111   }
  112 
  113   // Override where notes are to be stored
  114   // The local version of note path, in case it isn't set in the config
  115   char tmp_note_path[strlen(home_path) + 17];
  116 
  117   if(config_isset(&c, "path") == 1) {
  118     note_path = config_get(&c, "path");
  119   } else {
  120     strcpy(tmp_note_path, home_path);
  121     strcat(tmp_note_path, "/Documents/Notes");
  122     note_path = tmp_note_path;
  123   }
  124 
  125   // Override the extension used by the notes
  126   if(config_isset(&c, "extension")) {
  127     note_ext = config_get(&c, "extension");
  128   }
  129 
  130   // Override editor settings
  131   char tmp_editor[128];
  132   if(config_isset(&c, "editor")) {
  133     editor = config_get(&c, "editor");
  134   } else {
  135     get_user_editor(tmp_editor);
  136     editor = tmp_editor;
  137   }
  138 
  139   // // If the init command was passed, we want to init before checking if the
  140   // // note path exists, otherwise the error will display and the store will
  141   // // never be initialized.
  142   // if( string( argv[1] ) == "init" ) {
  143   //   path p( note_path );
  144   //   return p.create();
  145   // }
  146 
  147   // // Check to make sure the note path exists
  148   // path p( note_path );
  149   // if( ! p.exists() ) {
  150   //   string out =
  151   //   "\nThe note store path '" + p.out() + "' does not exist.\n\n"
  152   //   "If this is your first time running noteless, please run "
  153   //   "'noteless init' to\n"
  154   //   "create the note store here.\n\n"
  155   //   "Otherwise, please verify the path variable in your configuration.";
  156   //   cout << out << endl;
  157   //   return 1;
  158   // }
  159 
  160   note_list_t list;
  161 
  162   note_list_new(&list, note_path, note_ext);
  163 
  164   if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
  165     list_notes(&list);
  166   } else if(strcmp(argv[1], "new") == 0) {
  167     int create_status = note_list_create_note(&list, editor, argv[2]);
  168     // Notify user that a note already exists by the specified name
  169     if(create_status == 1) {
  170       printf("A note by the name %s already exists.\n", argv[2]);
  171       return create_status;
  172     }
  173   } else if(strcmp(argv[1], "edit") == 0) {
  174     return note_list_edit(&list, editor, argv[2]);
  175   } else if(strcmp(argv[1], "rm") == 0) {
  176     int rm_status = note_list_rm_note(&list, argv[2]);
  177     if(rm_status == 0) {
  178       printf("Note matching \"%s\" deleted successfully\n", argv[2]);
  179     } else if(rm_status == 1) {
  180       printf("Error: There are no notes matching \"%s\".\n", argv[2]);
  181     } else {
  182       printf("Error: Failed deleting note matching \"%s\".\n", argv[2]);
  183     }
  184     return rm_status;
  185   } else if(strcmp(argv[1], "find") == 0) {
  186     return note_list_search(&list, argv[2], 1);
  187   } else if(strcmp(argv[1], "cat") == 0) {
  188     return note_list_cat_note(&list, argv[2]);
  189   } else if(strcmp(argv[1], "help") == 0) {
  190     get_help();
  191   } else if(note_list_get_note_id(&list, argv[1]) != -1 ) {
  192     // Try to open the note if it exists
  193     return note_list_edit(&list, editor, argv[1]);
  194   } else {
  195     printf("Error: Unknown command or note name '%s'.\n", argv[1]);
  196     return 1;
  197   }
  198 
  199   // DANGER WILL ROBINSON!!!
  200 
  201   // Clean up
  202   note_list_free(&list);
  203   config_free(&c);
  204 
  205   return 0;
  206 }

Generated by cgit