/** * Copyright (C) 2021 Aaron Ball * * Noteless is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Noteless is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with noteless. If not, see . */ #include #include #include #include #include #include #include "note_list.h" #include "note.h" #include "config.h" /** * Prints the standard help text */ void get_help() { char out[] = "\nNoteless provides a simple command line interface for note " "taking.\n\n" "What makes this different than simply using a command line text editor is\n" "there is no need to type paths or file extensions. It handles all of the\n" "file management without having to change directories. It can also perform\n" "search operations without having to write a long command.\n\n" "Arguments\n" " cat Outputs the specified note's contents verbatim.\n" " new Creates the specified note and opens for editing.\n" " edit Opens the specified note for editing.\n" " find Performs a case-insensitive search of all notes for the\n" " rm Deletes the specified note.\n" " given search term.\n" " help Displays this help text\n" " ls,list Lists all notes in note directory.\n"; printf("%s\n", out); } /** * Prints names of all notes, sorted by last modified date * * @param list Note list whos names to enumerate */ void list_notes(note_list* list) { note* n; int i = 0; int count = 0; note list_preload[256]; char modtime[256]; // Load note structs into an array while((n = note_list_read(list)) != NULL) { note_cpy(&list_preload[count], n); count++; } // Sort the preloaded list qsort(list_preload, count, sizeof(note), note_date_cmp); // Output from oldest to newest for(i = 0; i < count; i++) { strftime(modtime, 256, "%F %T", localtime(&list_preload[i].mtime)); strip_extension(list_preload[i].name); printf("%s %s\n", modtime, list_preload[i].name); } } /** * Set up environment variables. * Handles loading in configuration defaults before loading the config file * values if one is present. */ void config_setup(struct config *c) { char cpath[1024]; config_init(c); // Set default note path sprintf(c->notepath, "%s/%s", getenv("HOME"), "/Documents/Notes"); // Set the default note extension strcpy(c->extension, "adoc"); // Set the default note extension strcpy(c->editor, "vim"); sprintf(cpath, "%s/.config/noteless.conf", getenv("HOME")); config_read(c, cpath); } int main(int argc, char* argv []) { // Print helptext if no commands specified if(argc == 1) { printf("\nNo command specified. Printing help text.\n"); get_help(); return 1; } struct config c; note_list list; config_setup(&c); note_list_new(&list, c.notepath, c.extension); if(errno != 0) { return 1; } if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) { list_notes(&list); } else if(strcmp(argv[1], "new") == 0) { int create_status = note_list_create_note(&list, c.editor, argv[2]); // Notify user that a note already exists by the specified name if(create_status == 1) { printf("A note by the name %s already exists.\n", argv[2]); return create_status; } } else if(strcmp(argv[1], "edit") == 0) { return note_list_edit(&list, c.editor, argv[2]); } else if(strcmp(argv[1], "rm") == 0) { int rm_status = note_list_rm_note(&list, argv[2]); if(rm_status == 0) { printf("Note matching \"%s\" deleted successfully\n", argv[2]); } else if(rm_status == 1) { printf("Error: There are no notes matching \"%s\".\n", argv[2]); } else { printf("Error: Failed deleting note matching \"%s\".\n", argv[2]); } return rm_status; } else if(strcmp(argv[1], "find") == 0) { return note_list_search(&list, argv[2], 1); } else if(strcmp(argv[1], "cat") == 0) { return note_list_cat_note(&list, argv[2]); } else if(strcmp(argv[1], "help") == 0) { get_help(); } else { int err = note_list_edit(&list, c.editor, argv[1]); if(err == 1) { printf("%s is also not a valid command.\n", argv[1]); return 1; } } // Clean up note_list_free(&list); return 0; }