#lang racket/base (require racket/file racket/list racket/match racket/string) ;; ------------------------------------------------ ;; Constants ;; ------------------------------------------------ (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 (path->string (build-path (find-system-path 'home-dir) program-file))) (define newline "\n") (define double-newline "\n\n") ;; ------------------------------------------------ ;; Messages ;; ------------------------------------------------ (define messages (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 command-help-1) '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-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 command-init) 'error-item-not-found "Error: Item not found." 'error-not-an-option "Error: Not an option." 'error-not-a-number "Error: Not a number." '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." 'updated-item "Updated item number ~a." 'added "Added '~a' to your list." 'removed "Removed '~a' from your list.")) ;; ------------------------------------------------ ;; helpers ;; ------------------------------------------------ (define (messages-ref key) (hash-ref messages key)) (define (displayln-messages-ref key) (displayln (messages-ref key))) (define (displayln-format-messages-ref key value) (displayln (format (messages-ref key) value))) (define (create-file string) (close-output-port (open-output-file string))) ;; ------------------------------------------------ ;; init ;; ------------------------------------------------ (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-symbol (string->symbol (read-line))]) (case input-symbol ['y (init/create-file)] ['n (init/cancel)] [else (displayln-messages-ref 'error-not-an-option)]))) (define (init) (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) (let* ([numbers (map number->string (range (length listof-items)))] [numbered-items (map (lambda (a b) (string-append a ". " b)) numbers listof-items)]) (for ([item numbered-items]) (displayln item)))) (define (ls) (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))) (displayln-messages-ref 'error-couldnt-find-file))) ;; ------------------------------------------------ ;; rm ;; ------------------------------------------------ (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-format-messages-ref 'removed item-to-remove)) (displayln-messages-ref 'error-item-not-found)))) (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 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* ([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 '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 ;; ------------------------------------------------ (define (help) (displayln (string-append "Usage:" newline (format " ~a [] []" program-name) double-newline "Commands:" newline (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) newline (format " ~a - Replaces the contents of an item with new text." command-update) double-newline "Examples:" newline (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) newline (format " ~a ~a 2 \"This is new text!\"" program-name command-update)))) (define (process-args vectorof-args) (match vectorof-args [(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)) (main (current-command-line-arguments))