changed a lot of functions

main
m455 2019-11-03 21:32:11 -05:00
parent 91eed7fee9
commit b69788bd91
5 changed files with 116 additions and 133 deletions

View File

@ -11,42 +11,36 @@
(define (check-args args) (define (check-args args)
(let ([args-length (vector-length args)]) (let ([args-length (vector-length args)])
(cond (cond [(equal? args-length 0)
[(equal? args-length 0)
(util:display-hash-ref messages:messages 'show-usage)] (util:display-hash-ref messages:messages 'show-usage)]
;; ls ;; ls
[(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-from-program-file)] (util:show-list-from-file config:path-to-list-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-to-list args)] (util:add-item-to-list config:path-to-list-file args)]
;; rm ;; rm
[(and [(and (equal? args-length 2)
(equal? args-length 2)
(equal? (vector-ref args 0) config:remove-command) (equal? (vector-ref args 0) config:remove-command)
(real? (string->number (vector-ref args 1))) (real? (string->number (vector-ref args 1)))
(or (positive? (string->number (vector-ref args 1))) (or (positive? (string->number (vector-ref args 1)))
(zero? (string->number (vector-ref args 1)))) (zero? (string->number (vector-ref args 1))))
;; Length subtract one because the numbering starts at zero ;; Length subtract one because the numbering starts at zero
(not (> (string->number (vector-ref args 1)) (sub1 (length (util:file->string-list config:path-to-file))))) (not (> (string->number (vector-ref args 1)) (sub1 (length (util:file->string-list config:path-to-list-file))))))
(util:remove-item-from-list args))] (util:remove-item-from-list config:path-to-list-file args)]
;; init ;; init
[(and [(and (equal? args-length 1)
(equal? args-length 1)
(equal? (vector:vector-member config:initialize-command args) 0)) (equal? (vector:vector-member config:initialize-command args) 0))
(init:initialize)] (init:initialize)]
;; help ;; help
[(and [(and (equal? args-length 1)
(equal? args-length 1)
(member (vector-ref args 0) config:help-command)) (member (vector-ref args 0) config:help-command))
(util:display-hash-ref messages:messages 'show-help)] (util:display-hash-ref messages:messages 'show-help)]

View File

@ -2,11 +2,15 @@
(provide (all-defined-out)) (provide (all-defined-out))
(define program-name "rodo") (define program-name "rodo")
(define program-directory (path->string (expand-user-path "~/.rodo/"))) (define program-directory
(define program-file "todo.txt") (path->string
(expand-user-path
(string-append "~/." program-name "/"))))
(define list-file "todo.txt")
(define remove-command "rm") (define remove-command "rm")
(define add-command "add") (define add-command "add")
(define list-command "ls") (define list-command "ls")
(define initialize-command "init") (define initialize-command "init")
(define help-command '("-h" "--help")) (define help-command '("-h" "--help"))
(define path-to-file (string-append program-directory program-file)) (define path-to-list-file
(string-append program-directory list-file))

View File

@ -10,27 +10,23 @@
(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 "> ")
(let (let ([user-input (read-line)])
([user-input (read-line)]) (cond [(member user-input (hash-ref messages:y/n 'yes))
(cond (begin
[(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-program-directory) (util:create-program-directory-700 config:program-directory)
(util:create-program-file) (util:create-list-file-600 config:path-to-list-file)
(if (and (if (and (util:program-directory-exists?)
(util:check-for-program-directory) (util:list-file-exists?))
(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)))]
[(member user-input (hash-ref messages:y/n 'no)) [(member user-input (hash-ref messages:y/n 'no))
(util:display-hash-ref messages:messages 'terminating)] (util:display-hash-ref messages:messages 'terminating)]
[else [else
(init-prompt messages:messages 'choose-y/n)]))) (init-prompt messages:messages 'choose-y/n)])))
(define (initialize) (define (initialize)
(if (util:check-for-program-file) (if (util:list-file-exists?)
(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

@ -31,7 +31,7 @@
(format "~a\n" config:initialize-command) (format "~a\n" config:initialize-command)
"====\n" "====\n"
(format "Creates ~a in ~a. This is where your todo list will be stored.\n\n" (format "Creates ~a in ~a. This is where your todo list will be stored.\n\n"
config:program-file config:list-file
config:program-directory) config:program-directory)
"Example:\n" "Example:\n"
@ -80,31 +80,31 @@
config:program-name) config:program-name)
'creating (format "> Creating ~a in ~a.\n" 'creating (format "> Creating ~a in ~a.\n"
config:program-file config:list-file
config:program-directory) config:program-directory)
'creation-error (string-append 'creation-error (string-append
(format "> Error: Could not create a(n) ~a in ~a.\n" (format "> Error: Could not create a(n) ~a in ~a.\n"
config:program-file config:list-file
config:program-directory) config:program-directory)
"> This might be due to directory permissions.\n") "> This might be due to directory permissions.\n")
'file-already-exists (format "> Error: ~a already exists in ~a~a.\n" 'file-already-exists (format "> Error: ~a already exists in ~a~a.\n"
config:program-name config:program-name
config:program-directory config:program-directory
config:program-file) config:list-file)
'successfully-created (format "> ~a has been successfully created in ~a.\n" 'successfully-created (format "> ~a has been successfully created in ~a.\n"
config:program-directory config:program-directory
config:program-file) config:list-file)
'file-not-found (format "> Error: Could not find ~a~a\n" 'file-not-found (format "> Error: Could not find ~a~a\n"
config:program-directory config:program-directory
config:program-file) config:list-file)
'init-y/n (string-append 'init-y/n (string-append
(format "> ~a will be created in ~a.\n" (format "> ~a will be created in ~a.\n"
config:program-file config:list-file
config:program-directory) config:program-directory)
"> Are you sure you want to continue? [y/n]\n") "> Are you sure you want to continue? [y/n]\n")

113
util.rkt
View File

@ -8,33 +8,33 @@
(provide (all-defined-out)) (provide (all-defined-out))
(define (check-for-program-file) (define (list-file-exists?)
(file-exists? config:path-to-file)) (file-exists? config:path-to-list-file))
(define (create-program-file) (define (program-directory-exists?)
(let ([opened-file (open-output-file config:path-to-file (directory-exists? config:program-directory))
(define (create-list-file-600 a-file)
(let ([opened-file (open-output-file a-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 a-file #o600))
(define (check-for-program-directory) (define (create-program-directory-700 a-directory)
(directory-exists? config:program-directory)) (make-directory a-directory)
(file-or-directory-permissions a-directory #o700))
(define (create-program-directory)
(make-directory config:program-directory)
(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)))
(define (file->string-list config:path-to-file-to-file) (define (file->string-list a-file)
(let ([todo-list (file:file->lines config:path-to-file-to-file (let ([todo-list (file:file->lines a-file
#:mode 'text #:mode 'text
#:line-mode 'any)]) #:line-mode 'any)])
todo-list)) todo-list))
(define (list-empty? lst) (define (list-file-empty? lst)
(list:empty? (file->string-list lst))) (list:empty? (file->string-list lst)))
(define (get-removed-item lst args) (define (get-removed-item lst args)
@ -43,26 +43,21 @@
(define (surround-with-quotation-marks args) (define (surround-with-quotation-marks args)
(display (string-append "\"" args "\""))) (display (string-append "\"" args "\"")))
(define (list->dotted-list lst) (define (list->list-of-dotted-numbers lst)
(string-append lst "." " ")) (map (lambda (x) (string-append x ". "))
(map number->string (list:range (length lst)))))
(define (list->numbered-list lst) (define (list->numbered-list lst)
;; Take the list made in the first (map), which is (map (lambda (x y) (string-append x y))
;; '(0 1 2 ...), and append that to each item in a list (list->list-of-dotted-numbers lst)
(map string-append
;; Note: compose starts from the last element in it's
;; list. In this case, it starts at (number->string).
(map (compose1 list->dotted-list number->string)
(list:range (length lst)))
lst)) lst))
(define (display-prettified-program-file) (define (file->numbered-list-as-lines a-file)
(display (string:string-join (string:string-join (list->numbered-list (file->string-list a-file))
(list->numbered-list (file->string-list config:path-to-file))
"\n" "\n"
#:after-last "\n"))) #:after-last "\n"))
(define (append-item-to-end args lst) (define (append-item-to-list 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)
@ -75,57 +70,51 @@
(surround-with-quotation-marks args) (surround-with-quotation-marks 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-from-file a-file)
(cond [(and (cond [(and (program-directory-exists?)
(check-for-program-directory) (list-file-exists?))
(check-for-program-file)) (if (list-file-empty? a-file)
(if
(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 (file->numbered-list-as-lines a-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-file a-file args)
;; Add an item to the end of a list and write to a file (let ([new-list (append-item-to-list args a-file)])
(let ([new-list (append-item-to-end args config:path-to-file)]) (file:display-to-file (string:string-join new-list "\n")
(file:display-to-file a-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 (display-item-added args))
(display-item-added args)))
(define (add-item-to-list args) ;; The cdr here prevents user commands, such as "add" from being added to the file
(if (and (check-for-program-directory) (define (add-item-to-list a-file args)
(check-for-program-file)) (if (and (program-directory-exists?)
;; The cdr here prevents user commands, such as "add" (list-file-exists?))
;; from being added to the file (write-item-to-file a-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-list-file a-file args)
(let* ([removed-item (get-removed-item config:path-to-file args)] (let* ([removed-item (get-removed-item a-file args)]
[new-list (remove removed-item (file->string-list config:path-to-file))]) [new-list (remove removed-item (file->string-list a-file))])
(file:display-to-file (file:display-to-file (string:string-join new-list "\n")
(string:string-join new-list "\n") a-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 a-file args)
(cond [(list-empty? config:path-to-file) (cond [(list-file-empty? a-file)
(display-hash-ref messages:messages 'empty-todo-list)] (display-hash-ref messages:messages 'empty-todo-list)]
[(and (check-for-program-directory) [(and (program-directory-exists?)
(check-for-program-file)) (list-file-exists?))
(remove-item-from-program-file (vector-ref args 1))] (remove-item-from-list-file a-file (vector-ref args 1))]
[(and (not (check-for-program-directory)) [(and (not (program-directory-exists?))
(not (check-for-program-file))) (not (list-file-exists?)))
(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))]))