2019-05-25 13:49:06 +00:00
|
|
|
#lang racket/base
|
|
|
|
|
2020-06-19 21:54:51 +00:00
|
|
|
(require racket/file
|
|
|
|
racket/list
|
|
|
|
racket/match
|
|
|
|
racket/string)
|
|
|
|
|
|
|
|
;; ------------------------------------------------
|
2020-07-14 21:04:03 +00:00
|
|
|
;; Constants
|
2020-06-19 21:54:51 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-14 21:04:03 +00:00
|
|
|
(define help-command-1 "help")
|
|
|
|
(define help-command-2 "--help")
|
|
|
|
(define help-command-3 "-h")
|
|
|
|
(define init-command "init")
|
2020-06-19 21:54:51 +00:00
|
|
|
(define ls-command "ls")
|
|
|
|
(define rm-command "rm")
|
|
|
|
(define add-command "add")
|
|
|
|
(define program-name "rodo")
|
2020-07-14 21:04:03 +00:00
|
|
|
(define program-file (string-append "." program-name))
|
|
|
|
(define program-path (build-path (find-system-path 'home-dir) program-file))
|
|
|
|
(define program-permissions 384) ;; 600/-rw------- permissions
|
|
|
|
(define newline "\n") ;; This is easier to see than a '\n' inside of a string
|
|
|
|
(define double-newline "\n\n")
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
2020-07-14 21:04:03 +00:00
|
|
|
;; Messages
|
2020-06-19 21:54:51 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
(define messages
|
|
|
|
(hash
|
2020-07-14 21:04:03 +00:00
|
|
|
'error-incorrect-usage
|
|
|
|
(format (string-append "Error: Incorrect usage." newline
|
|
|
|
"Try running '~a ~a' for more information.")
|
|
|
|
program-name
|
|
|
|
help-command-1)
|
|
|
|
|
|
|
|
'error-file-already-exists
|
|
|
|
(format "Error: The file '~a' already exists." program-path)
|
|
|
|
|
|
|
|
'error-fake-file-exists
|
|
|
|
(format (string-append "Error: The directory '~a' exists." newline
|
|
|
|
"Try moving, renaming, or deleting this directory.")
|
|
|
|
program-path)
|
|
|
|
|
|
|
|
'error-file-doesnt-exist
|
|
|
|
(format (string-append "Error: '~a' doesn't exist." newline
|
|
|
|
"Try running '~a ~a'.")
|
|
|
|
program-path
|
|
|
|
program-name
|
|
|
|
init-command)
|
|
|
|
|
|
|
|
'error-item-not-found
|
|
|
|
"Error: Item not found."
|
|
|
|
|
|
|
|
|
|
|
|
'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 "~a will create '~a'." newline
|
|
|
|
"Is this okay? [y/n]")
|
|
|
|
program-name
|
|
|
|
program-path)
|
|
|
|
|
|
|
|
'file-created
|
|
|
|
(format "Successfully created ~a." program-path)
|
|
|
|
|
|
|
|
'empty-list
|
|
|
|
"There is nothing in your list."))
|
|
|
|
|
|
|
|
(define formatees
|
|
|
|
(hash
|
|
|
|
'error-not-an-option
|
|
|
|
(string-append "Error: '~a' is not an option." newline
|
|
|
|
"Please choose 'y' or 'n'.")
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
'error-not-a-number
|
|
|
|
(string-append "Error: '~a' is not a number.")
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
'added
|
|
|
|
"Added '~a' to your list."
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
'removed
|
|
|
|
"Removed '~a' from your list."))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; helpers
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (messages-ref key)
|
2020-07-14 21:04:03 +00:00
|
|
|
(hash-ref messages key))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
(define (formatees-ref key)
|
|
|
|
(hash-ref formatees key))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
(define (displayln-messages-ref key)
|
|
|
|
(let ([message (messages-ref key)])
|
|
|
|
(displayln message)))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
(define (displayln-formatees-ref key string)
|
|
|
|
(let* ([formatee (formatees-ref key)]
|
|
|
|
[formatee-formatted (format formatee string)])
|
|
|
|
(displayln formatee-formatted)))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
(define (create-file string)
|
|
|
|
(close-output-port
|
|
|
|
(open-output-file string)))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-15 15:50:13 +00:00
|
|
|
(define (has-program-permissions? string)
|
|
|
|
(equal? program-permissions (file-or-directory-permissions string 'bits)))
|
|
|
|
|
2020-06-19 21:54:51 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-14 21:04:03 +00:00
|
|
|
;; Check conditions
|
2020-06-19 21:54:51 +00:00
|
|
|
;; ------------------------------------------------
|
2020-07-14 21:04:03 +00:00
|
|
|
(define (check-conditions)
|
2020-06-19 21:54:51 +00:00
|
|
|
(cond
|
|
|
|
[(directory-exists? program-path)
|
2020-07-14 21:04:03 +00:00
|
|
|
(begin (displayln-messages-ref 'error-fake-file-exists)
|
|
|
|
(exit))]
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
[(not (file-exists? program-path))
|
2020-07-14 21:04:03 +00:00
|
|
|
(begin (displayln-messages-ref 'error-file-doesnt-exist)
|
|
|
|
(exit))]
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-15 15:50:13 +00:00
|
|
|
[(not (has-program-permissions? program-path))
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln-messages-ref 'warning-permissions)]
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
[else 'do-nothing]))
|
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; init
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (init/create-file)
|
|
|
|
(create-file program-path)
|
|
|
|
(file-or-directory-permissions program-path
|
|
|
|
program-permissions)
|
|
|
|
(displayln-messages-ref 'file-created))
|
|
|
|
|
|
|
|
(define (init/cancel)
|
|
|
|
(displayln-messages-ref 'init-cancelled)
|
|
|
|
(exit))
|
|
|
|
|
|
|
|
(define (init/prompt)
|
|
|
|
(displayln-messages-ref 'init-prompt)
|
|
|
|
(display "> ")
|
|
|
|
(let* ([input (read-line)]
|
|
|
|
[input-symbol (string->symbol input)])
|
|
|
|
(case input-symbol
|
|
|
|
['y (init/create-file)]
|
|
|
|
['n (init/cancel)]
|
|
|
|
[else (displayln-formatees-ref 'error-not-an-option input)])))
|
|
|
|
|
|
|
|
(define (init)
|
|
|
|
(cond
|
|
|
|
[(file-exists? program-path)
|
|
|
|
(displayln-messages-ref 'error-file-already-exists)]
|
|
|
|
|
|
|
|
[(directory-exists? program-path)
|
|
|
|
(displayln-messages-ref 'error-fake-file-exists)]
|
|
|
|
|
|
|
|
[else (init/prompt)]))
|
|
|
|
|
2020-06-19 21:54:51 +00:00
|
|
|
;; ------------------------------------------------
|
|
|
|
;; ls
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (ls/display-list listof-items)
|
2020-07-14 21:04:03 +00:00
|
|
|
;; The add1 in the first binding starts the
|
|
|
|
;; listof-numbers at 1 instead of 0 to make the
|
|
|
|
;; list numbering more human-friendly
|
2020-06-19 21:54:51 +00:00
|
|
|
(let* ([listof-numbers (map add1 (range (length listof-items)))]
|
|
|
|
[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-items)])
|
|
|
|
(for ([item listof-numbered-items])
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln item))))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
(define (ls)
|
2020-07-14 21:04:03 +00:00
|
|
|
(check-conditions)
|
2020-06-19 21:54:51 +00:00
|
|
|
(let ([listof-items (file->lines program-path)])
|
|
|
|
(if (null? listof-items)
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln-messages-ref 'empty-list)
|
2020-06-19 21:54:51 +00:00
|
|
|
(ls/display-list listof-items))))
|
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; rm
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (rm/remove-item listof-items item-number)
|
|
|
|
(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)
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln-formatees-ref 'removed item-to-remove)))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
2020-07-14 21:04:03 +00:00
|
|
|
(define (rm/process-string string)
|
2020-06-19 21:54:51 +00:00
|
|
|
(let* ([listof-items (file->lines program-path)]
|
2020-07-14 21:04:03 +00:00
|
|
|
[item-number (string->number string)]
|
|
|
|
;; Subtract 1 from the user's original number,
|
|
|
|
;; because we want to convert the number from
|
|
|
|
;; human numbers (1 2 3) to index numbers
|
|
|
|
;; (0 1 2)
|
|
|
|
[item-number-sub1 (sub1 item-number)]
|
|
|
|
[list-length (length listof-items)])
|
2020-06-19 21:54:51 +00:00
|
|
|
(if (and (not (null? listof-items))
|
|
|
|
(number? item-number)
|
|
|
|
(positive? item-number)
|
2020-07-14 21:04:03 +00:00
|
|
|
;; Don't allow numbers that are equal to
|
|
|
|
;; or greater than list-length, because
|
|
|
|
;; the list index starts at 0
|
|
|
|
;;
|
2020-06-19 21:54:51 +00:00
|
|
|
;; Example:
|
2020-07-14 21:04:03 +00:00
|
|
|
;; Length of (1 2 3) = 3
|
|
|
|
;;
|
|
|
|
;; To reference the highest number, we
|
|
|
|
;; use (list-ref (1 2 3) 2)
|
2020-06-19 21:54:51 +00:00
|
|
|
(< item-number-sub1 list-length))
|
|
|
|
(rm/remove-item listof-items item-number-sub1)
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln-messages-ref 'error-item-not-found))))
|
|
|
|
|
|
|
|
(define (rm string)
|
|
|
|
(check-conditions)
|
|
|
|
(if (string->number string)
|
|
|
|
(rm/process-string string)
|
|
|
|
(displayln-formatees-ref 'error-not-a-number string)))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; add
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (add string)
|
2020-07-14 21:04:03 +00:00
|
|
|
(check-conditions)
|
|
|
|
;; The removing and adding of the '\n' is to
|
|
|
|
;; ensure only one '\n' exists at the end of the
|
|
|
|
;; item to be added.
|
2020-06-19 21:54:51 +00:00
|
|
|
(let* ([string-no-newline (string-replace string "\n" "")]
|
|
|
|
[string-newline (string-append string-no-newline "\n")])
|
|
|
|
(display-to-file string-newline
|
|
|
|
program-path
|
|
|
|
#:exists 'append)
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln-formatees-ref 'added string-no-newline)))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
;; ------------------------------------------------
|
|
|
|
;; help
|
|
|
|
;; ------------------------------------------------
|
|
|
|
(define (help)
|
2020-07-14 21:04:03 +00:00
|
|
|
(displayln
|
|
|
|
(string-append
|
|
|
|
"Usage:" newline
|
|
|
|
(format " ~a [<command>] [<args>]" program-name)
|
|
|
|
double-newline
|
|
|
|
|
|
|
|
"Commands:" newline
|
|
|
|
(format " ~a - Creates your list." init-command) newline
|
|
|
|
(format " ~a - Adds an item to your list." add-command) newline
|
|
|
|
(format " ~a - Prints a numbered list of your items." ls-command) newline
|
|
|
|
(format " ~a - Removes an item from your list." rm-command)
|
|
|
|
double-newline
|
|
|
|
|
|
|
|
"Examples:" newline
|
|
|
|
(format " ~a ~a" program-name init-command) newline
|
|
|
|
(format " ~a ~a \"Go for a walk\"" program-name add-command) newline
|
|
|
|
(format " ~a ~a" program-name ls-command) newline
|
|
|
|
(format " ~a ~a 2" program-name rm-command))))
|
2020-06-19 21:54:51 +00:00
|
|
|
|
|
|
|
(define (process-args vectorof-args)
|
|
|
|
(match vectorof-args
|
2020-07-16 14:13:52 +00:00
|
|
|
[(or (vector (== help-command-1))
|
|
|
|
(vector (== help-command-2))
|
|
|
|
(vector (== help-command-3))) (help)]
|
|
|
|
[(vector (== ls-command)) (ls)]
|
|
|
|
[(vector (== init-command)) (init)]
|
2020-07-16 21:03:53 +00:00
|
|
|
[(vector (== add-command) a) (add a)]
|
|
|
|
[(vector (== rm-command) a) (rm a)]
|
2020-09-24 19:09:41 +00:00
|
|
|
[(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)]
|
|
|
|
[_ (ls)]))
|
2020-06-12 04:46:44 +00:00
|
|
|
|
|
|
|
(define (main vectorof-args)
|
|
|
|
(process-args vectorof-args))
|
2019-05-25 13:49:06 +00:00
|
|
|
|
2020-02-02 04:18:43 +00:00
|
|
|
(main (current-command-line-arguments))
|