Added a new 'update' feature and cleaned things up!
parent
e33ef0c2fb
commit
e58a97724e
90
README.md
90
README.md
|
@ -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)
|
||||
|
||||
## Table of Contents
|
||||
## Table of contents
|
||||
|
||||
- [Disclaimer](#disclaimer)
|
||||
- [Conventions used in this document](#conventions-used-in-this-document)
|
||||
- [Platforms](#platforms)
|
||||
- [Requirements](#requirements)
|
||||
- [Quick start](#quick-start)
|
||||
- [Downloading rodo](#downloading-rodo)
|
||||
- [Downloading rodo using git](#downloading-rodo-using-git)
|
||||
- [To download rodo using git](#to-download-rodo-using-git)
|
||||
- [Installing rodo](#installing-rodo)
|
||||
- [Installing rodo globally](#installing-rodo-globally)
|
||||
- [To install rodo globally](#to-install-rodo-globally)
|
||||
- [Installing rodo locally](#installing-rodo-locally)
|
||||
- [To install rodo locally](#to-install-rodo-locally)
|
||||
- [Installing rodo to a custom directory](#installing-rodo-to-a-custom-directory)
|
||||
- [To install rodo to a custom directory](#to-install-rodo-to-a-custom-directory)
|
||||
- [Uninstalling rodo](#uninstalling-rodo)
|
||||
- [Uninstalling rodo globally](#uninstalling-rodo-globally)
|
||||
- [To uninstall rodo globally](#to-uninstall-rodo-globally)
|
||||
- [Uninstalling rodo locally](#uninstalling-rodo-locally)
|
||||
- [To uninstall rodo locally](#to-uninstall-rodo-locally)
|
||||
- [Using rodo](#using-rodo)
|
||||
- [Initializing rodo](#initializing-rodo)
|
||||
- [To initialize rodo](#to-initialize-rodo)
|
||||
- [Showing the help message](#showing-the-help-message)
|
||||
- [To show the help message](#to-show-the-help-message)
|
||||
- [Displaying your list](#displaying-your-list)
|
||||
- [To display your list](#to-display-your-list)
|
||||
- [Adding an item to your list](#adding-an-item-to-your-list)
|
||||
- [To add an item to your list](#to-add-an-item-to-your-list)
|
||||
- [Removing an item from your list](#removing-an-item-from-your-list)
|
||||
- [To remove an item from your list](#to-remove-an-item-from-your-list)
|
||||
- [List of commands](#list-of-commands)
|
||||
- [Usage examples](#usage-examples)
|
||||
<!-- vim-markdown-toc GFM -->
|
||||
|
||||
* [Disclaimer](#disclaimer)
|
||||
* [Conventions used in this document](#conventions-used-in-this-document)
|
||||
* [Platforms](#platforms)
|
||||
* [Requirements](#requirements)
|
||||
* [Quick start](#quick-start)
|
||||
* [Downloading rodo](#downloading-rodo)
|
||||
* [Downloading rodo using git](#downloading-rodo-using-git)
|
||||
* [To download rodo using git](#to-download-rodo-using-git)
|
||||
* [Installing rodo](#installing-rodo)
|
||||
* [Installing rodo globally](#installing-rodo-globally)
|
||||
* [To install rodo globally](#to-install-rodo-globally)
|
||||
* [Installing rodo locally](#installing-rodo-locally)
|
||||
* [To install rodo locally](#to-install-rodo-locally)
|
||||
* [Installing rodo to a custom directory](#installing-rodo-to-a-custom-directory)
|
||||
* [To install rodo to a custom directory](#to-install-rodo-to-a-custom-directory)
|
||||
* [Uninstalling rodo](#uninstalling-rodo)
|
||||
* [Uninstalling rodo globally](#uninstalling-rodo-globally)
|
||||
* [To uninstall rodo globally](#to-uninstall-rodo-globally)
|
||||
* [Uninstalling rodo locally](#uninstalling-rodo-locally)
|
||||
* [To uninstall rodo locally](#to-uninstall-rodo-locally)
|
||||
* [Using rodo](#using-rodo)
|
||||
* [Initializing rodo](#initializing-rodo)
|
||||
* [To initialize rodo](#to-initialize-rodo)
|
||||
* [Showing the help message](#showing-the-help-message)
|
||||
* [To show the help message](#to-show-the-help-message)
|
||||
* [Displaying your list](#displaying-your-list)
|
||||
* [To display your list](#to-display-your-list)
|
||||
* [Adding an item to your list](#adding-an-item-to-your-list)
|
||||
* [To add an item to your list](#to-add-an-item-to-your-list)
|
||||
* [Removing an item from your list](#removing-an-item-from-your-list)
|
||||
* [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
|
||||
|
||||
|
@ -247,8 +253,17 @@ your list.
|
|||
|
||||
1. Run `rodo rm 1`
|
||||
|
||||
**Note 1**: The "1" in the procedure above will remove the first item in your
|
||||
list.
|
||||
**Note 2**: You may need to run `rodo ls` first to see which numbers correspond
|
||||
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
|
||||
with which item in your list.
|
||||
|
@ -262,6 +277,7 @@ This section lists and describes rodo's commands.
|
|||
* `ls` displays your list
|
||||
* `add` adds an item to your list
|
||||
* `rm` removes an item from your list
|
||||
* `update` replaces the contents of an item with new text
|
||||
|
||||
## Usage examples
|
||||
|
||||
|
@ -274,3 +290,5 @@ This section lists and describes rodo's commands.
|
|||
`rodo add "this is an item"`
|
||||
|
||||
`rodo rm 1`
|
||||
|
||||
`rodo update 1 "This is new text"`
|
||||
|
|
271
src/rodo.rkt
271
src/rodo.rkt
|
@ -8,80 +8,75 @@
|
|||
;; ------------------------------------------------
|
||||
;; Constants
|
||||
;; ------------------------------------------------
|
||||
(define help-command-1 "help")
|
||||
(define help-command-2 "--help")
|
||||
(define help-command-3 "-h")
|
||||
(define init-command "init")
|
||||
(define ls-command "ls")
|
||||
(define rm-command "rm")
|
||||
(define add-command "add")
|
||||
(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")
|
||||
(define command-update "update")
|
||||
(define program-name "rodo")
|
||||
(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 program-path (path->string (build-path (find-system-path 'home-dir) program-file)))
|
||||
(define newline "\n")
|
||||
(define double-newline "\n\n")
|
||||
|
||||
;; ------------------------------------------------
|
||||
;; Messages
|
||||
;; ------------------------------------------------
|
||||
(define messages
|
||||
(hash
|
||||
(hash 'error-too-many-arguments
|
||||
(format (string-append "Error: Too many arguments." newline
|
||||
"Try running '~a ~a' for more information.")
|
||||
program-name
|
||||
command-help-1)
|
||||
|
||||
'error-incorrect-usage
|
||||
(format (string-append "Error: Incorrect usage." newline
|
||||
"Try running '~a ~a' for more information.")
|
||||
program-name
|
||||
help-command-1)
|
||||
command-help-1)
|
||||
|
||||
'error-file-already-exists
|
||||
(format "Error: The file '~a' already exists." program-path)
|
||||
'error-couldnt-find-file
|
||||
(format (string-append "Error: Couldn't find ~a" newline
|
||||
"If the file doesn't exist, try running ~a ~a")
|
||||
program-path
|
||||
program-name
|
||||
command-init)
|
||||
|
||||
'error-fake-file-exists
|
||||
(format (string-append "Error: The directory '~a' exists." newline
|
||||
"Try moving, renaming, or deleting this directory.")
|
||||
program-path)
|
||||
'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
|
||||
init-command)
|
||||
command-init)
|
||||
|
||||
'error-item-not-found
|
||||
"Error: Item not found."
|
||||
|
||||
'error-not-an-option
|
||||
"Error: Not an option."
|
||||
|
||||
'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)
|
||||
'error-not-a-number
|
||||
"Error: Not a number."
|
||||
|
||||
'init-cancelled
|
||||
(format "Cancelled the creation of '~a'." program-path)
|
||||
|
||||
'init-prompt
|
||||
(format (string-append "~a will create '~a'." newline
|
||||
(format (string-append "The file '~a' will be created." 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'.")
|
||||
|
||||
'error-not-a-number
|
||||
(string-append "Error: '~a' is not a number.")
|
||||
"There is nothing in your list."
|
||||
|
||||
'added
|
||||
"Added '~a' to your list."
|
||||
|
@ -95,155 +90,116 @@
|
|||
(define (messages-ref key)
|
||||
(hash-ref messages key))
|
||||
|
||||
(define (formatees-ref key)
|
||||
(hash-ref formatees key))
|
||||
|
||||
(define (displayln-messages-ref key)
|
||||
(let ([message (messages-ref key)])
|
||||
(displayln message)))
|
||||
(displayln (messages-ref key)))
|
||||
|
||||
(define (displayln-formatees-ref key string)
|
||||
(let* ([formatee (formatees-ref key)]
|
||||
[formatee-formatted (format formatee string)])
|
||||
(displayln formatee-formatted)))
|
||||
(define (displayln-format-messages-ref key value)
|
||||
(displayln (format (messages-ref key) value)))
|
||||
|
||||
(define (create-file string)
|
||||
(close-output-port
|
||||
(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]))
|
||||
(close-output-port (open-output-file string)))
|
||||
|
||||
;; ------------------------------------------------
|
||||
;; 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/create-file)
|
||||
(create-file program-path)
|
||||
(displayln-messages-ref 'file-created))
|
||||
|
||||
(define (init/prompt)
|
||||
(displayln-messages-ref 'init-prompt)
|
||||
(display "> ")
|
||||
(let* ([input (read-line)]
|
||||
[input-symbol (string->symbol input)])
|
||||
(let ([input-symbol (string->symbol (read-line))])
|
||||
(case input-symbol
|
||||
['y (init/create-file)]
|
||||
['n (init/cancel)]
|
||||
[else (displayln-formatees-ref 'error-not-an-option input)])))
|
||||
[else (displayln-messages-ref 'error-not-an-option)])))
|
||||
|
||||
(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)]))
|
||||
(if (or (file-exists? program-path)
|
||||
(directory-exists? program-path))
|
||||
(displayln-messages-ref 'error-something-exists)
|
||||
(init/prompt)))
|
||||
|
||||
;; ------------------------------------------------
|
||||
;; ls
|
||||
;; ------------------------------------------------
|
||||
(define (ls/display-list listof-items)
|
||||
;; The add1 in the first binding starts the
|
||||
;; listof-numbers at 1 instead of 0 to make the
|
||||
;; list numbering more human-friendly
|
||||
(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
|
||||
(let* ([numbers (map number->string (range (length listof-items)))]
|
||||
[numbered-items (map (lambda (a b) (string-append a ". " b))
|
||||
numbers
|
||||
listof-items)])
|
||||
(for ([item listof-numbered-items])
|
||||
(for ([item numbered-items])
|
||||
(displayln item))))
|
||||
|
||||
(define (ls)
|
||||
(check-conditions)
|
||||
(if (file-exists? program-path)
|
||||
(let ([listof-items (file->lines program-path)])
|
||||
(if (null? listof-items)
|
||||
(displayln-messages-ref 'empty-list)
|
||||
(ls/display-list listof-items))))
|
||||
(ls/display-list listof-items)))
|
||||
(displayln-messages-ref 'couldnt-file-file)))
|
||||
|
||||
;; ------------------------------------------------
|
||||
;; rm
|
||||
;; ------------------------------------------------
|
||||
(define (rm/remove-item listof-items item-number)
|
||||
(define (rm/remove-item item-number)
|
||||
(let ([listof-items (file->lines program-path)])
|
||||
(if (and (not (null? listof-items))
|
||||
(exact? item-number)
|
||||
(>= 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-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))
|
||||
(number? item-number)
|
||||
(positive? item-number)
|
||||
;; Don't allow numbers that are equal to
|
||||
;; or greater than list-length, because
|
||||
;; the list index starts at 0
|
||||
;;
|
||||
;; Example:
|
||||
;; Length of (1 2 3) = 3
|
||||
;;
|
||||
;; 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)
|
||||
(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 string)
|
||||
(check-conditions)
|
||||
(if (string->number string)
|
||||
(rm/process-string string)
|
||||
(displayln-formatees-ref 'error-not-a-number string)))
|
||||
(define (rm arg)
|
||||
(if (file-exists? program-path)
|
||||
(let ([item-number (string->number arg)])
|
||||
(if item-number
|
||||
(rm/remove-item item-number)
|
||||
(displayln-format-messages-ref 'error-not-a-number arg)))
|
||||
(displayln-messages-ref 'error-couldnt-find-file)))
|
||||
|
||||
;; ------------------------------------------------
|
||||
;; add
|
||||
;; ------------------------------------------------
|
||||
(define (add string)
|
||||
(check-conditions)
|
||||
(define (add item)
|
||||
(if (file-exists? program-path)
|
||||
;; The removing and adding of the '\n' is to
|
||||
;; ensure only one '\n' exists at the end of the
|
||||
;; item to be added.
|
||||
(let* ([string-no-newline (string-replace string "\n" "")]
|
||||
[string-newline (string-append string-no-newline "\n")])
|
||||
(display-to-file string-newline
|
||||
(let* ([item-no-newline (string-replace item "\n" "")]
|
||||
[item-newline (string-append item-no-newline "\n")])
|
||||
(display-to-file item-newline program-path #:exists 'append)
|
||||
(displayln-format-messages-ref 'added item-no-newline))
|
||||
(displayln-messages-ref 'error-couldnt-find-file)))
|
||||
|
||||
(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 'append)
|
||||
(displayln-formatees-ref 'added string-no-newline)))
|
||||
#: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
|
||||
|
@ -256,29 +212,34 @@
|
|||
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)
|
||||
(format " ~a - Creates a file in ~a, where your items will be stored."
|
||||
command-init
|
||||
program-path) newline
|
||||
(format " ~a - Adds an item to your list." command-add) 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
|
||||
(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))))
|
||||
(format " ~a" program-name) newline
|
||||
(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)
|
||||
(format " ~a ~a 2 \"This is new text!\"" program-name command-update))))
|
||||
|
||||
(define (process-args vectorof-args)
|
||||
(match vectorof-args
|
||||
[(or (vector (== help-command-1))
|
||||
(vector (== help-command-2))
|
||||
(vector (== help-command-3))) (help)]
|
||||
[(vector (== ls-command)) (ls)]
|
||||
[(vector (== init-command)) (init)]
|
||||
[(vector (== add-command) a) (add a)]
|
||||
[(vector (== rm-command) a) (rm a)]
|
||||
[(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)]
|
||||
[_ (ls)]))
|
||||
[(vector (== command-update) n text) (update n text)]
|
||||
[(vector (== command-add) a) (add a)]
|
||||
[(vector (== command-rm) a) (rm a)]
|
||||
[(vector (== command-ls)) (ls)]
|
||||
[(vector (== command-init)) (init)]
|
||||
[(or (vector (== command-help-1))
|
||||
(vector (== command-help-2))
|
||||
(vector (== command-help-3))) (help)]
|
||||
[_ (displayln-messages-ref 'error-incorrect-usage)]))
|
||||
|
||||
(define (main vectorof-args)
|
||||
(process-args vectorof-args))
|
||||
|
|
Loading…
Reference in New Issue