From 570ef8d78f82976533f0f4a4a099e53ea72756b7 Mon Sep 17 00:00:00 2001 From: m455 Date: Fri, 22 Nov 2019 13:35:25 -0500 Subject: [PATCH] rewrote everything --- args.rkt | 18 +++++------ config.rkt | 41 +++++++++++------------ init.rkt | 31 +++++++++--------- messages.rkt | 64 +++++++++++++++--------------------- utils.rkt | 91 +++++++++++++++++++++++----------------------------- 5 files changed, 109 insertions(+), 136 deletions(-) diff --git a/args.rkt b/args.rkt index 44a134e..2ebf8a3 100644 --- a/args.rkt +++ b/args.rkt @@ -16,30 +16,30 @@ ;; help-command [(and (equal? args-length 1) - (member (list-ref args 0) config:help-command)) + (member (list-ref args 0) config:help-commands)) (utils:display-hash-ref messages:messages 'show-help)] ;; initialize-command [(and (equal? args-length 1) - (member (list-ref args 0) config:initialize-command)) - (init:initialize)] + (member (list-ref args 0) config:initialize-commands)) + (init:check-initialize-conditions)] ;; add-command [(and (or (equal? args-length 2) (>= args-length 2)) - (member (list-ref args 0) config:add-command)) - (utils:add-item-to-list config:list-file args)] + (member (list-ref args 0) config:add-commands)) + (utils:check-add-conditions args)] ;; list-command [(and (equal? args-length 1) - (member (list-ref args 0) config:list-command)) - (utils:show-list-from-file config:list-file)] + (member (list-ref args 0) config:list-commands)) + (utils:check-list-conditions)] ;; remove-command [(and (equal? args-length 2) - (member (list-ref args 0) config:remove-command) + (member (list-ref args 0) config:remove-commands) (real? (string->number (list-ref args 1))) (or (positive? (string->number (list-ref args 1))) (zero? (string->number (list-ref args 1))))) - (utils:remove-item-from-list args)] + (utils:check-remove-conditions args)] [else (utils:display-hash-ref messages:messages 'show-usage)]))) diff --git a/config.rkt b/config.rkt index b19ab37..9782c62 100644 --- a/config.rkt +++ b/config.rkt @@ -6,34 +6,29 @@ (define program-directory (path->string - (expand-user-path - (string-append "~/." program-name "/")))) + (expand-user-path + (string-append "~/." program-name "/")))) (define list-file (string-append program-directory file-name)) -;; TODO: pluralize this value's name -(define help-command '("-h" - "--help" - "h" - "help")) +(define help-commands '("-h" + "--help" + "h" + "help")) -;; TODO: pluralize this value's name -(define initialize-command '("init" - "create" - "start" - "begin")) +(define initialize-commands '("init" + "create" + "start" + "begin")) -;; TODO: pluralize this value's name -(define add-command '("add" - "a")) +(define add-commands '("add" + "a")) -;; TODO: pluralize this value's name -(define list-command '("ls" - "list")) +(define list-commands '("ls" + "list")) -;; TODO: pluralize this value's name -(define remove-command '("rm" - "remove" - "del" - "delete")) +(define remove-commands '("rm" + "remove" + "del" + "delete")) diff --git a/init.rkt b/init.rkt index f71acef..16b0129 100644 --- a/init.rkt +++ b/init.rkt @@ -6,26 +6,27 @@ (provide (all-defined-out)) -(define (init-prompt hash-list key) - (utils:display-hash-ref hash-list key) +(define (create-initialization-contents) + (utils:display-hash-ref messages:messages 'creating) + (utils:create-directory-700 config:program-directory) + (utils:create-file-600 config:list-file) + (if (and (directory-exists? config:program-directory) + (file-exists? config:list-file)) + (utils:display-hash-ref messages:messages 'successfully-created) + ;; Otherwise + (utils:display-hash-ref messages:messages 'creation-error))) + +(define (initialize) + (utils:display-hash-ref messages:messages 'init-y/n) (display "> ") (let ([user-input (read-line)]) (cond [(member user-input (hash-ref messages:y/n 'yes)) - (begin - (utils:display-hash-ref messages:messages 'creating) - (utils:create-directory-700 config:program-directory) - (utils:create-file-600 config:list-file) - (if (and (directory-exists? config:program-directory) - (file-exists? config:list-file)) - (utils:display-hash-ref messages:messages 'successfully-created) - (utils:display-hash-ref messages:messages 'creation-error)))] + (create-initialization-contents)] [(member user-input (hash-ref messages:y/n 'no)) (utils:display-hash-ref messages:messages 'terminating)] - [else - (init-prompt messages:messages 'choose-y/n)]))) + [else (utils:display-hash-ref messages:messages 'choose-y/n)]))) -(define (initialize) +(define (check-initialize-conditions) (if (file-exists? config:list-file) (utils:display-hash-ref messages:messages 'file-already-exists) - (begin - (init-prompt messages:messages 'init-y/n)))) + (initialize))) diff --git a/messages.rkt b/messages.rkt index 2a0f484..95d46d0 100644 --- a/messages.rkt +++ b/messages.rkt @@ -4,7 +4,8 @@ (provide (all-defined-out)) -(define newline "\n") +(define (indent string) + (string-append "\t" string)) (define messages (hash @@ -22,80 +23,68 @@ "\n\n" ;; initialize-command - (format "~a" (car config:initialize-command)) + (format "~a" (car config:initialize-commands)) "\n\n" - "\t" - (format "Creates a list in ~a." config:list-file) + (indent (format "Creates a list in ~a." config:list-file)) "\n\n" ;; list-command - (format "~a" (car config:list-command)) + (format "~a" (car config:list-commands)) "\n\n" - "\t" - "Displays items in your list." + (indent "Displays items in your list.") "\n\n" ;; add-command - (format "~a" (car config:add-command)) + (format "~a" (car config:add-commands)) "\n\n" - "\t" - "Adds an item to your list." + (indent "Adds an item to your list.") "\n\n" ;; remove-command - (format "~a" (car config:remove-command)) + (format "~a" (car config:remove-commands)) "\n\n" - "\t" - "Removes an item from your list." + (indent "Removes an item from your list.") "\n\n" "Usage examples\n" "==============" "\n\n" + ;; initialize-command - (format "~a" (car config:initialize-command)) + (format "~a" (car config:initialize-commands)) "\n\n" - "\t" - (format "~a ~a" config:program-name (car config:initialize-command)) + (indent (format "~a ~a" config:program-name (car config:initialize-commands))) "\n\n" ;; list-command - (format "~a" (car config:list-command)) + (format "~a" (car config:list-commands)) "\n\n" - "\t" - (format "~a ~a" config:program-name (car config:list-command)) + (indent (format "~a ~a" config:program-name (car config:list-commands))) "\n\n" ;; add-command - (format "~a" (car config:add-command)) + (format "~a" (car config:add-commands)) "\n\n" - "\t" - (format "~a ~a this is an item without double quotation marks" config:program-name (car config:add-command)) + (indent (format "~a ~a this is an item without double quotation marks" config:program-name (car config:add-commands))) "\n\n" - "\t" - (format "~a ~a \"this is an item surrounded by double quotation marks\"" config:program-name (car config:add-command)) + (indent (format "~a ~a \"this is an item surrounded by double quotation marks\"" config:program-name (car config:add-commands))) "\n\n" - "\t" - "Note: Grave accents (`) and single quotation marks (\') cannot be used with this command." + (indent "Note: Grave accents (`) and single quotation marks (\') cannot be used with this command.") "\n\n" ;; remove-command - (format "~a" (car config:remove-command)) + (format "~a" (car config:remove-commands)) "\n\n" - "\t" - (format "~a ~a 2" config:program-name (car config:remove-command)) + (indent (format "~a ~a 2" config:program-name (car config:remove-commands))) "\n\n" - "\t" - "Note: The example above will remove the third item from your list, because the list starts at zero." + (indent "Note: The example above will remove the third item from your list, because the list starts at zero.") "\n\n" "Can't see the whole help message?\n" "=================================" "\n\n" - "\t" (format "Try running \"~a -h | less\" (without the double quotation marks), so you can use the arrow keys to scroll up and down." config:program-name) "\n\n" - "\t" "When you want to quit, type \"q\" (without the double quotation marks)." "\n\n") @@ -120,7 +109,7 @@ config:list-file) 'try-init (format "> Try typing \"~a ~a\" to set it up (without the double quotation marks).\n" - config:program-name (car config:initialize-command)) + config:program-name (car config:initialize-commands)) 'terminating (format "> Exited ~a.\n" config:program-name) @@ -128,13 +117,10 @@ 'not-in-list "> Error: Item does not exist\n" - 'item-added-prefix "> Added " + 'item-added "> Added \"~a\" to your list\n" - 'item-added-suffix " to list\n" + 'item-removed "> Removed \"~a\" from your list\n")) - 'item-removed-prefix "> Removed " - - 'item-removed-suffix " from list\n")) (define y/n (hash 'yes '("yes" "Yes" "y" "Y") 'no '("no" "No" "n" "N"))) diff --git a/utils.rkt b/utils.rkt index 945f1f2..deab853 100644 --- a/utils.rkt +++ b/utils.rkt @@ -20,6 +20,10 @@ (define (display-hash-ref hash-list key) (display (hash-ref hash-list key))) +(define (display-hash-ref-multi hash-list key-list) + (for ([key key-list]) + (display (hash-ref hash-list key)))) + (define (list->ascending-numbers lst) (list:range (length lst))) @@ -41,61 +45,58 @@ (define (surround-with-quotation-marks item) (string-append "\"" item "\"")) -(define (display-item-added item) - (display-hash-ref messages:messages 'item-added-prefix) - (display (surround-with-quotation-marks item)) - (display-hash-ref messages:messages 'item-added-suffix)) +(define (display-item-added item-to-add) + (display (format (hash-ref messages:messages 'item-added) item-to-add))) -(define (display-item-removed args) - (display-hash-ref messages:messages 'item-removed-prefix) - (display (surround-with-quotation-marks args)) - (display-hash-ref messages:messages 'item-removed-suffix)) +(define (display-item-removed item-to-remove) + (display (format (hash-ref messages:messages 'item-removed) item-to-remove))) -;; TODO: Turn into a check-show-list-conditions and then break -;; the rest down into separate functions -(define (show-list-from-file a-file) +(define (check-list-conditions) (cond ;; If exists and not empty [(and (file-exists? config:list-file) - (not (null? (file:file->lines a-file)))) - (display (file->vertically-numbered-list a-file))] + (not (null? (file:file->lines config:list-file)))) + (display (file->vertically-numbered-list config:list-file))] ;; If exists and empty [(and (file-exists? config:list-file) - (null? (file:file->lines a-file))) + (null? (file:file->lines config:list-file))) (display-hash-ref messages:messages 'empty-list)] ;; If not exist [(and (not (file-exists? config:list-file))) - (begin - (display-hash-ref messages:messages 'file-not-found) - (display-hash-ref messages:messages 'try-init))] + (display-hash-ref-multi messages:messages '(file-not-found try-init))] - ;; Otherwise [else (display-hash-ref messages:messages 'show-usage)])) -(define (append-item-to-list args lst) - (append (file:file->lines lst) '(args))) +(define (append-element-to-end-of-list lst item-to-add) + (reverse (cons item-to-add (reverse (file:file->lines lst))))) -;; TODO: Turn into a check-add-conditions and then break -;; the rest down into separate procedures -(define (add-item-to-list a-file args) +(define (add-item-to-list args) + (let* ([item-to-add (string:string-join (cdr args) " ")] + [new-list (append-element-to-end-of-list config:list-file item-to-add)]) + (file:display-lines-to-file new-list + config:list-file + #:mode 'text + #:exists 'truncate) + (display-item-added item-to-add))) + +(define (check-add-conditions args) (if (and (file-exists? config:list-file)) - ;; Make this a procedure - (let* ([item (string:string-join (cdr args) " ")] - [new-list (append-item-to-list item config:list-file)]) - (file:display-to-file (string:string-join new-list "\n") - config:list-file - #:mode 'text - #:exists 'truncate) - (display-item-added item)) - (begin - (display-hash-ref messages:messages 'file-not-found) - (display-hash-ref messages:messages 'try-init)))) + (add-item-to-list args) + ;; Otherwise + (display-hash-ref-multi messages:messages '(file-not-found try-init)))) -;; TODO: Turn into a check-remove-conditions and then break -;; the rest down into separate procedures -(define (remove-item-from-list args) +(define (remove-item-from-list user-args) + (let* ([item-to-remove (list-ref (file:file->lines config:list-file) user-args)] + [new-list (remove item-to-remove (file:file->lines config:list-file))]) + (file:display-lines-to-file new-list + config:list-file + #:mode 'text + #:exists 'truncate) + (display-item-removed item-to-remove))) + +(define (check-remove-conditions args) (cond ;; If directory and file exist, but file is empty [(and (directory-exists? config:program-directory) @@ -111,21 +112,11 @@ ;; Length subtract one because the numbering starts at zero [list-length (sub1 (length (file:file->lines config:list-file)))]) (if (not (> user-args list-length)) - ;; Make this a procedure - (let* ([item-to-remove (list-ref (file:file->lines config:list-file) user-args)] - [new-list (remove item-to-remove (file:file->lines config:list-file))]) - ;; Might be able to just use display-lines-to-file procedure. See docs - (file:display-to-file (string:string-join new-list "\n") - config:list-file - #:mode 'text - #:exists 'truncate) - (display-item-removed item-to-remove)) - ;; Else + (remove-item-from-list user-args) + ;; Otherwise (display-hash-ref messages:messages 'item-not-found)))] ;; If directory and file don't exist [(and (not (directory-exists? config:program-directory)) (not (file-exists? config:list-file))) - (begin - (display-hash-ref messages:messages 'file-not-found) - (display-hash-ref messages:messages 'try-init))])) + (display-hash-ref-multi messages:messages '(file-not-found try-init))]))