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)
(let([args-length (vector-length args)])
(cond [(equal? args-length 0)
(cond
[(equal? args-length 0)
(util:display-hash-ref messages:messages 'show-usage)]
[(and
@ -27,11 +28,10 @@
[(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))
(vector:vector-member
(vector-ref args 1)
(list->vector
(map number->string (list:rest (list:range (length (util:file->string-list config:path))))))))
;; 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

View File

@ -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))

View File

@ -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
;; 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:rest 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)