made function names more descriptive

main
m455 2019-05-25 09:49:06 -04:00
parent 59630f3b64
commit ed0152a71c
4 changed files with 157 additions and 158 deletions

View File

@ -19,13 +19,13 @@
[(and [(and
(equal? args-length 1) (equal? args-length 1)
(equal? (vector:vector-member config:list-command args) 0)) (equal? (vector:vector-member config:list-command args) 0))
(util:show-list)] (util:show-list-from-program-file)]
;; add ;; add
[(and [(and
(or (equal? args-length 2) (>= args-length 2)) (or (equal? args-length 2) (>= args-length 2))
(equal? (vector-ref args 0) config:add-command)) (equal? (vector-ref args 0) config:add-command))
(util:add-item args)] (util:add-item-to-list args)]
;; rm ;; rm
[(and [(and
@ -35,7 +35,7 @@
(positive? (string->number (vector-ref args 1))) (positive? (string->number (vector-ref args 1)))
(not (> (string->number (vector-ref args 1)) (length (util:file->string-list config:path-to-file)))) (not (> (string->number (vector-ref args 1)) (length (util:file->string-list config:path-to-file))))
(not (< (string->number (vector-ref args 1)) (car (list:range (length (util:file->string-list config:path-to-file))))))) (not (< (string->number (vector-ref args 1)) (car (list:range (length (util:file->string-list config:path-to-file)))))))
(util:remove-item args)] (util:remove-item-from-list args)]
;; init ;; init
[(and [(and

View File

@ -15,11 +15,11 @@
(cond (cond
[(member user-input (hash-ref messages:y/n 'yes)) [(member user-input (hash-ref messages:y/n 'yes))
(util:display-hash-ref messages:messages 'creating) (util:display-hash-ref messages:messages 'creating)
(util:create-directory) (util:create-program-directory)
(util:create-file) (util:create-program-file)
(if (and (if (and
(util:check-for-directory) (util:check-for-program-directory)
(util:check-for-file)) (util:check-for-program-file))
(util:display-hash-ref messages:messages 'successfully-created) (util:display-hash-ref messages:messages 'successfully-created)
(util:display-hash-ref messages:messages 'creation-error))] (util:display-hash-ref messages:messages 'creation-error))]
@ -30,7 +30,7 @@
(init-prompt messages:messages 'choose-y/n)]))) (init-prompt messages:messages 'choose-y/n)])))
(define (initialize) (define (initialize)
(if (util:check-for-file) (if (util:check-for-program-file)
(util:display-hash-ref messages:messages 'file-already-exists) (util:display-hash-ref messages:messages 'file-already-exists)
(begin (begin
(init-prompt messages:messages 'init-y/n)))) (init-prompt messages:messages 'init-y/n))))

View File

@ -1,8 +1,8 @@
#lang racket/base #lang racket/base
(require (prefix-in args: "args.rkt")) (require (prefix-in args: "args.rkt"))
(define (main args) (define (main args)
(args:check-args args)) (args:check-args args))
(main (current-command-line-arguments)) (main (current-command-line-arguments))

283
util.rkt
View File

@ -1,142 +1,141 @@
#lang racket/base #lang racket/base
(require (prefix-in list: racket/list) (require (prefix-in list: racket/list)
(prefix-in file: racket/file) (prefix-in file: racket/file)
(prefix-in string: racket/string) (prefix-in string: racket/string)
(prefix-in config: "config.rkt") (prefix-in config: "config.rkt")
(prefix-in messages: "messages.rkt")) (prefix-in messages: "messages.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
(define (check-for-file) (define (check-for-program-file)
(file-exists? config:path-to-file)) (file-exists? config:path-to-file))
(define (create-file) (define (create-program-file)
(let ([opened-file (let ([opened-file (open-output-file config:path-to-file
(open-output-file config:path-to-file #:mode 'text
#:mode 'text #:exists 'truncate)])
#:exists 'truncate)]) (close-output-port opened-file))
(close-output-port opened-file)) (file-or-directory-permissions config:path-to-file #o600))
(file-or-directory-permissions config:path-to-file #o600))
(define (check-for-program-directory)
(define (check-for-directory) (directory-exists? config:program-directory))
(directory-exists? config:program-directory))
(define (create-program-directory)
(define (create-directory) (make-directory config:program-directory)
(make-directory config:program-directory) (file-or-directory-permissions config:program-directory #o700))
(file-or-directory-permissions config:program-directory #o700))
(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
;; Just so I don't have to keep typing "#:mode...#:line-mode..." every time (define (file->string-list config:path-to-file-to-file)
(define (file->string-list config:path-to-file-to-file) (let ([todo-list (file:file->lines config:path-to-file-to-file
(let ([todo-list (file:file->lines config:path-to-file-to-file #:mode 'text
#:mode 'text #:line-mode 'any)])
#:line-mode 'any)]) todo-list))
todo-list))
(define (list-empty? lst)
(define (list-empty? lst) (list:empty? (file->string-list lst)))
(list:empty? (file->string-list lst)))
;; Find out which item is being removed by scooping up
;; Find out which item is being removed by scooping up ;; the number the user entered in the command line
;; the number the user entered in the command line ;; arguments
;; argument (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 they are ;; are actually viewing the list numbers as human numbers
;; are actually viewing the list numbers as human numbers ;; so (actual-number +1)
;; so (actual-number +1) (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-in-quotes args) (display (string-append "\"" args "\"")))
(display (string-append "\"" args "\"")))
(define (prefix-item-with-period lst)
(define (prefix-with-period lst) (string-append lst ". "))
(string-append lst ". "))
(define (prefix-item-with-number lst)
(define (prefix-with-number lst) (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, as if it were nested, so that would be add1 here
;; list, as if it were nested, so that would be add1 here (map (compose prefix-item-with-period number->string add1)
(map (compose prefix-with-period number->string add1) (list:range (length lst)))
(list:range (length lst))) lst))
lst))
(define (display-prettified-program-file)
(define (display-prettified-list) (display
(display (string:string-join
(string:string-join (prefix-item-with-number (file->string-list config:path-to-file))
(prefix-with-number (file->string-list config:path-to-file)) "\n"
"\n" #:after-last "\n")))
#:after-last "\n")))
;; This one is pretty wild
;; This is a bit of ugly scheme sorcery (define (append-item-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)))))
(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) (surround-item-in-quotation-marks 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) (surround-item-in-quotation-marks args)
(surround-in-quotes args) (display-hash-ref messages:messages 'item-removed-suffix))
(display-hash-ref messages:messages 'item-removed-suffix))
(define (show-list-from-program-file)
(define (show-list) (cond [(and
(cond [(and (check-for-program-directory)
(check-for-directory) (check-for-program-file))
(check-for-file)) (if
(if ;; If file exists, see if it's empty, if so
;; If file exists, see if it's empty, if so ;; tell the user
;; tell the user (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) ;; If file isn't empty, display a pretty list
;; If file isn't empty, display a pretty list (display-prettified-program-file))]
(display-prettified-list))] ;; If file doesn't exist, tell the user
;; 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 (write-item-to-program-file args)
(define (add-item-to-file args) ;; Add item to end of list and write to file
;; Add item to end of list and write to file (let ([new-list (append-item-to-end args config:path-to-file)])
(let ([new-list (append-to-end args 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-added args)))
(display-item-added args)))
(define (add-item-to-list args)
(define (add-item args) (if (and
(if (and (check-for-program-directory)
(check-for-directory) (check-for-program-file))
(check-for-file)) ;; The cdr here prevents the command line argument "add" from being added to the file
;; The cdr here prevents the command line argument "add" from being added to the file (write-item-to-program-file (string:string-join (cdr (vector->list args))))
(add-item-to-file (string:string-join (cdr (vector->list args)))) (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-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 args) (cond [(list-empty? config:path-to-file)
(cond [(list-empty? config:path-to-file) (display-hash-ref messages:messages 'empty-todo-list)]
(display-hash-ref messages:messages 'empty-todo-list)] [(and
[(and (check-for-program-directory)
(check-for-directory) (check-for-program-file))
(check-for-file)) (remove-item-from-program-file (vector-ref args 1))]
(remove-item-from-file (vector-ref args 1))] [(and (not (check-for-program-directory)) (not (check-for-program-file)))
[(and (not (check-for-directory)) (not (check-for-file))) (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))]))