summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 049de6d6a3a8d552171618b407e81f09c5bacf9f (plain)
    1 /**
    2  * Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
    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 <stdio.h>
   18 #include <stdlib.h>
   19 #include <dirent.h>
   20 #include <string.h>
   21 #include <time.h>
   22 
   23 #include "note_list.h"
   24 #include "note.h"
   25 #include "config.h"
   26 
   27 /**
   28  * Prints the standard help text
   29  */
   30 void get_help() {
   31   char out[] = "\nNoteless provides a simple command line interface for note "
   32   "taking.\n\n"
   33   "What makes this different than simply using a command line text editor is\n"
   34   "there is no need to type paths or file extensions. It handles all of the\n"
   35   "file management without having to change directories. It can also perform\n"
   36   "search operations without having to write a long command.\n\n"
   37   "Arguments\n"
   38   " cat <note>   Outputs the specified note's contents verbatim.\n"
   39   " new <note>   Creates the specified note and opens for editing.\n"
   40   " edit <note>  Opens the specified note for editing.\n"
   41   " find <term>  Performs a case-insensitive search of all notes for the\n"
   42   " rm <note>    Deletes the specified note.\n"
   43   "              given search term.\n"
   44   " help         Displays this help text\n"
   45   " ls,list      Lists all notes in note directory.\n";
   46   printf("%s\n", out);
   47 }
   48 
   49 
   50 /**
   51  * Prints names of all notes, sorted by last modified date
   52  *
   53  * @param list Note list whos names to enumerate
   54  */
   55 void list_notes(note_list* list) {
   56   note* n;
   57   int i = 0;
   58   int count = 0;
   59   note list_preload[256];
   60   char modtime[256];
   61 
   62   // Load note structs into an array
   63   while((n = note_list_read(list)) != NULL) {
   64     note_cpy(&list_preload[count], n);
   65     count++;
   66   }
   67 
   68   // Sort the preloaded list
   69   qsort(list_preload, count, sizeof(note), note_date_cmp);
   70 
   71   // Output from oldest to newest
   72   for(i = 0; i < count; i++) {
   73     strftime(modtime, 256, "%F %T", localtime(&list_preload[i].mtime));
   74     strip_extension(list_preload[i].name);
   75     printf("%s  %s\n", modtime, list_preload[i].name);
   76   }
   77 }
   78 
   79 
   80 /**
   81  * Sets up environment variables.
   82  * Handles loading in configuration defaults before loading the config file
   83  * values if one is present.
   84  */
   85 void config_setup(config_t* c) {
   86   // Get the home dir
   87   char home_path[strlen(getenv("HOME"))];
   88   strcpy(home_path, getenv("HOME"));
   89 
   90   // Set default note path
   91   char tmp_note_path[strlen(home_path) + 17];
   92   strcpy(tmp_note_path, home_path);
   93   strcat(tmp_note_path, "/Documents/Notes");
   94   config_set(c, "note_path", tmp_note_path);
   95 
   96   // Set the default note extension
   97   config_set(c, "extension", "mdown");
   98 
   99   // Set editor default
  100   char tmp_editor[128];
  101   get_user_editor(tmp_editor);
  102   config_set(c, "editor", tmp_editor);
  103 
  104   // Assemble the path to the conf file
  105   char conf_path[strlen(home_path) + 23];
  106   strcpy(conf_path, home_path);
  107   strcat(conf_path, "/.config/noteless.conf");
  108   // Load the actual config file to override any defaults
  109   config_load(c, conf_path);
  110 }
  111 
  112 
  113 int main(int argc, char* argv []) {
  114   // Print helptext if no commands specified
  115   if(argc == 1) {
  116     printf("\nNo command specified. Printing help text.\n");
  117     get_help();
  118     return 1;
  119   }
  120 
  121   config_t  c;
  122   note_list list;
  123   
  124   //
  125   // Config setup
  126   //
  127   config_setup(&c);
  128 
  129   note_list_new(
  130     &list,
  131     config_get(&c, "note_path"),
  132     config_get(&c, "extension")
  133   );
  134 
  135   if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
  136     list_notes(&list);
  137   } else if(strcmp(argv[1], "new") == 0) {
  138     int create_status = note_list_create_note(&list, config_get(&c, "editor"), argv[2]);
  139     // Notify user that a note already exists by the specified name
  140     if(create_status == 1) {
  141       printf("A note by the name %s already exists.\n", argv[2]);
  142       return create_status;
  143     }
  144   } else if(strcmp(argv[1], "edit") == 0) {
  145     return note_list_edit(&list, config_get(&c, "editor"), argv[2]);
  146   } else if(strcmp(argv[1], "rm") == 0) {
  147     int rm_status = note_list_rm_note(&list, argv[2]);
  148     if(rm_status == 0) {
  149       printf("Note matching \"%s\" deleted successfully\n", argv[2]);
  150     } else if(rm_status == 1) {
  151       printf("Error: There are no notes matching \"%s\".\n", argv[2]);
  152     } else {
  153       printf("Error: Failed deleting note matching \"%s\".\n", argv[2]);
  154     }
  155     return rm_status;
  156   } else if(strcmp(argv[1], "find") == 0) {
  157     return note_list_search(&list, argv[2], 1);
  158   } else if(strcmp(argv[1], "cat") == 0) {
  159     return note_list_cat_note(&list, argv[2]);
  160   } else if(strcmp(argv[1], "help") == 0) {
  161     get_help();
  162   } else {
  163     int err = note_list_edit(&list, config_get(&c, "editor"), argv[1]);
  164     if(err == 1) {
  165       printf("%s is also not a valid command.\n", argv[1]);
  166       return 1;
  167     }
  168   }
  169 
  170   // Clean up
  171   note_list_free(&list);
  172   config_free(&c);
  173 
  174   return 0;
  175 }

Generated by cgit