cleaned up util.rkt and args.rkt

main
m455 2019-02-05 22:03:21 -05:00
parent b8b4cd6d60
commit 1e7f73957e
1 changed files with 34 additions and 21 deletions

View File

@ -37,8 +37,8 @@
;; "#:mode...#:line-mode..." every time ;; "#: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
#:line-mode 'any)]) #:line-mode 'any)])
todo-list)) todo-list))
(define (list-empty? lst) (define (list-empty? lst)
@ -56,31 +56,34 @@
(define (surround-in-quotes args) (define (surround-in-quotes args)
(display (string-append "\"" args "\""))) (display (string-append "\"" args "\"")))
(define (prefix-with-period lst)
(string-append lst ". "))
(define (prefix-with-number lst) (define (prefix-with-number lst)
;; Connect the list of numbers to the list of items ;; Connect the list of numbers to the list of items
(map string-append (map string-append
;; Convert the numbers made below into strings (map prefix-with-period
;; so we can append them to other strings ;; 3. Convert the numbers made below into strings
(map number->string ;; so we can append them to other strings
;; The add1 here makes the numbers more human by (map number->string
;; starting at 1 instead of 0 ;; 2. The add1 here makes the numbers more human by
(map add1 ;; starting at 1 instead of 0
;; Create a list of numbers from the total (map add1
;; number of items in a list ;; 1. Create a list of numbers from the total
(list:range (length lst)))) ;; number of items in a list
(list:range (length lst)))))
;; This is just the original list that everything will ;; This is just the original list that everything will
;; be appended to ;; be appended to
lst)) lst))
(define (prefix-with-period lst) (define (display-prettified-list)
(string-append ". " lst))
(define (prettify-list)
(display (display
(string:string-join (prefix-with-number (map prefix-with-period (file->string-list config:path))) (string:string-join
(prefix-with-number (file->string-list config:path))
"\n" "\n"
#:after-last "\n"))) #:after-last "\n")))
;; This is a bit of ugly scheme sorcery
(define (append-to-end args lst) (define (append-to-end args lst)
(reverse (cons args (reverse (file->string-list lst))))) (reverse (cons args (reverse (file->string-list lst)))))
@ -99,17 +102,23 @@
(check-for-folder) (check-for-folder)
(check-for-file)) (check-for-file))
(if (if
;; If file exists, see if it's empty, if so
;; tell the user
(list-empty? config:path) (list-empty? config:path)
(display-hash-ref messages:messages 'empty-todo-list) (display-hash-ref messages:messages 'empty-todo-list)
(prettify-list))] ;; If file isn't empty, display a pretty list
(display-prettified-list))]
;; If file doesn't exist, tell the user
[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 (add-item-to-file args) (define (add-item-to-file args)
;; Add item to end of list and write to file
(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")
config:path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'replace)
@ -126,13 +135,15 @@
(define (remove-item-from-file args) (define (remove-item-from-file args)
(let ([removed-item (get-removed-item config:path args)] (let ([removed-item (get-removed-item config:path args)]
;; Todo:
;; Some how add removed-item here, instead of
;; repeating (get-removed-item config:path args).
;; This is still a mystery to me
[new-list (remove [new-list (remove
;; Todo: Some how add removed-item here
;; from above
(get-removed-item config:path args) (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")
config:path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'replace)
@ -141,10 +152,12 @@
(define (remove-item args) (define (remove-item args)
(cond [(list-empty? config:path) (cond [(list-empty? config:path)
(display-hash-ref messages:messages 'empty-todo-list)] (display-hash-ref messages:messages 'empty-todo-list)]
[(and [(and
(check-for-folder) (check-for-folder)
(check-for-file)) (check-for-file))
(remove-item-from-file (vector-ref args 1))] (remove-item-from-file (vector-ref args 1))]
[(and (not (check-for-folder)) (not (check-for-file))) [(and (not (check-for-folder)) (not (check-for-file)))
(begin (begin
(display-hash-ref messages:messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)