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

Generated by cgit