2020-06-15 17:55:24 +00:00
|
|
|
#lang racket/base
|
|
|
|
|
|
|
|
(require racket/file
|
|
|
|
racket/list
|
|
|
|
racket/match
|
|
|
|
racket/string)
|
2020-06-14 02:15:04 +00:00
|
|
|
|
2020-06-15 03:52:12 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; values
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define nicethings-string ".nicethings")
|
|
|
|
(define home-directory (find-system-path 'home-dir))
|
|
|
|
(define listof-home-contents (directory-list home-directory))
|
2020-06-15 17:55:24 +00:00
|
|
|
(define nicethings-path (build-path home-directory nicethings-string))
|
2020-06-15 03:52:12 +00:00
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; messages
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define messages
|
|
|
|
(hash
|
2020-06-15 15:07:48 +00:00
|
|
|
'not-found "> '~a' wasn't found."
|
2020-06-15 17:55:24 +00:00
|
|
|
'item-not-found (list "> Error: Item not found."
|
|
|
|
"> Try using the 'ls' command to see which number correlates to which message in your list.")
|
|
|
|
'empty-list "> Your list of nice things is empty."
|
2020-06-15 15:07:48 +00:00
|
|
|
'not-found-prompt "> You will need it to use nicethings.\n> Do you want to create it? [y/n]\n> "
|
|
|
|
'wrong-permissions "> '~a''s permissions are incorrect."
|
|
|
|
'wrong-permissions-prompt "> You will need the permissions to be fixed before using nicethings.\n> Do you want to fix them? [y/n]\n> "
|
|
|
|
'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings."
|
|
|
|
'try (list "> For usage help, try running the following command:"
|
|
|
|
"nicethings --help")
|
|
|
|
'cancel "> Cancelled."
|
|
|
|
'file-created "> '~a' was successfully created."
|
|
|
|
'permissions-fixed "> '~a''s permissions were successfully fixed."
|
|
|
|
'not-an-option "> Error: '~a' is not an option."
|
|
|
|
'add-expected-arg (list "> Error: Found 'add', but no arguments were found."
|
|
|
|
"> The 'add' command expects one quoted argument after it."
|
2020-06-15 17:55:24 +00:00
|
|
|
"> Example: nicethings add \"You are beautiful\".")
|
2020-06-15 15:07:48 +00:00
|
|
|
'rm-expected-arg (list "> Error: Found 'rm', but no arguments were found."
|
|
|
|
"> The 'rm' command expects one number as an argument after it."
|
2020-06-15 17:55:24 +00:00
|
|
|
"> Example: nicethings rm 2"
|
|
|
|
"> Note: You may need to use the 'ls' command to see which number correlates to which message in your list.")
|
2020-06-15 15:07:48 +00:00
|
|
|
'ls-expected-no-args (list "> Error: Found 'ls', but also found other arguments."
|
|
|
|
"> The 'ls' command expects no arguments after it."
|
|
|
|
"> Example:"
|
|
|
|
"nicethings ls")
|
2020-06-15 17:55:24 +00:00
|
|
|
'added "> Added '~a' to your list of nice things."
|
|
|
|
'removed "> Removed '~a' from your list of nice things."))
|
2020-06-14 02:15:04 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; helpers
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (displayln-for . strings)
|
|
|
|
(for ([string strings])
|
|
|
|
(displayln string)))
|
|
|
|
|
2020-06-15 17:55:24 +00:00
|
|
|
(define (display-message-list key)
|
|
|
|
(apply displayln-for (messages-ref key)))
|
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
(define-syntax-rule (displayln-format str ...)
|
|
|
|
(displayln (format str ...)))
|
|
|
|
|
|
|
|
(define (messages-ref key)
|
|
|
|
(hash-ref messages key))
|
2020-06-15 15:07:48 +00:00
|
|
|
|
|
|
|
(define (file-has-420-permissions? file)
|
|
|
|
(equal? 420 (file-or-directory-permissions file 'bits)))
|
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; repair
|
|
|
|
;; ------------------------------------------------
|
2020-06-15 03:52:12 +00:00
|
|
|
(define (repair/not-an-option user-input)
|
|
|
|
(displayln-format (messages-ref 'not-an-option) user-input)
|
|
|
|
(exit))
|
|
|
|
|
2020-06-14 20:39:48 +00:00
|
|
|
(define (repair/cancel)
|
2020-06-15 15:07:48 +00:00
|
|
|
(displayln (messages-ref 'cancel))
|
|
|
|
(exit))
|
|
|
|
|
|
|
|
(define (repair/fix-permissions)
|
2020-06-15 17:55:24 +00:00
|
|
|
(file-or-directory-permissions nicethings-path 420)
|
|
|
|
(displayln-format (messages-ref 'permissions-fixed) nicethings-path)
|
2020-06-15 15:07:48 +00:00
|
|
|
(exit))
|
|
|
|
|
|
|
|
(define (repair/wrong-permissions)
|
|
|
|
(display (messages-ref 'wrong-permissions-prompt))
|
|
|
|
(let ([user-input (read-line)])
|
|
|
|
(case (string->symbol user-input)
|
|
|
|
['y (repair/fix-permissions)]
|
|
|
|
['n (repair/cancel)]
|
|
|
|
[else (repair/not-an-option user-input)])))
|
|
|
|
|
|
|
|
(define (repair/create-file)
|
2020-06-15 17:55:24 +00:00
|
|
|
(close-output-port (open-output-file nicethings-path))
|
|
|
|
(displayln-format (messages-ref 'file-created) nicethings-path)
|
2020-06-14 20:39:48 +00:00
|
|
|
(exit))
|
|
|
|
|
2020-06-15 15:07:48 +00:00
|
|
|
(define (repair/not-found)
|
|
|
|
(display (messages-ref 'not-found-prompt))
|
2020-06-14 20:39:48 +00:00
|
|
|
(let ([user-input (read-line)])
|
|
|
|
(case (string->symbol user-input)
|
2020-06-15 15:07:48 +00:00
|
|
|
['y (repair/create-file)]
|
2020-06-15 03:52:12 +00:00
|
|
|
['n (repair/cancel)]
|
|
|
|
[else (repair/not-an-option user-input)])))
|
2020-06-14 20:39:48 +00:00
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
(define (repair)
|
2020-06-15 15:07:48 +00:00
|
|
|
(cond
|
|
|
|
;; Check for a "fake" '.nicethings' file, which is a directory named '.nicethings'
|
2020-06-15 17:55:24 +00:00
|
|
|
[(directory-exists? nicethings-path)
|
|
|
|
(begin (displayln-format (messages-ref 'fake-file-found) nicethings-path)
|
2020-06-15 15:07:48 +00:00
|
|
|
(exit))]
|
|
|
|
;; Check for a missing '.nicethings' file
|
2020-06-15 17:55:24 +00:00
|
|
|
[(not (file-exists? nicethings-path))
|
|
|
|
(begin (displayln-format (messages-ref 'not-found) nicethings-path)
|
2020-06-15 15:07:48 +00:00
|
|
|
(repair/not-found))]
|
|
|
|
;; Check for incorrect permissions on '.nicethings' file
|
2020-06-15 17:55:24 +00:00
|
|
|
[(not (file-has-420-permissions? nicethings-path))
|
|
|
|
(begin (displayln-format (messages-ref 'wrong-permissions) nicethings-path)
|
2020-06-15 15:07:48 +00:00
|
|
|
(repair/wrong-permissions))]
|
|
|
|
[else 'do-nothing]))
|
2020-06-15 03:52:12 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
2020-06-15 17:55:24 +00:00
|
|
|
;; ls
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (ls/display-list listof-nicethings)
|
|
|
|
;; add1 starts the listof-numbers at 1 instead of 0
|
|
|
|
(let* ([listof-numbers (map add1 (range (length listof-nicethings)))]
|
|
|
|
[listof-number-strings (map number->string listof-numbers)]
|
|
|
|
[combine-lists (lambda (a b) (string-append a ". " b))]
|
|
|
|
[listof-numbered-items (map combine-lists
|
|
|
|
listof-number-strings
|
|
|
|
listof-nicethings)])
|
|
|
|
(for ([item listof-numbered-items])
|
|
|
|
(displayln item))))
|
|
|
|
|
|
|
|
(define (ls)
|
|
|
|
(repair)
|
|
|
|
(let ([listof-nicethings (file->lines nicethings-path)])
|
|
|
|
(if (null? listof-nicethings)
|
|
|
|
(displayln (messages-ref 'empty-list))
|
|
|
|
(ls/display-list listof-nicethings))))
|
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; rm
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (rm/remove-item listof-nicethings item-number)
|
|
|
|
(let* ([item-to-remove (list-ref listof-nicethings item-number)]
|
|
|
|
[list-without-item (remove item-to-remove listof-nicethings)])
|
|
|
|
(display-lines-to-file list-without-item
|
|
|
|
nicethings-path
|
|
|
|
#:exists 'truncate)
|
|
|
|
(displayln-format (messages-ref 'removed) item-to-remove)))
|
|
|
|
|
|
|
|
(define (rm string)
|
|
|
|
(repair)
|
|
|
|
(let* ([listof-nicethings (file->lines nicethings-path)]
|
|
|
|
;; subtract 1 because the index starts at
|
|
|
|
;; 0 under the hood, but the numbers presented from 'ls'
|
|
|
|
;; start at 1.
|
|
|
|
[item-number (sub1 (string->number string))]
|
|
|
|
[list-length (length listof-nicethings)])
|
|
|
|
(if (and (not (null? listof-nicethings))
|
|
|
|
(number? item-number)
|
|
|
|
(positive? item-number)
|
|
|
|
;; less than length, because the index
|
|
|
|
;; starts at 0 under the hood
|
|
|
|
(< item-number list-length))
|
|
|
|
(rm/remove-item listof-nicethings item-number)
|
|
|
|
(display-message-list 'item-not-found))))
|
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; add
|
2020-06-15 03:52:12 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; The string-cleaned and -remade is incase there
|
|
|
|
;; are multiple newline characters. This ensures
|
|
|
|
;; there is only one newline character.
|
|
|
|
(define (add string)
|
|
|
|
(repair)
|
|
|
|
(let* ([string-no-newline (string-replace string "\n" "")]
|
2020-06-15 17:55:24 +00:00
|
|
|
[string-newline (string-append string-no-newline "\n")])
|
2020-06-15 03:52:12 +00:00
|
|
|
(display-to-file string-newline
|
2020-06-15 17:55:24 +00:00
|
|
|
nicethings-path
|
2020-06-15 03:52:12 +00:00
|
|
|
#:exists 'append)
|
|
|
|
(displayln-format (messages-ref 'added) string-no-newline)))
|
2020-06-14 02:15:04 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; random message
|
|
|
|
;; ------------------------------------------------
|
2020-06-15 17:55:24 +00:00
|
|
|
(define (random-message/append-nicethings-file home-directory)
|
|
|
|
(build-path home-directory nicethings-string))
|
|
|
|
|
2020-06-15 03:52:12 +00:00
|
|
|
(define (random-message)
|
|
|
|
(repair)
|
2020-06-15 17:55:24 +00:00
|
|
|
(let* ([root (find-system-path 'sys-dir)]
|
|
|
|
[root-home (build-path root "home")]
|
|
|
|
[listof-homes (directory-list root-home #:build? #t)]
|
|
|
|
[paths-to-nicethings (map random-message/append-nicethings-file listof-homes)]
|
2020-06-15 15:07:48 +00:00
|
|
|
[directories-with-nicethings (filter file-exists? paths-to-nicethings)]
|
2020-06-15 17:55:24 +00:00
|
|
|
[directories-with-420 (filter file-has-420-permissions? directories-with-nicethings)]
|
|
|
|
[listof-nicethings (apply append (map file->lines directories-with-420))]
|
|
|
|
[random-number (random (length listof-nicethings))]
|
|
|
|
[random-nicething (list-ref listof-nicethings random-number)])
|
|
|
|
(displayln random-nicething)))
|
2020-06-15 03:52:12 +00:00
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; help
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (help)
|
|
|
|
(displayln-for
|
|
|
|
"Usage:"
|
|
|
|
" town nicethings [<command>] [<args>]"
|
|
|
|
""
|
|
|
|
"Commands:"
|
2020-06-15 03:52:12 +00:00
|
|
|
" No command - Print a random nice thing."
|
|
|
|
" add - Add a message to the list of nice things."
|
|
|
|
" ls - Print a numbered list of the nice things you have added."
|
|
|
|
" rm - Remove a message you have added from the list of nice things."
|
2020-06-14 02:15:04 +00:00
|
|
|
""
|
|
|
|
"Examples:"
|
|
|
|
" town nicethings"
|
|
|
|
" town add \"You are beautiful\""
|
|
|
|
" town ls"
|
|
|
|
" town rm 2"))
|
|
|
|
|
|
|
|
(define (process-args vectorof-args)
|
2020-06-15 03:52:12 +00:00
|
|
|
(define (args-ref number)
|
|
|
|
(vector-ref vectorof-args number))
|
2020-06-14 02:15:04 +00:00
|
|
|
(match vectorof-args
|
2020-06-15 03:52:12 +00:00
|
|
|
;; Proper usage
|
|
|
|
[(or '#("-h")
|
|
|
|
'#("--help")
|
|
|
|
'#("help")) (help)]
|
|
|
|
[(vector "add" _) (add (args-ref 1))]
|
2020-06-15 17:55:24 +00:00
|
|
|
[(vector "rm" _) (rm (args-ref 1))]
|
|
|
|
[(vector "ls") (ls)]
|
2020-06-15 03:52:12 +00:00
|
|
|
[(vector) (random-message)]
|
|
|
|
;; Improper usage (Give the user hints if part of the usage is correct)
|
2020-06-15 17:55:24 +00:00
|
|
|
[(vector "ls" _) (display-message-list 'ls-expected-no-args)]
|
|
|
|
[(vector "add") (display-message-list 'add-expected-arg)]
|
|
|
|
[(vector "rm") (display-message-list 'rm-expected-arg)]
|
|
|
|
[(vector _ ...) (display-message-list 'try)]))
|
2020-06-14 02:15:04 +00:00
|
|
|
|
|
|
|
(define (main vectorof-args)
|
|
|
|
(process-args vectorof-args))
|
|
|
|
|
|
|
|
(main (current-command-line-arguments))
|