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-20 04:21:19 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-15 15:52:32 +00:00
|
|
|
;; Constants
|
2020-06-20 04:21:19 +00:00
|
|
|
;; ------------------------------------------------
|
2021-04-16 01:47:16 +00:00
|
|
|
(define command-help-1 "help")
|
|
|
|
(define command-help-2 "--help")
|
|
|
|
(define command-help-3 "-h")
|
|
|
|
(define command-init "init")
|
|
|
|
(define command-ls "ls")
|
|
|
|
(define command-rm "rm")
|
|
|
|
(define command-add "add")
|
2021-04-15 13:20:32 +00:00
|
|
|
(define program-name "nicethings")
|
|
|
|
(define program-file (string-append "." program-name))
|
|
|
|
(define program-path (path->string (build-path (find-system-path 'home-dir) program-file)))
|
|
|
|
(define read-permissions 420)
|
|
|
|
(define newline "\n")
|
|
|
|
(define double-newline "\n\n")
|
2020-06-20 04:21:19 +00:00
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-15 15:52:32 +00:00
|
|
|
;; Messages
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
(define messages
|
2021-04-15 13:20:32 +00:00
|
|
|
(hash 'error-too-many-arguments
|
|
|
|
(format (string-append "Error: Too many arguments." newline
|
|
|
|
"Try running '~a ~a' for more information.")
|
|
|
|
program-name
|
2021-04-16 01:47:16 +00:00
|
|
|
command-help-1)
|
2021-04-15 13:20:32 +00:00
|
|
|
|
|
|
|
'error-incorrect-usage
|
|
|
|
(format (string-append "Error: Incorrect usage." newline
|
|
|
|
"Try running '~a ~a' for more information.")
|
|
|
|
program-name
|
2021-04-16 01:47:16 +00:00
|
|
|
command-help-1)
|
2021-04-15 13:20:32 +00:00
|
|
|
|
|
|
|
'couldnt-find-file
|
|
|
|
(format (string-append "Error: Couldn't find ~a" newline
|
|
|
|
"Either the file doesn't exist, or the permissions are incorrect." newline
|
|
|
|
"If the file exists, try running chmod 600 ~a"
|
|
|
|
"If the file doesn't exist, try running ~a ~a")
|
|
|
|
program-path
|
|
|
|
program-path
|
|
|
|
program-name
|
2021-04-16 01:47:16 +00:00
|
|
|
command-init)
|
2021-04-15 13:20:32 +00:00
|
|
|
|
|
|
|
'error-something-exists
|
|
|
|
(format "Error: It looks like ~a already exists." program-path)
|
|
|
|
|
|
|
|
'error-file-doesnt-exist
|
|
|
|
(format (string-append "Error: '~a' doesn't exist." newline
|
|
|
|
"Try running '~a ~a'.")
|
|
|
|
program-path
|
|
|
|
program-name
|
2021-04-16 01:47:16 +00:00
|
|
|
command-init)
|
2021-04-15 13:20:32 +00:00
|
|
|
|
|
|
|
'error-item-not-found
|
|
|
|
"Error: Item not found."
|
|
|
|
|
|
|
|
'error-not-an-option
|
|
|
|
"Error: not an option."
|
|
|
|
|
|
|
|
'error-not-a-number
|
|
|
|
"Error: not a number."
|
|
|
|
|
|
|
|
'warning-permissions
|
|
|
|
(format (string-append "Warning: The permissions on your '~a' file are incorrect." newline
|
|
|
|
"Other users on this host may be able to read your file." newline
|
|
|
|
"Try running 'chmod 600 ~a' to fix this." newline)
|
|
|
|
program-path
|
|
|
|
program-path)
|
|
|
|
|
|
|
|
'init-cancelled
|
|
|
|
(format "Cancelled the creation of '~a'." program-path)
|
|
|
|
|
|
|
|
'init-prompt
|
|
|
|
(format (string-append "The file '~a' will be created." newline
|
|
|
|
"Is this okay? [y/n]")
|
|
|
|
program-path)
|
|
|
|
|
|
|
|
'file-created
|
|
|
|
(format "Successfully created ~a." program-path)
|
|
|
|
|
|
|
|
'empty-list
|
|
|
|
"There is nothing in your list."
|
|
|
|
|
|
|
|
'added
|
|
|
|
"Added '~a' to your list."
|
|
|
|
|
|
|
|
'removed
|
|
|
|
"Removed '~a' from your list."))
|
2020-06-20 04:21:19 +00:00
|
|
|
|
2020-07-15 15:52:32 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; helpers
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (messages-ref key)
|
|
|
|
(hash-ref messages key))
|
2020-06-20 04:21:19 +00:00
|
|
|
|
2020-07-15 15:52:32 +00:00
|
|
|
(define (displayln-messages-ref key)
|
2021-04-15 13:20:32 +00:00
|
|
|
(displayln (messages-ref key)))
|
2020-06-20 04:21:19 +00:00
|
|
|
|
2021-04-15 13:20:32 +00:00
|
|
|
(define (displayln-format-messages-ref key value)
|
|
|
|
(displayln (format (messages-ref key) value)))
|
2020-06-20 04:21:19 +00:00
|
|
|
|
2020-07-15 15:52:32 +00:00
|
|
|
(define (create-file string)
|
2021-04-15 13:20:32 +00:00
|
|
|
(close-output-port (open-output-file string)))
|
2020-06-15 15:07:48 +00:00
|
|
|
|
2021-04-15 13:20:32 +00:00
|
|
|
(define (has-read-permissions? string)
|
|
|
|
(equal? read-permissions (file-or-directory-permissions string 'bits)))
|
2020-06-15 15:07:48 +00:00
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-15 15:52:32 +00:00
|
|
|
;; init
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-15 15:52:32 +00:00
|
|
|
(define (init/cancel)
|
|
|
|
(displayln-messages-ref 'init-cancelled)
|
2020-06-20 04:21:19 +00:00
|
|
|
(exit))
|
|
|
|
|
2021-04-15 13:20:32 +00:00
|
|
|
(define (init/create-file)
|
|
|
|
(create-file program-path)
|
|
|
|
(file-or-directory-permissions program-path read-permissions)
|
|
|
|
(displayln-messages-ref 'file-created))
|
|
|
|
|
2020-07-15 15:52:32 +00:00
|
|
|
(define (init/prompt)
|
|
|
|
(displayln-messages-ref 'init-prompt)
|
|
|
|
(display "> ")
|
2021-04-15 13:20:32 +00:00
|
|
|
(let ([input-symbol (string->symbol (read-line))])
|
2020-07-15 15:52:32 +00:00
|
|
|
(case input-symbol
|
2021-04-15 13:20:32 +00:00
|
|
|
['y (init/create-file)]
|
|
|
|
['n (init/cancel)]
|
|
|
|
[else (displayln-messages-ref 'error-not-an-option)])))
|
2020-07-15 15:52:32 +00:00
|
|
|
|
|
|
|
(define (init)
|
2021-04-15 13:20:32 +00:00
|
|
|
(if (or (file-exists? program-path)
|
|
|
|
(directory-exists? program-path))
|
|
|
|
(displayln-messages-ref 'error-something-exists)
|
|
|
|
(init/prompt)))
|
2020-06-15 03:52:12 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
2020-06-15 17:55:24 +00:00
|
|
|
;; ls
|
|
|
|
;; ------------------------------------------------
|
2020-06-20 04:21:19 +00:00
|
|
|
(define (ls/display-list listof-items)
|
2021-04-15 13:20:32 +00:00
|
|
|
(let* ([listof-number-strings (map number->string (range (length listof-items)))]
|
|
|
|
[listof-numbered-items (map (lambda (a b) (string-append a ". " b))
|
2020-06-15 17:55:24 +00:00
|
|
|
listof-number-strings
|
2020-06-20 04:21:19 +00:00
|
|
|
listof-items)])
|
2020-06-15 17:55:24 +00:00
|
|
|
(for ([item listof-numbered-items])
|
2020-07-15 15:52:32 +00:00
|
|
|
(displayln item))))
|
2020-06-15 17:55:24 +00:00
|
|
|
|
|
|
|
(define (ls)
|
2021-04-16 01:57:38 +00:00
|
|
|
(if (and (file-exists? program-path)
|
|
|
|
(has-read-permissions? program-path))
|
2021-04-15 13:20:32 +00:00
|
|
|
(let ([listof-items (file->lines program-path)])
|
|
|
|
(if (null? listof-items)
|
2020-07-15 15:52:32 +00:00
|
|
|
(displayln-messages-ref 'empty-list)
|
2021-04-15 13:20:32 +00:00
|
|
|
(ls/display-list listof-items)))
|
|
|
|
(displayln-messages-ref 'couldnt-file-file)))
|
2020-06-15 17:55:24 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; rm
|
|
|
|
;; ------------------------------------------------
|
2021-04-15 13:20:32 +00:00
|
|
|
(define (rm/remove-nicething item-number)
|
|
|
|
(let ([listof-items (file->lines program-path)])
|
2020-06-20 04:21:19 +00:00
|
|
|
(if (and (not (null? listof-items))
|
2021-04-16 01:35:44 +00:00
|
|
|
(exact? item-number)
|
2021-04-15 13:20:32 +00:00
|
|
|
(>= item-number 0)
|
|
|
|
(< item-number (length listof-items)))
|
|
|
|
(let* ([item-to-remove (list-ref listof-items item-number)]
|
|
|
|
[list-without-item (remove item-to-remove listof-items)])
|
|
|
|
(display-lines-to-file list-without-item program-path #:exists 'truncate)
|
|
|
|
(displayln-format-messages-ref 'removed item-to-remove))
|
|
|
|
(displayln-messages-ref 'error-item-not-found))))
|
|
|
|
|
|
|
|
(define (rm arg)
|
2021-04-16 01:57:38 +00:00
|
|
|
(if (and (file-exists? program-path)
|
|
|
|
(has-read-permissions? program-path))
|
2021-04-15 13:20:32 +00:00
|
|
|
(let ([item-number (string->number arg)])
|
|
|
|
(if item-number
|
|
|
|
(rm/remove-nicething item-number)
|
|
|
|
(displayln-format-messages-ref 'error-not-a-number arg)))
|
|
|
|
(displayln-messages-ref 'couldnt-find-file)))
|
2020-06-15 17:55:24 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; add
|
2020-06-15 03:52:12 +00:00
|
|
|
;; ------------------------------------------------
|
2021-04-15 13:20:32 +00:00
|
|
|
(define (add nicething)
|
2021-04-16 01:57:38 +00:00
|
|
|
(if (and (file-exists? program-path)
|
|
|
|
(has-read-permissions? program-path))
|
2021-04-15 13:20:32 +00:00
|
|
|
;; The removing and adding of the '\n' is to
|
|
|
|
;; ensure only one '\n' exists at the end of the
|
|
|
|
;; item to be added.
|
|
|
|
(let* ([nicething-no-newline (string-replace nicething "\n" "")]
|
|
|
|
[nicething-newline (string-append nicething-no-newline "\n")])
|
|
|
|
(display-to-file nicething-newline program-path #:exists 'append)
|
|
|
|
(displayln-format-messages-ref 'added nicething-no-newline))
|
|
|
|
(displayln-messages-ref 'couldnt-find-file)))
|
2020-06-14 02:15:04 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; random message
|
|
|
|
;; ------------------------------------------------
|
2020-06-15 03:52:12 +00:00
|
|
|
(define (random-message)
|
2021-04-15 13:20:32 +00:00
|
|
|
(let* ([root-home (build-path (find-system-path 'sys-dir) "home")] ;; /home
|
2020-07-15 15:52:32 +00:00
|
|
|
;; `#:build? #t` Builds full paths for all items listed in /home/
|
2021-04-15 13:20:32 +00:00
|
|
|
[listof-home-directories (directory-list root-home #:build? #t)]
|
|
|
|
[listof-nicethings-paths (map (lambda (x) (build-path x program-file))
|
|
|
|
listof-home-directories)]
|
|
|
|
[paths-with-nicethings (filter file-exists? listof-nicethings-paths)]
|
|
|
|
[paths-readable (filter has-read-permissions? paths-with-nicethings)]
|
|
|
|
[listof-nicethings (apply append (map file->lines paths-readable))]
|
|
|
|
[list-length (length listof-nicethings)])
|
2020-06-15 18:14:50 +00:00
|
|
|
(when (not (zero? list-length))
|
2021-04-15 13:20:32 +00:00
|
|
|
(let* ([random-number (random list-length)]
|
|
|
|
[random-nicething (list-ref listof-nicethings random-number)]
|
|
|
|
[nicething-trimmed (string-replace random-nicething "\n" "")])
|
|
|
|
(displayln nicething-trimmed)))))
|
2020-06-15 03:52:12 +00:00
|
|
|
|
2020-06-14 02:15:04 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; help
|
2020-06-20 04:22:39 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (help)
|
2020-07-15 15:52:32 +00:00
|
|
|
(displayln
|
|
|
|
(string-append
|
2021-04-15 13:20:32 +00:00
|
|
|
"Usage:" newline
|
|
|
|
(format " ~a [<command>] [<args>]" program-name)
|
|
|
|
double-newline
|
|
|
|
|
|
|
|
"Commands:" newline
|
|
|
|
" No command - Displays a random nicething from a random user." newline
|
|
|
|
(format " ~a - Creates a file in ~a, which allows you to contribute to the town-wide list of nicethings."
|
2021-04-16 01:47:16 +00:00
|
|
|
command-init
|
2021-04-15 13:20:32 +00:00
|
|
|
program-path) newline
|
2021-04-16 01:47:16 +00:00
|
|
|
(format " ~a - Adds a nicething to your list." command-add) newline
|
|
|
|
(format " ~a - Prints a numbered list of the nicethings you've added." command-ls) newline
|
|
|
|
(format " ~a - Removes a nicething from your list." command-rm)
|
2021-04-15 13:20:32 +00:00
|
|
|
double-newline
|
|
|
|
|
|
|
|
"Examples:" newline
|
|
|
|
(format " ~a" program-name) newline
|
2021-04-16 01:47:16 +00:00
|
|
|
(format " ~a ~a" program-name command-init) newline
|
|
|
|
(format " ~a ~a \"You are wonderful\"" program-name command-add) newline
|
|
|
|
(format " ~a ~a" program-name command-ls) newline
|
|
|
|
(format " ~a ~a 2" program-name command-rm))))
|
2020-06-14 02:15:04 +00:00
|
|
|
|
|
|
|
(define (process-args vectorof-args)
|
|
|
|
(match vectorof-args
|
2021-04-16 01:47:16 +00:00
|
|
|
[(or (vector (== command-help-1))
|
|
|
|
(vector (== command-help-2))
|
|
|
|
(vector (== command-help-3))) (help)]
|
|
|
|
[(vector (== command-ls)) (ls)]
|
|
|
|
[(vector (== command-init)) (init)]
|
|
|
|
[(vector (== command-add) a) (add a)]
|
|
|
|
[(vector (== command-rm) a) (rm a)]
|
2020-09-28 18:05:03 +00:00
|
|
|
[_ (random-message)]
|
|
|
|
[(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)]))
|
2020-06-14 02:15:04 +00:00
|
|
|
|
|
|
|
(define (main vectorof-args)
|
|
|
|
(process-args vectorof-args))
|
|
|
|
|
|
|
|
(main (current-command-line-arguments))
|