nicethings/nicethings.rkt

102 lines
3.7 KiB
Racket
Raw Normal View History

2020-06-14 02:15:04 +00:00
#lang racket
;; ------------------------------------------------
;; messages
;; ------------------------------------------------
(define messages
(hash
2020-06-14 20:39:48 +00:00
'not-found "'~a' wasn't found."
'repair-prompt "You will need it to use nicethings.\nDo you want to create it? [y/n]\n> "
'fake-file-found "The directory '~a' was found.\nPlease move this file somewhere else before using nicethings."
'not-a-command "Error: '~a' is not a command."
'cancel-creation "Cancelled nicethings file creation."
'file-created "'~a' was successfully created."
'not-an-option "Error: '~a' is not an option."))
2020-06-14 02:15:04 +00:00
;; ------------------------------------------------
;; helpers
;; ------------------------------------------------
(define (displayln-for . strings)
(for ([string strings])
(displayln string)))
(define-syntax-rule (displayln-format str ...)
(displayln (format str ...)))
(define (messages-ref key)
(hash-ref messages key))
;; ------------------------------------------------
;; repair
;; ------------------------------------------------
2020-06-14 20:39:48 +00:00
(define (repair/cancel)
(displayln (messages-ref 'cancel-creation))
(exit))
;; Check for a "fake" '.nicethings' file,
;; which is a directory named '.nicethings'
(define (repair/start dot-nicethings-path)
(if (directory-exists? dot-nicethings-path)
(displayln-format (messages-ref 'fake-file-found) dot-nicethings-path)
(begin (close-output-port (open-output-file dot-nicethings-path))
(displayln-format (messages-ref 'file-created) dot-nicethings-path))))
(define (repair/prompt dot-nicethings-path)
(display (messages-ref 'repair-prompt))
(let ([user-input (read-line)])
(case (string->symbol user-input)
['y (repair/start dot-nicethings-path)]
['n (repair/cancel)]
[else (displayln-format (messages-ref 'not-an-option) user-input)])))
2020-06-14 02:15:04 +00:00
(define (repair)
(let* ([dot-nicethings-string ".nicethings"]
[home-directory (find-system-path 'home-dir)]
[listof-home-contents (directory-list home-directory)]
[dot-nicethings-path (build-path home-directory dot-nicethings-string)])
2020-06-14 20:39:48 +00:00
(when (not (file-exists? dot-nicethings-path))
2020-06-14 02:15:04 +00:00
(displayln-format (messages-ref 'not-found) dot-nicethings-path)
2020-06-14 20:39:48 +00:00
(repair/prompt dot-nicethings-path))))
2020-06-14 02:15:04 +00:00
;; ------------------------------------------------
;; random message
;; ------------------------------------------------
(define (random)
;; (let* ([home (get-environment-variable "HOME")]
;; [listof-home-contents (directory home #t)])
(repair))
;; ------------------------------------------------
;; help
;; ------------------------------------------------
(define (help)
(displayln-for
"Usage:"
" town nicethings [<command>] [<args>]"
""
"Commands:"
2020-06-14 20:39:48 +00:00
" No command - Print a random message."
" add - Add a message to the list of messages."
" ls - Print a numbered list of the messages you have added."
" rm - Remove a message from the list of messages."
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)
(match vectorof-args
[(or (vector "-h")
(vector "--help")
(vector "help")) (help)]
[(vector "add" _) (displayln "todo: make add procedure")]
[(vector "ls") (displayln "todo: make ls procedure")]
[(vector "rm" _) (displayln "todo: make rm procedure")]
[(vector _) (displayln-format (messages-ref 'not-a-command) (vector-ref vectorof-args 0))]
[_ (random)]))
(define (main vectorof-args)
(process-args vectorof-args))
(main (current-command-line-arguments))