Added a new 'update' feature and cleaned things up!

main
Jesse Laprade 2021-04-15 22:09:16 -04:00
parent e33ef0c2fb
commit e58a97724e
2 changed files with 211 additions and 232 deletions

View File

@ -6,41 +6,47 @@ A todo-list program for the command line.
![A screenshot of a user adding an removing items from their todo list in a terminal](images/screenshot.gif) ![A screenshot of a user adding an removing items from their todo list in a terminal](images/screenshot.gif)
## Table of Contents ## Table of contents
- [Disclaimer](#disclaimer) <!-- vim-markdown-toc GFM -->
- [Conventions used in this document](#conventions-used-in-this-document)
- [Platforms](#platforms) * [Disclaimer](#disclaimer)
- [Requirements](#requirements) * [Conventions used in this document](#conventions-used-in-this-document)
- [Quick start](#quick-start) * [Platforms](#platforms)
- [Downloading rodo](#downloading-rodo) * [Requirements](#requirements)
- [Downloading rodo using git](#downloading-rodo-using-git) * [Quick start](#quick-start)
- [To download rodo using git](#to-download-rodo-using-git) * [Downloading rodo](#downloading-rodo)
- [Installing rodo](#installing-rodo) * [Downloading rodo using git](#downloading-rodo-using-git)
- [Installing rodo globally](#installing-rodo-globally) * [To download rodo using git](#to-download-rodo-using-git)
- [To install rodo globally](#to-install-rodo-globally) * [Installing rodo](#installing-rodo)
- [Installing rodo locally](#installing-rodo-locally) * [Installing rodo globally](#installing-rodo-globally)
- [To install rodo locally](#to-install-rodo-locally) * [To install rodo globally](#to-install-rodo-globally)
- [Installing rodo to a custom directory](#installing-rodo-to-a-custom-directory) * [Installing rodo locally](#installing-rodo-locally)
- [To install rodo to a custom directory](#to-install-rodo-to-a-custom-directory) * [To install rodo locally](#to-install-rodo-locally)
- [Uninstalling rodo](#uninstalling-rodo) * [Installing rodo to a custom directory](#installing-rodo-to-a-custom-directory)
- [Uninstalling rodo globally](#uninstalling-rodo-globally) * [To install rodo to a custom directory](#to-install-rodo-to-a-custom-directory)
- [To uninstall rodo globally](#to-uninstall-rodo-globally) * [Uninstalling rodo](#uninstalling-rodo)
- [Uninstalling rodo locally](#uninstalling-rodo-locally) * [Uninstalling rodo globally](#uninstalling-rodo-globally)
- [To uninstall rodo locally](#to-uninstall-rodo-locally) * [To uninstall rodo globally](#to-uninstall-rodo-globally)
- [Using rodo](#using-rodo) * [Uninstalling rodo locally](#uninstalling-rodo-locally)
- [Initializing rodo](#initializing-rodo) * [To uninstall rodo locally](#to-uninstall-rodo-locally)
- [To initialize rodo](#to-initialize-rodo) * [Using rodo](#using-rodo)
- [Showing the help message](#showing-the-help-message) * [Initializing rodo](#initializing-rodo)
- [To show the help message](#to-show-the-help-message) * [To initialize rodo](#to-initialize-rodo)
- [Displaying your list](#displaying-your-list) * [Showing the help message](#showing-the-help-message)
- [To display your list](#to-display-your-list) * [To show the help message](#to-show-the-help-message)
- [Adding an item to your list](#adding-an-item-to-your-list) * [Displaying your list](#displaying-your-list)
- [To add an item to your list](#to-add-an-item-to-your-list) * [To display your list](#to-display-your-list)
- [Removing an item from your list](#removing-an-item-from-your-list) * [Adding an item to your list](#adding-an-item-to-your-list)
- [To remove an item from your list](#to-remove-an-item-from-your-list) * [To add an item to your list](#to-add-an-item-to-your-list)
- [List of commands](#list-of-commands) * [Removing an item from your list](#removing-an-item-from-your-list)
- [Usage examples](#usage-examples) * [To remove an item from your list](#to-remove-an-item-from-your-list)
* [Changing the text of an item in your list](#changing-the-text-of-an-item-in-your-list)
* [To change the text of an item in your list](#to-change-the-text-of-an-item-in-your-list)
* [List of commands](#list-of-commands)
* [Usage examples](#usage-examples)
<!-- vim-markdown-toc -->
## Disclaimer ## Disclaimer
@ -247,8 +253,17 @@ your list.
1. Run `rodo rm 1` 1. Run `rodo rm 1`
**Note 1**: The "1" in the procedure above will remove the first item in your **Note 2**: You may need to run `rodo ls` first to see which numbers correspond
list. with which item in your list.
### Changing the text of an item in your list
When changing an item in your list, you can reference the numbers beside each
item when [Displaying your list](#displaying-your-list).
#### To change the text of an item in your list
1. Run `rodo update 2 "This is new text"`
**Note 2**: You may need to run `rodo ls` first to see which numbers correspond **Note 2**: You may need to run `rodo ls` first to see which numbers correspond
with which item in your list. with which item in your list.
@ -262,6 +277,7 @@ This section lists and describes rodo's commands.
* `ls` displays your list * `ls` displays your list
* `add` adds an item to your list * `add` adds an item to your list
* `rm` removes an item from your list * `rm` removes an item from your list
* `update` replaces the contents of an item with new text
## Usage examples ## Usage examples
@ -274,3 +290,5 @@ This section lists and describes rodo's commands.
`rodo add "this is an item"` `rodo add "this is an item"`
`rodo rm 1` `rodo rm 1`
`rodo update 1 "This is new text"`

View File

@ -8,86 +8,81 @@
;; ------------------------------------------------ ;; ------------------------------------------------
;; Constants ;; Constants
;; ------------------------------------------------ ;; ------------------------------------------------
(define help-command-1 "help") (define command-help-1 "help")
(define help-command-2 "--help") (define command-help-2 "--help")
(define help-command-3 "-h") (define command-help-3 "-h")
(define init-command "init") (define command-init "init")
(define ls-command "ls") (define command-ls "ls")
(define rm-command "rm") (define command-rm "rm")
(define add-command "add") (define command-add "add")
(define program-name "rodo") (define command-update "update")
(define program-file (string-append "." program-name)) (define program-name "rodo")
(define program-path (build-path (find-system-path 'home-dir) program-file)) (define program-file (string-append "." program-name))
(define program-permissions 384) ;; 600/-rw------- permissions (define program-path (path->string (build-path (find-system-path 'home-dir) program-file)))
(define newline "\n") ;; This is easier to see than a '\n' inside of a string (define newline "\n")
(define double-newline "\n\n") (define double-newline "\n\n")
;; ------------------------------------------------ ;; ------------------------------------------------
;; Messages ;; Messages
;; ------------------------------------------------ ;; ------------------------------------------------
(define messages (define messages
(hash (hash 'error-too-many-arguments
'error-incorrect-usage (format (string-append "Error: Too many arguments." newline
(format (string-append "Error: Incorrect usage." newline "Try running '~a ~a' for more information.")
"Try running '~a ~a' for more information.") program-name
program-name command-help-1)
help-command-1)
'error-file-already-exists 'error-incorrect-usage
(format "Error: The file '~a' already exists." program-path) (format (string-append "Error: Incorrect usage." newline
"Try running '~a ~a' for more information.")
program-name
command-help-1)
'error-fake-file-exists 'error-couldnt-find-file
(format (string-append "Error: The directory '~a' exists." newline (format (string-append "Error: Couldn't find ~a" newline
"Try moving, renaming, or deleting this directory.") "If the file doesn't exist, try running ~a ~a")
program-path) program-path
program-name
command-init)
'error-file-doesnt-exist 'error-something-exists
(format (string-append "Error: '~a' doesn't exist." newline (format "Error: It looks like ~a already exists." program-path)
"Try running '~a ~a'.")
program-path
program-name
init-command)
'error-item-not-found 'error-file-doesnt-exist
"Error: Item not found." (format (string-append "Error: '~a' doesn't exist." newline
"Try running '~a ~a'.")
program-path
program-name
command-init)
'error-item-not-found
"Error: Item not found."
'warning-permissions 'error-not-an-option
(format (string-append "Warning: The permissions on your '~a' file are incorrect." newline "Error: Not an option."
"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 'error-not-a-number
(format "Cancelled the creation of '~a'." program-path) "Error: Not a number."
'init-prompt 'init-cancelled
(format (string-append "~a will create '~a'." newline (format "Cancelled the creation of '~a'." program-path)
"Is this okay? [y/n]")
program-name
program-path)
'file-created 'init-prompt
(format "Successfully created ~a." program-path) (format (string-append "The file '~a' will be created." newline
"Is this okay? [y/n]")
program-path)
'empty-list 'file-created
"There is nothing in your list.")) (format "Successfully created ~a." program-path)
(define formatees 'empty-list
(hash "There is nothing in your list."
'error-not-an-option
(string-append "Error: '~a' is not an option." newline
"Please choose 'y' or 'n'.")
'error-not-a-number 'added
(string-append "Error: '~a' is not a number.") "Added '~a' to your list."
'added 'removed
"Added '~a' to your list." "Removed '~a' from your list."))
'removed
"Removed '~a' from your list."))
;; ------------------------------------------------ ;; ------------------------------------------------
;; helpers ;; helpers
@ -95,155 +90,116 @@
(define (messages-ref key) (define (messages-ref key)
(hash-ref messages key)) (hash-ref messages key))
(define (formatees-ref key)
(hash-ref formatees key))
(define (displayln-messages-ref key) (define (displayln-messages-ref key)
(let ([message (messages-ref key)]) (displayln (messages-ref key)))
(displayln message)))
(define (displayln-formatees-ref key string) (define (displayln-format-messages-ref key value)
(let* ([formatee (formatees-ref key)] (displayln (format (messages-ref key) value)))
[formatee-formatted (format formatee string)])
(displayln formatee-formatted)))
(define (create-file string) (define (create-file string)
(close-output-port (close-output-port (open-output-file string)))
(open-output-file string)))
(define (has-program-permissions? string)
(equal? program-permissions (file-or-directory-permissions string 'bits)))
;; ------------------------------------------------
;; Check conditions
;; ------------------------------------------------
(define (check-conditions)
(cond
[(directory-exists? program-path)
(begin (displayln-messages-ref 'error-fake-file-exists)
(exit))]
[(not (file-exists? program-path))
(begin (displayln-messages-ref 'error-file-doesnt-exist)
(exit))]
[(not (has-program-permissions? program-path))
(displayln-messages-ref 'warning-permissions)]
[else 'do-nothing]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; init ;; 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) (define (init/cancel)
(displayln-messages-ref 'init-cancelled) (displayln-messages-ref 'init-cancelled)
(exit)) (exit))
(define (init/create-file)
(create-file program-path)
(displayln-messages-ref 'file-created))
(define (init/prompt) (define (init/prompt)
(displayln-messages-ref 'init-prompt) (displayln-messages-ref 'init-prompt)
(display "> ") (display "> ")
(let* ([input (read-line)] (let ([input-symbol (string->symbol (read-line))])
[input-symbol (string->symbol input)])
(case input-symbol (case input-symbol
['y (init/create-file)] ['y (init/create-file)]
['n (init/cancel)] ['n (init/cancel)]
[else (displayln-formatees-ref 'error-not-an-option input)]))) [else (displayln-messages-ref 'error-not-an-option)])))
(define (init) (define (init)
(cond (if (or (file-exists? program-path)
[(file-exists? program-path) (directory-exists? program-path))
(displayln-messages-ref 'error-file-already-exists)] (displayln-messages-ref 'error-something-exists)
(init/prompt)))
[(directory-exists? program-path)
(displayln-messages-ref 'error-fake-file-exists)]
[else (init/prompt)]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; ls ;; ls
;; ------------------------------------------------ ;; ------------------------------------------------
(define (ls/display-list listof-items) (define (ls/display-list listof-items)
;; The add1 in the first binding starts the (let* ([numbers (map number->string (range (length listof-items)))]
;; listof-numbers at 1 instead of 0 to make the [numbered-items (map (lambda (a b) (string-append a ". " b))
;; list numbering more human-friendly numbers
(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)]) listof-items)])
(for ([item listof-numbered-items]) (for ([item numbered-items])
(displayln item)))) (displayln item))))
(define (ls) (define (ls)
(check-conditions) (if (file-exists? program-path)
(let ([listof-items (file->lines program-path)]) (let ([listof-items (file->lines program-path)])
(if (null? listof-items) (if (null? listof-items)
(displayln-messages-ref 'empty-list) (displayln-messages-ref 'empty-list)
(ls/display-list listof-items)))) (ls/display-list listof-items)))
(displayln-messages-ref 'couldnt-file-file)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; rm ;; rm
;; ------------------------------------------------ ;; ------------------------------------------------
(define (rm/remove-item listof-items item-number) (define (rm/remove-item item-number)
(let* ([item-to-remove (list-ref listof-items item-number)] (let ([listof-items (file->lines program-path)])
[list-without-item (remove item-to-remove listof-items)])
(display-lines-to-file list-without-item
program-path
#:exists 'truncate)
(displayln-formatees-ref 'removed item-to-remove)))
(define (rm/process-string string)
(let* ([listof-items (file->lines program-path)]
[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)])
(if (and (not (null? listof-items)) (if (and (not (null? listof-items))
(number? item-number) (exact? item-number)
(positive? item-number) (>= item-number 0)
;; Don't allow numbers that are equal to (< item-number (length listof-items)))
;; or greater than list-length, because (let* ([item-to-remove (list-ref listof-items item-number)]
;; the list index starts at 0 [list-without-item (remove item-to-remove listof-items)])
;; (display-lines-to-file list-without-item program-path #:exists 'truncate)
;; Example: (displayln-format-messages-ref 'removed item-to-remove))
;; Length of (1 2 3) = 3 (displayln-messages-ref 'error-item-not-found))))
;;
;; To reference the highest number, we
;; use (list-ref (1 2 3) 2)
(< item-number-sub1 list-length))
(rm/remove-item listof-items item-number-sub1)
(displayln-messages-ref 'error-item-not-found))))
(define (rm string) (define (rm arg)
(check-conditions) (if (file-exists? program-path)
(if (string->number string) (let ([item-number (string->number arg)])
(rm/process-string string) (if item-number
(displayln-formatees-ref 'error-not-a-number string))) (rm/remove-item item-number)
(displayln-format-messages-ref 'error-not-a-number arg)))
(displayln-messages-ref 'error-couldnt-find-file)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; add ;; add
;; ------------------------------------------------ ;; ------------------------------------------------
(define (add string) (define (add item)
(check-conditions) (if (file-exists? program-path)
;; The removing and adding of the '\n' is to ;; The removing and adding of the '\n' is to
;; ensure only one '\n' exists at the end of the ;; ensure only one '\n' exists at the end of the
;; item to be added. ;; item to be added.
(let* ([string-no-newline (string-replace string "\n" "")] (let* ([item-no-newline (string-replace item "\n" "")]
[string-newline (string-append string-no-newline "\n")]) [item-newline (string-append item-no-newline "\n")])
(display-to-file string-newline (display-to-file item-newline program-path #:exists 'append)
program-path (displayln-format-messages-ref 'added item-no-newline))
#:exists 'append) (displayln-messages-ref 'error-couldnt-find-file)))
(displayln-formatees-ref 'added string-no-newline)))
(define (update/update-item n item-index text)
(let ([item-list (file->lines program-path)])
(if (and (not (null? item-list))
(>= item-index 0)
(exact? item-index)
(< item-index (length item-list)))
(begin (display-lines-to-file (list-set item-list item-index text)
program-path
#:exists 'truncate)
(displayln-format-messages-ref 'updated-item n))
(displayln-messages-ref 'error-item-not-found))))
(define (update n text)
(if (file-exists? program-path)
(let ([item-index (string->number n)])
(if item-index
(update/update-item n item-index text)
(displayln-messages-ref 'error-not-a-number)))
(displayln-messages-ref 'error-couldnt-find-file)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; help ;; help
@ -251,34 +207,39 @@
(define (help) (define (help)
(displayln (displayln
(string-append (string-append
"Usage:" newline "Usage:" newline
(format " ~a [<command>] [<args>]" program-name) (format " ~a [<command>] [<args>]" program-name)
double-newline double-newline
"Commands:" newline "Commands:" newline
(format " ~a - Creates your list." init-command) newline (format " ~a - Creates a file in ~a, where your items will be stored."
(format " ~a - Adds an item to your list." add-command) newline command-init
(format " ~a - Prints a numbered list of your items." ls-command) newline program-path) newline
(format " ~a - Removes an item from your list." rm-command) (format " ~a - Adds an item to your list." command-add) newline
double-newline (format " ~a - Prints a numbered list of the items you've added." command-ls) newline
(format " ~a - Removes an item from your list." command-rm)
(format " ~a - Replaces the contents of an item with new text." command-update)
double-newline
"Examples:" newline "Examples:" newline
(format " ~a ~a" program-name init-command) newline (format " ~a" program-name) newline
(format " ~a ~a \"Go for a walk\"" program-name add-command) newline (format " ~a ~a" program-name command-init) newline
(format " ~a ~a" program-name ls-command) newline (format " ~a ~a \"You are wonderful\"" program-name command-add) newline
(format " ~a ~a 2" program-name rm-command)))) (format " ~a ~a" program-name command-ls) newline
(format " ~a ~a 2" program-name command-rm)
(format " ~a ~a 2 \"This is new text!\"" program-name command-update))))
(define (process-args vectorof-args) (define (process-args vectorof-args)
(match vectorof-args (match vectorof-args
[(or (vector (== help-command-1)) [(vector (== command-update) n text) (update n text)]
(vector (== help-command-2)) [(vector (== command-add) a) (add a)]
(vector (== help-command-3))) (help)] [(vector (== command-rm) a) (rm a)]
[(vector (== ls-command)) (ls)] [(vector (== command-ls)) (ls)]
[(vector (== init-command)) (init)] [(vector (== command-init)) (init)]
[(vector (== add-command) a) (add a)] [(or (vector (== command-help-1))
[(vector (== rm-command) a) (rm a)] (vector (== command-help-2))
[(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)] (vector (== command-help-3))) (help)]
[_ (ls)])) [_ (displayln-messages-ref 'error-incorrect-usage)]))
(define (main vectorof-args) (define (main vectorof-args)
(process-args vectorof-args)) (process-args vectorof-args))