cleaned up messages.rkt

main
m455 2019-08-25 18:37:34 -04:00
parent 9abb50b9bd
commit 7376d8efff
2 changed files with 156 additions and 180 deletions

View File

@ -1,153 +1,130 @@
#lang racket/base #lang racket/base
(require (prefix-in config: "config.rkt")) (require (prefix-in config: "config.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
(define messages (define messages
(hash (hash
'show-help (string-append 'show-help (string-append
"Usage\n" "Usage\n"
"=====\n" "=====\n"
"If you are using the " (format "If you want to use the ~a or ~a commands, follow the structure below:\n"
"<" config:initialize-command ">" " or " config:initialize-command
"<" config:list-command ">" " commands, follow the structure below:\n" config:list-command)
config:program-name " <command>\n" (format "~a <command>\n\n"
"\n" config:program-name)
"If you are using the " (format "If you want to use the ~a or ~a commands, follow the structure below:\n"
"<" config:add-command ">" " or " config:add-command
"<" config:remove-command ">" " commands, follow the structure below:\n" config:remove-command)
config:program-name " <command> [argument]\n" (format "~a <command> [argument]\n\n" config:program-name)
"\n"
"Note: Replace <command> with one of the Commands below without the surrounding \"<\" and \">\".\n\n"
"Commands\n"
"========\n" "Commands\n"
"========\n"
"<" config:initialize-command ">" ":\n" (format "This section describes the commands available for ~a.\n\n" config:program-name)
"Create a file in " (format "Note: The commands mentioned in this section should replace the \"<command>\" filler text found in the Usage section above.\n\n")
config:program-directory
config:program-file ;; init
" where your to-do list will be stored\n" (format "~a\n" config:initialize-command)
"Example:\n" "====\n"
config:program-name " " (format "Creates a file in ~a~a. This is where your to-do list will be stored.\n\n"
config:initialize-command "\n" config:program-directory
"\n" config:program-file)
"<" config:list-command ">" ":\n" "Example:\n"
"Displays items in the list\n" (format "~a ~a\n\n" config:program-name config:initialize-command)
"Example:\n"
config:program-name " " ;; ls
config:list-command "\n" (format "~a\n" config:list-command)
"\n" "====\n"
"Displays items in your to-do list.\n\n"
"<" config:add-command ">" " "
"[argument]:\n" "Example:\n"
"Adds an item to the list\n" (format "~a ~a\n\n" config:program-name config:list-command)
"Example:\n"
config:program-name " " (format "~a [argument(s)]\n" config:add-command)
config:add-command " " "=================\n"
"bread\n" "Adds an item to the list.\n\n"
config:program-name " "
config:add-command " " "Examples:\n"
"this is an unquoted task\n" (format "~a ~a this is an unquoted item\n" config:program-name config:add-command)
config:program-name " " (format "~a ~a \"this is a quoted item\"\n\n" config:program-name config:add-command)
config:add-command " "
"\"this is a quoted task which isn't any different from an unquoted task\"\n" (format "~a [argument]\n" config:remove-command)
"\n" "=============\n"
"Removes an item from the list\n\n"
"<" config:remove-command ">" " "
"[argument]:\n" "Example:\n"
"Removes an item from the list\n" (format "~a ~a 1\n\n" config:program-name config:remove-command)
"Example:\n"
config:program-name " " config:remove-command " " "1\n" (format "Note: You may have to run `~a ~a` to see which number corresponds to which item in your to-do list."
"\n" config:program-name
config:list-command)
"Note:\n" " "
"You may have to run " "In the example above, the first item was removed from the to-do list.\n\n"
"`" config:program-name " " config:list-command "`" " "
"see which number corresponds to which item in the list." "Can't see the whole help message?\n"
" In the example above, the first item was removed from the list\n" "=================================\n"
"\n" (format "Try running `~a -h | less` so you can use the arrow keys to scrolll up and down."
config:program-name)
"Can't see the whole help message?\n" " "
"=================================\n" "When you want to quit, type `q` (without the grave accents).")
"Try running "
"`" config:program-name " -h | less` to use the arrow keys " 'empty-to-do-list "> There is nothing in your list \n"
"to scroll up and down through the help message, then when you "
"want to quit, just type `q` (without the grave accents)\n") 'show-usage (format "> For usage type `~a -h` or `~a --help`\n"
config:program-name
'empty-to-do-list "> There is nothing in your list \n" config:program-name)
'show-usage (string-append 'creating (format "> Creating a(n) ~a file in ~a\n"
"> For usage type " config:program-file
"`" config:program-name " -h`" config:program-directory)
" or "
"`" config:program-name " --help`\n") 'creation-error (string-append
(format "> Error: Could not create a(n) ~a in ~a.\n"
'creating (string-append config:program-file
"> Creating " config:program-directory)
config:program-file "> This might be due to directory permissions\n")
" file in "
config:program-directory "\n") 'file-already-exists (format "> Error: ~a already exists in ~a~a.\n"
config:program-name
'creation-error (string-append config:program-directory
"> Error: Could not create " config:program-file)
config:program-file
" in " 'successfully-created (format "> ~a~a has been successfully created\n"
config:program-directory config:program-directory
".\n" config:program-file)
"> This may be due to directory permissions\n")
'file-not-found (format "> Error: Could not find ~a~a\n"
'file-already-exists (string-append config:program-directory
"> Error: " config:program-file)
config:program-name
" already exists in " 'init-y/n (string-append
config:program-directory (format "> A ~a file will be created in the ~a directory.\n"
config:program-file "\n") config:program-file
config:program-directory)
'successfully-created (string-append "> Are you sure you want to continue? [y/n]\n")
"> "
config:program-directory 'try-init (format "> Try typing `~a ~a` to set it up (without the grave accents).\n"
config:program-file config:program-name
" has been successfully created\n") config:initialize-command)
'file-not-found (string-append 'terminating (format "> Exiting ~a\n" config:program-name)
"> Error: Could not find "
config:program-directory 'choose-y/n "> Error: Please choose y or n\n"
config:program-file "\n")
'not-in-list "> Error: Item does not exist\n"
'init-y/n (string-append
"> A " 'item-added-prefix "> Added "
config:program-file
" file will be created in the directory " 'item-added-suffix " to list\n"
config:program-directory "\n"
"> Are you sure you want to continue? [y/n]\n") 'item-removed-prefix "> Removed "
'try-init (string-append 'item-removed-suffix " from list\n"))
"> Try typing "
"`" config:program-name " init` " (define y/n (hash 'yes '("yes" "Yes" "y" "Y")
"to set it up\n") 'no '("no" "No" "n" "N")))
'terminating (string-append
"> Exiting "
config:program-name
"\n")
'choose-y/n "> Error: Please choose y or n\n"
'not-in-list "> Error: Item does not exist\n"
'item-added-prefix "> Added "
'item-added-suffix " to list\n"
'item-removed-prefix "> Removed "
'item-removed-suffix " from list\n"))
(define y/n
(hash
'yes '("yes" "Yes" "y" "Y")
'no '("no" "No" "n" "N")))

View File

@ -30,8 +30,8 @@
(define (file->string-list config:path-to-file-to-file) (define (file->string-list config:path-to-file-to-file)
(let ([to-do-list (file:file->lines config:path-to-file-to-file (let ([to-do-list (file:file->lines config:path-to-file-to-file
#:mode 'text #:mode 'text
#:line-mode 'any)]) #:line-mode 'any)])
to-do-list)) to-do-list))
(define (list-empty? lst) (define (list-empty? lst)
@ -61,11 +61,10 @@
(define (display-prettified-program-file) (define (display-prettified-program-file)
(display (string:string-join (display (string:string-join
(list->numbered-list (file->string-list config:path-to-file)) (list->numbered-list (file->string-list config:path-to-file))
"\n" "\n"
#:after-last "\n"))) #:after-last "\n")))
;; 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)))))
@ -81,45 +80,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-to-do-list) (display-hash-ref messages:messages 'empty-to-do-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 user commands, such as "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)