Updated code

cleanup
m455 2020-06-20 00:21:19 -04:00
parent 104e44b769
commit 60ba955cd9
1 changed files with 163 additions and 115 deletions

View File

@ -6,157 +6,207 @@
racket/string) racket/string)
;; ------------------------------------------------ ;; ------------------------------------------------
;; values ;; OLD values
;; ------------------------------------------------ ;; ------------------------------------------------
(define nicethings-string ".nicethings") (define nicethings-string ".nicethings")
(define nicethings-path (build-path (find-system-path 'home-dir) nicethings-string)) (define nicethings-path (build-path (find-system-path 'home-dir) nicethings-string))
;; ------------------------------------------------
;; values
;; ------------------------------------------------
(define help-command "help")
(define ls-command "ls")
(define rm-command "rm")
(define add-command "add")
(define program-name "nicethings")
(define program-name-dotted (string-append "." program-name))
(define home-directory-path (find-system-path 'home-dir))
(define program-path (build-path home-directory-path program-name-dotted))
(define correct-permissions 420) ;; -rw-r--r-- permissions
(define message-prefix "> ")
;; ------------------------------------------------ ;; ------------------------------------------------
;; messages ;; messages
;; ------------------------------------------------ ;; ------------------------------------------------
(define messages (define messages
(hash (hash
'not-found "> '~a' wasn't found." 'error-add (list (format "Error: No arguments were found after the '~a' command." add-command)
'item-not-found (list "> Error: Item not found." (format "Suggestion: Try using a quoted argument after the '~a' command." ls-command)
"> Try using the 'ls' command to see which number correlates to which message in your list.") (format "Example: ~a add \"Go for a walk\"" program-name))
'empty-list "> Your list of nice things is empty."
'not-found-prompt "> You will need it to add new messages to nicethings.\n> Do you want to create it? [y/n]\n> " 'item-not-found (list "Error: Item not found."
'wrong-permissions "> '~a''s permissions are incorrect." (format "Suggestion: Try using the '~a' command to see which number " ls-command)
'wrong-permissions-prompt "> You will need the permissions to be fixed before using nicethings.\n> Do you want to fix them? [y/n]\n> " " correlates to which message in your list."
'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings." (format "Example: ~a ~a" program-name ls-command))
'try (list "> For usage help, try running the following command:"
"nicethings --help") 'error-rm (list (format "Error: No arguments were found after the '~a' command." rm-command)
'cancel "> Cancelled." (format "Suggestion: Try using a number after the '~a' command." rm-command)
'file-created "> '~a' was successfully created." (format "Example: ~a ~a 2" program-name rm-command)
'permissions-fixed "> '~a''s permissions were successfully fixed." ""
'not-an-option "> Error: '~a' is not an option." (format "Note: You may need to run the '~a' command to see which " ls-command)
'add-expected-arg (list "> Error: Found 'add', but no arguments were found." " number correlates to which item in your list."
"> The 'add' command expects one quoted argument after it." (format "Example: ~a ~a" program-name ls-command))
"> Example: nicethings add \"You are beautiful\".")
'rm-expected-arg (list "> Error: Found 'rm', but no arguments were found." 'error-usage (list "Error: Incorrect usage."
"> The 'rm' command expects one number as an argument after it." (format "Suggestions: Try running the '~a' command." help-command)
"> Example: nicethings rm 2" (format "Example: ~a ~a" program-name help-command))
"> Note: You may need to use the 'ls' command to see which number correlates to which message in your list.")
'ls-expected-no-args (list "> Error: Found 'ls', but also found other arguments." 'error-ls (list (format "Error: Found arguments after the '~a' command." ls-command)
"> The 'ls' command expects no arguments after it." (format "Suggestions: Try using '~a' without any arguments." ls-command)
"> Example:" (format "Example: ~a ~a" program-name ls-command))
"nicethings ls")
'added "> Added '~a' to your list of nice things." 'error-fake-file (list (format "Error: A '~a' directory was found in your home directory." program-name-dotted)
'removed "> Removed '~a' from your list of nice things.")) (format "Suggestion 1: Move the '~a' directory out of your " program-name-dotted)
(format " home directory before using ~a." program-name)
(format "Suggestion 2: Rename the '~a' directory before using ~a." program-name-dotted program-name))
'error-permissions-prompt (list (format "Error: '~a''s permissions are incorrect." program-path)
"Would you like to fix them? [y/n]")
'cancel-creation (list (format "Cancelled the creation of ~a." program-path))
'permissions-fixed (list (format "'~a''s permissions were successfully fixed." program-path))
'file-created (list (format "'~a' was successfully created." program-path))
'not-found-prompt (list (format "'~a' wasn't found." program-path)
"Would you like to create it? [y/n]")
'error-option (list "Error: '~a' is not an option.")
'empty-list (list "There is nothing in your list.")
'added (list "'~a' was added to your list.")
'removed (list "'~a' was removed from your list.")))
;; ------------------------------------------------ ;; ------------------------------------------------
;; helpers ;; helpers
;; ------------------------------------------------ ;; ------------------------------------------------
(define (messages-ref key)
(map (lambda (element) (string-append message-prefix element))
(hash-ref messages key)))
(define-syntax-rule (displayln-message-format key string ...)
(let ([string-to-format (string-join (messages-ref key) "\n")])
(displayln (format string-to-format string ...))))
(define (displayln-message-list key)
(let ([listof-strings (messages-ref key)])
(for ([string listof-strings])
(displayln string))))
(define (displayln-for . strings) (define (displayln-for . strings)
(for ([string strings]) (for ([string strings])
(displayln string))) (displayln string)))
(define (display-message-list key) (define (file-has-correct-permissions? file)
(apply displayln-for (messages-ref key))) (equal? correct-permissions (file-or-directory-permissions file 'bits)))
(define-syntax-rule (displayln-format str ...)
(displayln (format str ...)))
(define (messages-ref key)
(hash-ref messages key))
(define (file-has-420-permissions? file)
(equal? 420 (file-or-directory-permissions file 'bits)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; repair ;; repair
;; ------------------------------------------------ ;; ------------------------------------------------
(define (repair/not-an-option user-input) (define (repair/not-an-option user-input)
(displayln-format (messages-ref 'not-an-option) user-input) (displayln-message-format 'error-option user-input)
(exit)) (exit))
(define (repair/cancel) (define (repair/cancel)
(displayln (messages-ref 'cancel)) (displayln-message-list 'cancel-creation)
(exit)) (exit))
(define (repair/fix-permissions) (define (repair/fake-file)
(file-or-directory-permissions nicethings-path 420) (displayln-message-list 'error-fake-file)
(displayln-format (messages-ref 'permissions-fixed) nicethings-path)) (exit))
(define (repair/wrong-permissions) (define (repair/create-exit-mode key)
(display (messages-ref 'wrong-permissions-prompt)) (match key
[(or 'ls 'rm) (exit)]
['add 'do-nothing]))
(define (repair/fix-permissions key)
(file-or-directory-permissions program-path correct-permissions)
(displayln-message-list 'permissions-fixed)
(repair/create-exit-mode key))
(define (repair/wrong-permissions key)
(displayln-message-list 'error-permissions-prompt)
(display message-prefix)
(let ([user-input (read-line)]) (let ([user-input (read-line)])
(case (string->symbol user-input) (case (string->symbol user-input)
['y (repair/fix-permissions)] ['y (repair/fix-permissions key)]
['n (repair/cancel)] ['n (repair/cancel)]
[else (repair/not-an-option user-input)]))) [else (repair/not-an-option user-input)])))
(define (repair/create-file) (define (repair/create-file key)
(close-output-port (open-output-file nicethings-path)) (close-output-port (open-output-file program-path))
(file-or-directory-permissions nicethings-path 420) (file-or-directory-permissions program-path correct-permissions)
(displayln-format (messages-ref 'file-created) nicethings-path)) (displayln-message-list 'file-created)
(repair/create-exit-mode key))
(define (repair/not-found) (define (repair/not-found key)
(display (messages-ref 'not-found-prompt)) (displayln-message-list 'not-found-prompt)
(display message-prefix)
(let ([user-input (read-line)]) (let ([user-input (read-line)])
(case (string->symbol user-input) (case (string->symbol user-input)
['y (repair/create-file)] ['y (repair/create-file key)]
['n (repair/cancel)] ['n (repair/cancel)]
[else (repair/not-an-option user-input)]))) [else (repair/not-an-option user-input)])))
(define (repair) (define (repair key)
(cond (cond
;; Check for a "fake" '.nicethings' file, which is a directory named '.nicethings' [(directory-exists? program-path)
[(directory-exists? nicethings-path) (repair/fake-file)]
(begin (displayln-format (messages-ref 'fake-file-found) nicethings-path)
(exit))] [(not (file-exists? program-path))
;; Check for a missing '.nicethings' file (repair/not-found key)]
[(not (file-exists? nicethings-path))
(begin (displayln-format (messages-ref 'not-found) nicethings-path) [(not (file-has-correct-permissions? program-path))
(repair/not-found))] (repair/wrong-permissions key)]
;; Check for incorrect permissions on '.nicethings' file
[(not (file-has-420-permissions? nicethings-path))
(begin (displayln-format (messages-ref 'wrong-permissions) nicethings-path)
(repair/wrong-permissions))]
[else 'do-nothing])) [else 'do-nothing]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; ls ;; ls
;; ------------------------------------------------ ;; ------------------------------------------------
(define (ls/display-list listof-nicethings) (define (ls/display-list listof-items)
;; add1 starts the listof-numbers at 1 instead of 0 ;; add1 starts the listof-numbers at 1 instead of 0
(let* ([listof-numbers (map add1 (range (length listof-nicethings)))] (let* ([listof-numbers (map add1 (range (length listof-items)))]
[listof-number-strings (map number->string listof-numbers)] [listof-number-strings (map number->string listof-numbers)]
[combine-lists (lambda (a b) (string-append a ". " b))] [combine-lists (lambda (a b) (string-append a ". " b))]
[listof-numbered-items (map combine-lists [listof-numbered-items (map combine-lists
listof-number-strings listof-number-strings
listof-nicethings)]) listof-items)])
(for ([item listof-numbered-items]) (for ([item listof-numbered-items])
(displayln item)))) (displayln item))))
(define (ls) (define (ls)
(repair) (repair 'ls)
(let ([listof-nicethings (file->lines nicethings-path)]) (let ([listof-items (file->lines program-path)])
(if (null? listof-nicethings) (if (null? listof-items)
(displayln (messages-ref 'empty-list)) (displayln-message-list 'empty-list)
(ls/display-list listof-nicethings)))) (ls/display-list listof-items))))
;; ------------------------------------------------ ;; ------------------------------------------------
;; rm ;; rm
;; ------------------------------------------------ ;; ------------------------------------------------
(define (rm/remove-item listof-nicethings item-number) (define (rm/remove-item listof-items item-number)
(let* ([item-to-remove (list-ref listof-nicethings item-number)] (let* ([item-to-remove (list-ref listof-items item-number)]
[list-without-item (remove item-to-remove listof-nicethings)]) [list-without-item (remove item-to-remove listof-items)])
(display-lines-to-file list-without-item (display-lines-to-file list-without-item
nicethings-path program-path
#:exists 'truncate) #:exists 'truncate)
(displayln-format (messages-ref 'removed) item-to-remove))) (displayln-message-format 'removed item-to-remove)))
(define (rm string) (define (rm string)
(repair) (repair 'rm)
(let* ([listof-nicethings (file->lines nicethings-path)] (let* ([listof-items (file->lines program-path)]
;; subtract 1 because the index starts at ;; subtract 1 because the index starts at
;; 0 under the hood, but the numbers presented from 'ls' ;; 0 under the hood, but the numbers presented from 'ls'
;; start at 1. ;; start at 1.
[item-number (string->number string)] [item-number (string->number string)]
[item-number-sub1 (sub1 item-number)] [item-number-sub1 (sub1 item-number)]
[list-length (length listof-nicethings)]) [list-length (length listof-items)])
(if (and (not (null? listof-nicethings)) (if (and (not (null? listof-items))
(number? item-number) (number? item-number)
(positive? item-number) (positive? item-number)
;; 1 less than length, because we want to ;; 1 less than length, because we want to
@ -176,8 +226,8 @@
;; list length 3, then that would be ;; list length 3, then that would be
;; an error ;; an error
(< item-number-sub1 list-length)) (< item-number-sub1 list-length))
(rm/remove-item listof-nicethings item-number-sub1) (rm/remove-item listof-items item-number-sub1)
(display-message-list 'item-not-found)))) (displayln-message-list 'item-not-found))))
;; ------------------------------------------------ ;; ------------------------------------------------
;; add ;; add
@ -186,13 +236,13 @@
;; are multiple newline characters. This ensures ;; are multiple newline characters. This ensures
;; there is only one newline character. ;; there is only one newline character.
(define (add string) (define (add string)
(repair) (repair 'add)
(let* ([string-no-newline (string-replace string "\n" "")] (let* ([string-no-newline (string-replace string "\n" "")]
[string-newline (string-append string-no-newline "\n")]) [string-newline (string-append string-no-newline "\n")])
(display-to-file string-newline (display-to-file string-newline
nicethings-path program-path
#:exists 'append) #:exists 'append)
(displayln-format (messages-ref 'added) string-no-newline))) (displayln-message-format 'added string-no-newline)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; random message ;; random message
@ -201,12 +251,12 @@
(build-path home-directory nicethings-string)) (build-path home-directory nicethings-string))
(define (random-message) (define (random-message)
(let* ([root (find-system-path 'sys-dir)] (let* ([root (find-system-path 'sys-dir)] ;; /
[root-home (build-path root "home")] [root-home (build-path root "home")] ;; /home
[listof-homes (directory-list root-home #:build? #t)] [listof-homes (directory-list root-home #:build? #t)]
[paths-to-nicethings (map random-message/append-nicethings-file listof-homes)] [paths-to-nicethings (map random-message/append-nicethings-file listof-homes)]
[directories-with-nicethings (filter file-exists? paths-to-nicethings)] [directories-with-nicethings (filter file-exists? paths-to-nicethings)]
[directories-with-420 (filter file-has-420-permissions? directories-with-nicethings)] [directories-with-420 (filter file-has-correct-permissions? directories-with-nicethings)]
[listof-nicethings (apply append (map file->lines directories-with-420))] [listof-nicethings (apply append (map file->lines directories-with-420))]
[list-length (length listof-nicethings)]) [list-length (length listof-nicethings)])
(when (not (zero? list-length)) (when (not (zero? list-length))
@ -216,23 +266,18 @@
;; ------------------------------------------------ ;; ------------------------------------------------
;; help ;; help
;; ------------------------------------------------ ;; ------------------------------------------------ (define (help) (displayln-for "Usage:"
(define (help) (format " ~a [<command>] [<args>]" program-name)
(displayln-for
"Usage:"
" nicethings [<command>] [<args>]"
"" ""
"Commands:" "Commands:"
" No command - Print a random nice thing." (format " ~a - Adds an item to your list." add-command)
" add - Add a message to the list of nice things." (format " ~a - Prints a numbered list of your items." ls-command)
" ls - Print a numbered list of the nice things you have added." (format " ~a - Removes an item from your list." rm-command)
" rm - Remove a message you have added from the list of nice things."
"" ""
"Examples:" "Examples:"
" nicethings" (format " ~a ~a \"Go for a walk\"" program-name add-command)
" nicethings add \"You are beautiful\"" (format " ~a ~a" program-name ls-command)
" nicethings ls" (format " ~a ~a 2" program-name rm-command)))
" nicethings rm 2"))
(define (process-args vectorof-args) (define (process-args vectorof-args)
(define (args-ref number) (define (args-ref number)
@ -241,16 +286,19 @@
;; Proper usage ;; Proper usage
[(or '#("-h") [(or '#("-h")
'#("--help") '#("--help")
'#("help")) (help)] '#(help-command)) (help)]
[(vector "add" _) (add (args-ref 1))] [(vector add-command _) (add (args-ref 1))]
[(vector "rm" _) (rm (args-ref 1))] [(vector rm-command _) (rm (args-ref 1))]
[(vector "ls") (ls)] [(vector ls-command) (ls)]
[(vector) (random-message)] [(vector) (random)]
;; Improper usage (Give the user hints if part of the usage is correct) ;; Improper usage
[(vector "ls" _) (display-message-list 'ls-expected-no-args)] ;; This is checked so we can give the user hints on how to
[(vector "add") (display-message-list 'add-expected-arg)] ;; use the software if they have part of the command
[(vector "rm") (display-message-list 'rm-expected-arg)] ;; correct
[(vector _ ...) (display-message-list 'try)])) [(vector ls-command _) (displayln-message-list 'error-ls)]
[(vector add-command) (displayln-message-list 'error-add)]
[(vector rm-command) (displayln-message-list 'error-rm)]
[(vector _ ...)) (displayln-message-list 'error-usage)]))
(define (main vectorof-args) (define (main vectorof-args)
(process-args vectorof-args)) (process-args vectorof-args))