main
m455 2018-04-02 14:56:59 -04:00
parent 7f93bc2727
commit dd399ff6e6
1 changed files with 28 additions and 18 deletions

View File

@ -2,37 +2,40 @@
#lang racket/base #lang racket/base
(require json racket/vector) (require json racket/vector)
;; set the name of the program here
(define program-name "rodo") (define program-name "rodo")
;; set which path the program will live in (define program-file ".rodo")
(define program-path "~/.")
(define program-path "~/")
;; because I don't want to type `(display (hash-ref...` over and over again ;; because I don't want to type `(display (hash-ref...` over and over again
(define (d-hash-ref hash-list key) (define (d-hash-ref hash-list key)
(displayln (hash-ref hash-list key))) (displayln (hash-ref hash-list key)))
#|(define (add-to-list) ;;(define (add-to-list)
(let ([args (vector-ref (current-command-line-arguments) 1)]) ;; (let ([args (vector-ref (current-command-line-arguments) 1)])
(string-append "> Added " args " to list"))) ;; (string-append "> Added " args " to list")))
(define (remove-from-list) ;;(define (remove-from-list)
(let ([args (vector-ref (current-command-line-arguments) 1)]) ;; (let ([args (vector-ref (current-command-line-arguments) 1)])
(string-append "> Removed " args " from list"))) ;; (string-append "> Removed " args " from list")))
|#
;; define all messages for quick access ;; define all messages for quick access
(define messages (define messages
(hash (hash
'incorrect-usage (string-append "> For usage type `" program-name " -h` or `" program-name " --help`") 'incorrect-usage (string-append "> For usage type `" program-name " -h` or `" program-name " --help`")
'file-not-found (string-append "> " program-name " has not been setup in your home directory\n> Would you like to set it up now? [y/n]")
'file-exists (string-append "> ." program-name " file exists") 'file-not-found (string-append "> " program-file " has not been setup in your " program-path " directory\n> Would you like to set it up now? [y/n]")
'creating-file (string-append "> Creating ." program-name " file in your home directory...") 'file-exists (string-append "> " program-file " file exists")
'creating-file (string-append "> Creating a " program-file " file in your " program-path " directory...")
'file-exists-now (string-append "> " program-path program-file " have been successfully created")
'error-creating-file (string-append "> There was an error creating " program-file " in your " program-path " path.\n> This may be due to directory permissions")
'item-added "> Added item to list" 'item-added "> Added item to list"
'item-removed "> Added item to list" 'item-removed "> Added item to list"
'initializing (string-append "> Initializing " program-name " in your home directory")
'terminating (string-append "> Exiting " program-name "...") 'terminating (string-append "> Exiting " program-name "...")
'choose-y/n "> Error: Please choose y or n")) 'choose-y/n "> Error: Please choose y or n"))
@ -58,25 +61,32 @@
(cond (cond
[(member user-input (hash-ref y/n 'yes)) [(member user-input (hash-ref y/n 'yes))
(d-hash-ref messages 'creating-file) (d-hash-ref messages 'creating-file)
(open/create-file (string-append program-path program-name))] (open/create-file (string-append program-path program-file))
(if (file-exists? (expand-user-path (string-append program-path program-file)))
(d-hash-ref messages 'file-exists-now)
(d-hash-ref messages 'error-creating-file))]
[(member user-input (hash-ref y/n 'no)) [(member user-input (hash-ref y/n 'no))
(d-hash-ref messages 'terminating)] (d-hash-ref messages 'terminating)]
[else [else
(d-hash-ref messages 'choose-y/n) (prompt-initialize 'choose-y/n)])))
(prompt-initialize 'file-not-found)])))
(define (check-args args) (define (check-args args)
(let ([args-length (vector-length args)]) (let ([args-length (vector-length args)])
(cond (cond
[(or (equal? args-length 0) (> args-length 2)) [(or (equal? args-length 0) (> args-length 2))
(d-hash-ref messages 'incorrect-usage)] (d-hash-ref messages 'incorrect-usage)]
[(and (equal? args-length 2) (equal? (vector-member "add" args) 0)) [(and (equal? args-length 2) (equal? (vector-member "add" args) 0))
(d-hash-ref messages 'item-added)] (d-hash-ref messages 'item-added)]
[(and (equal? args-length 2) (equal? (vector-member "remove" args) 0))
(d-hash-ref messages 'item-removed)]
[(and (equal? args-length 1) (equal? (vector-member "init" args) 0)) [(and (equal? args-length 1) (equal? (vector-member "init" args) 0))
(todo-list-exist?)]))) (todo-list-exist?)])))
(define (todo-list-exist?) (define (todo-list-exist?)
(if (file-exists? (expand-user-path (string-append program-path program-name))) (if (file-exists? (expand-user-path (string-append program-path program-file)))
(d-hash-ref messages 'file-exists) (d-hash-ref messages 'file-exists)
(prompt-initialize 'file-not-found))) (prompt-initialize 'file-not-found)))