cleaning up all of this code, and commenting complex functions

main
m455 2019-02-05 21:20:28 -05:00
parent 3f68538eb3
commit b8b4cd6d60
3 changed files with 61 additions and 51 deletions

View File

@ -11,7 +11,8 @@
(define (check-args args) (define (check-args args)
(let([args-length (vector-length args)]) (let([args-length (vector-length args)])
(cond [(equal? args-length 0) (cond
[(equal? args-length 0)
(util:display-hash-ref messages:messages 'show-usage)] (util:display-hash-ref messages:messages 'show-usage)]
[(and [(and
@ -27,11 +28,10 @@
[(and [(and
(equal? args-length 2) (equal? args-length 2)
(equal? (vector:vector-member config:remove-command args) 0) (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)) (not (equal? (vector:vector-member "0" args) 1))
(vector:vector-member ;; Don't allow removal of items beyond last item
(vector-ref args 1) (not (> (string->number (vector-ref args 1)) (length (util:file->string-list config:path)))))
(list->vector
(map number->string (list:rest (list:range (length (util:file->string-list config:path))))))))
(util:remove-item args)] (util:remove-item args)]
[(and [(and

View File

@ -7,13 +7,6 @@
(provide (all-defined-out)) (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) (define (init-prompt hash-list key)
(util:display-hash-ref hash-list key) (util:display-hash-ref hash-list key)
(display "> ") (display "> ")
@ -25,7 +18,6 @@
(util:display-hash-ref messages:messages 'creating-file) (util:display-hash-ref messages:messages 'creating-file)
(util:create-folder) (util:create-folder)
(util:create-file) (util:create-file)
(initialize-file)
(if (and (if (and
(util:check-for-folder) (util:check-for-folder)
(util:check-for-file)) (util:check-for-file))

View File

@ -33,6 +33,8 @@
(define (display-hash-ref hash-list key) (define (display-hash-ref hash-list key)
(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) (define (file->string-list config:path-to-file)
(let ([todo-list (file:file->lines config:path-to-file (let ([todo-list (file:file->lines config:path-to-file
#:mode 'text #:mode 'text
@ -40,21 +42,35 @@
todo-list)) todo-list))
(define (list-empty? lst) (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) (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) (define (surround-in-quotes args)
(display (display (string-append "\"" args "\"")))
(string-append "\"" args "\"")))
(define (prefix-with-number lst) (define (prefix-with-number lst)
;; Connect the list of numbers to the list of items
(map string-append (map string-append
;; Convert the numbers made below into strings
;; so we can append them to other strings
(map number->string (map number->string
(list:rest ;; 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)))) (list:range (length lst))))
(list:rest lst))) ;; This is just the original list that everything will
;; be appended to
lst))
(define (prefix-with-period lst) (define (prefix-with-period lst)
(string-append ". " lst)) (string-append ". " lst))
@ -70,12 +86,12 @@
(define (display-item-added args) (define (display-item-added args)
(display-hash-ref messages:messages 'item-added-prefix) (display-hash-ref messages:messages 'item-added-prefix)
(quote-item args) (surround-in-quotes args)
(display-hash-ref messages:messages 'item-added-suffix)) (display-hash-ref messages:messages 'item-added-suffix))
(define (display-item-removed args) (define (display-item-removed args)
(display-hash-ref messages:messages 'item-removed-prefix) (display-hash-ref messages:messages 'item-removed-prefix)
(quote-item args) (surround-in-quotes args)
(display-hash-ref messages:messages 'item-removed-suffix)) (display-hash-ref messages:messages 'item-removed-suffix))
(define (show-list) (define (show-list)
@ -93,7 +109,7 @@
(define (add-item-to-file args) (define (add-item-to-file args)
(let ([new-list (append-to-end args config:path)]) (let ([new-list (append-to-end args config:path)])
(file:display-to-file (file:display-to-file
(string:string-join new-list "\n" #:after-last "\n") (string:string-join new-list "\n");; #:after-last "\n")
config:path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'replace)
@ -109,12 +125,14 @@
(display-hash-ref messages:messages 'try-init)))) (display-hash-ref messages:messages 'try-init))))
(define (remove-item-from-file args) (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 [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->string-list config:path))])
(file:display-to-file (file:display-to-file
(string:string-join new-list "\n" #:after-last "\n") (string:string-join new-list "\n");; #:after-last "\n")
config:path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'replace)