made the number-prefixing a little easier to read code-wise

main
m455 2019-06-30 17:07:33 -04:00
parent 9adb25c4a2
commit 67a1ebb988
1 changed files with 34 additions and 33 deletions

View File

@ -38,9 +38,9 @@
(list:empty? (file->string-list lst))) (list:empty? (file->string-list lst)))
(define (get-removed-item lst args) (define (get-removed-item lst args)
;; Subtract one from what the user chose, because they are ;; Subtract one from what the user chose, because what they are actually
;; are actually viewing the list numbers as human numbers, ;; viewing is a list starting from "1" rather than "0". Under the hood,
;; so what they see is actual-number +1 ;; the real list starts at 0.
(list-ref (file->string-list lst) (sub1 (string->number args)))) (list-ref (file->string-list lst) (sub1 (string->number args))))
(define (surround-item-in-quotation-marks args) (define (surround-item-in-quotation-marks args)
@ -50,21 +50,22 @@
(string-append lst ". ")) (string-append lst ". "))
(define (prefix-item-with-number lst) (define (prefix-item-with-number lst)
;; Take the list made in the first (map), which is
;; '(1 2 3 ...), and append that to each item in a list
(map string-append (map string-append
;; Note: compose starts from the last element in it's ;; Note: compose starts from the last element in it's
;; list, so that would be add1 here ;; list. In this case, it starts at (number->string).
(map (compose prefix-item-with-period number->string add1) (map (compose1 prefix-item-with-period number->string)
(list:range (length lst))) (list:range 1 (add1 (length lst))))
lst)) lst))
(define (display-prettified-program-file) (define (display-prettified-program-file)
(display (display (string:string-join
(string:string-join (prefix-item-with-number (file->string-list config:path-to-file))
(prefix-item-with-number (file->string-list config:path-to-file)) "\n"
"\n" #:after-last "\n")))
#:after-last "\n")))
;; This one is pretty wild ;; Don't ask
(define (append-item-to-end args lst) (define (append-item-to-end args lst)
(reverse (cons args (reverse (file->string-list lst))))) (reverse (cons args (reverse (file->string-list lst)))))
@ -80,45 +81,45 @@
(define (show-list-from-program-file) (define (show-list-from-program-file)
(cond [(and (cond [(and
(check-for-program-directory) (check-for-program-directory)
(check-for-program-file)) (check-for-program-file))
(if (if
(list-empty? config:path-to-file) (list-empty? config:path-to-file)
(display-hash-ref messages:messages 'empty-todo-list) (display-hash-ref messages:messages 'empty-todo-list)
(display-prettified-program-file))] (display-prettified-program-file))]
[else [else
(display-hash-ref messages:messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init)])) (display-hash-ref messages:messages 'try-init)]))
(define (write-item-to-program-file args) (define (write-item-to-program-file args)
;; Add an item to the end of a list and write to a file ;; Add an item to the end of a list and write to a file
(let ([new-list (append-item-to-end args config:path-to-file)]) (let ([new-list (append-item-to-end args config:path-to-file)])
(file:display-to-file (file:display-to-file
(string:string-join new-list "\n") config:path-to-file (string:string-join new-list "\n") config:path-to-file
#:mode 'text #:mode 'text
#:exists 'truncate) #:exists 'truncate)
;; After writing to the file, tell the user what was written ;; After writing to the file, tell the user what was written
(display-item-added args))) (display-item-added args)))
(define (add-item-to-list args) (define (add-item-to-list args)
(if (and (check-for-program-directory) (if (and (check-for-program-directory)
(check-for-program-file)) (check-for-program-file))
;; The cdr here prevents the first command line argument ("add") ;; The cdr here prevents the first command line argument ("add")
;; from being added to the file ;; from being added to the file
(write-item-to-program-file (string:string-join (cdr (vector->list args)))) (write-item-to-program-file (string:string-join (cdr (vector->list args))))
;; Else ;; Else
(begin (begin
(display-hash-ref messages:messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init)))) (display-hash-ref messages:messages 'try-init))))
(define (remove-item-from-program-file args) (define (remove-item-from-program-file args)
(let* ([removed-item (get-removed-item config:path-to-file args)] (let* ([removed-item (get-removed-item config:path-to-file args)]
[new-list (remove removed-item (file->string-list config:path-to-file))]) [new-list (remove removed-item (file->string-list config:path-to-file))])
(file:display-to-file (file:display-to-file
(string:string-join new-list "\n") (string:string-join new-list "\n")
config:path-to-file config:path-to-file
#:mode 'text #:mode 'text
#:exists 'truncate) #:exists 'truncate)
(display-item-removed removed-item))) (display-item-removed removed-item)))
(define (remove-item-from-list args) (define (remove-item-from-list args)