From b8b4cd6d60ef33aad00af91dcf8c11ba1ada06c5 Mon Sep 17 00:00:00 2001 From: m455 Date: Tue, 5 Feb 2019 21:20:28 -0500 Subject: [PATCH] cleaning up all of this code, and commenting complex functions --- args.rkt | 58 ++++++++++++++++++++++++++++---------------------------- init.rkt | 8 -------- util.rkt | 46 ++++++++++++++++++++++++++++++-------------- 3 files changed, 61 insertions(+), 51 deletions(-) diff --git a/args.rkt b/args.rkt index 8dd1422..d35889f 100644 --- a/args.rkt +++ b/args.rkt @@ -11,38 +11,38 @@ (define (check-args args) (let([args-length (vector-length args)]) - (cond [(equal? args-length 0) - (util:display-hash-ref messages:messages 'show-usage)] + (cond + [(equal? args-length 0) + (util:display-hash-ref messages:messages 'show-usage)] - [(and - (equal? args-length 1) - (equal? (vector:vector-member config:list-command args) 0)) - (util:show-list)] + [(and + (equal? args-length 1) + (equal? (vector:vector-member config:list-command args) 0)) + (util:show-list)] - [(and - (equal? args-length 2) - (equal? (vector-ref args 0) config:add-command)) - (util:add-item args)] + [(and + (equal? args-length 2) + (equal? (vector-ref args 0) config:add-command)) + (util:add-item args)] - [(and - (equal? args-length 2) - (equal? (vector:vector-member config:remove-command args) 0) - (not (equal? (vector:vector-member "0" args) 1)) - (vector:vector-member - (vector-ref args 1) - (list->vector - (map number->string (list:rest (list:range (length (util:file->string-list config:path)))))))) - (util:remove-item args)] + [(and + (equal? args-length 2) + (equal? (vector:vector-member config:remove-command args) 0) + ;; Don't allow user to remove zeroth item + (not (equal? (vector:vector-member "0" args) 1)) + ;; Don't allow removal of items beyond last item + (not (> (string->number (vector-ref args 1)) (length (util:file->string-list config:path))))) + (util:remove-item args)] - [(and - (equal? args-length 1) - (equal? (vector:vector-member config:initialize-command args) 0)) - (init:initialize)] + [(and + (equal? args-length 1) + (equal? (vector:vector-member config:initialize-command args) 0)) + (init:initialize)] - [(and - (equal? args-length 1) - (member (vector-ref args 0) config:help-command)) - (util:display-hash-ref messages:messages 'show-help)] + [(and + (equal? args-length 1) + (member (vector-ref args 0) config:help-command)) + (util:display-hash-ref messages:messages 'show-help)] - [else - (util:display-hash-ref messages:messages 'show-usage)]))) + [else + (util:display-hash-ref messages:messages 'show-usage)]))) diff --git a/init.rkt b/init.rkt index 0b1627f..d7d9b2e 100644 --- a/init.rkt +++ b/init.rkt @@ -7,13 +7,6 @@ (provide (all-defined-out)) -(define (initialize-file) - (file:display-to-file - "--Do not edit this file--\n" - config:path - #:mode 'text - #:exists 'replace)) - (define (init-prompt hash-list key) (util:display-hash-ref hash-list key) (display "> ") @@ -25,7 +18,6 @@ (util:display-hash-ref messages:messages 'creating-file) (util:create-folder) (util:create-file) - (initialize-file) (if (and (util:check-for-folder) (util:check-for-file)) diff --git a/util.rkt b/util.rkt index 37a6be4..6369a42 100644 --- a/util.rkt +++ b/util.rkt @@ -33,6 +33,8 @@ (define (display-hash-ref hash-list key) (display (hash-ref hash-list key))) +;; Just so I don't have to keep typing +;; "#:mode...#:line-mode..." every time (define (file->string-list config:path-to-file) (let ([todo-list (file:file->lines config:path-to-file #:mode 'text @@ -40,21 +42,35 @@ todo-list)) (define (list-empty? lst) - (list:empty? (list:rest (file->string-list lst)))) + (list:empty? (file->string-list lst))) +;; Find out which item is being removed by scooping up +;; the number the user entered in the command line +;; argument (define (get-removed-item lst args) - (list-ref lst (string->number args))) + ;; Subtract one from what the user chose, because they are + ;; are actually viewing the list numbers as human numbers + ;; so (actual-number +1) + (list-ref (file->string-list lst) (sub1 (string->number args)))) -(define (quote-item args) - (display - (string-append "\"" args "\""))) +(define (surround-in-quotes args) + (display (string-append "\"" args "\""))) (define (prefix-with-number lst) + ;; Connect the list of numbers to the list of items (map string-append + ;; Convert the numbers made below into strings + ;; so we can append them to other strings (map number->string - (list:rest - (list:range (length lst)))) - (list:rest lst))) + ;; The add1 here makes the numbers more human by + ;; starting at 1 instead of 0 + (map add1 + ;; Create a list of numbers from the total + ;; number of items in a list + (list:range (length lst)))) + ;; This is just the original list that everything will + ;; be appended to + lst)) (define (prefix-with-period lst) (string-append ". " lst)) @@ -70,12 +86,12 @@ (define (display-item-added args) (display-hash-ref messages:messages 'item-added-prefix) - (quote-item args) + (surround-in-quotes args) (display-hash-ref messages:messages 'item-added-suffix)) (define (display-item-removed args) (display-hash-ref messages:messages 'item-removed-prefix) - (quote-item args) + (surround-in-quotes args) (display-hash-ref messages:messages 'item-removed-suffix)) (define (show-list) @@ -93,7 +109,7 @@ (define (add-item-to-file args) (let ([new-list (append-to-end args config:path)]) (file:display-to-file - (string:string-join new-list "\n" #:after-last "\n") + (string:string-join new-list "\n");; #:after-last "\n") config:path #:mode 'text #:exists 'replace) @@ -109,12 +125,14 @@ (display-hash-ref messages:messages 'try-init)))) (define (remove-item-from-file args) - (let ([removed-item (get-removed-item (file->string-list config:path) args)] + (let ([removed-item (get-removed-item config:path args)] [new-list (remove - (list-ref (file->string-list config:path) (string->number args)) + ;; Todo: Some how add removed-item here + ;; from above + (get-removed-item config:path args) (file->string-list config:path))]) (file:display-to-file - (string:string-join new-list "\n" #:after-last "\n") + (string:string-join new-list "\n");; #:after-last "\n") config:path #:mode 'text #:exists 'replace)