changed main file to a directory instead

main
m455 2018-04-05 20:10:10 -04:00
parent 328d4c3297
commit 1c26fa59fc
1 changed files with 32 additions and 38 deletions

View File

@ -1,34 +1,23 @@
#! /usr/bin/env racket #! /usr/bin/env racket
#lang racket/base #lang racket/base
(require json racket/vector) (require racket/vector)
(define program-name "rodo") (define program-name "rodo")
(define program-file ".rodo") (define program-directory ".rodo/")
(define program-path "~/") (define program-path "~/")
(define (d-hash-ref hash-list key) (define (d-hash-ref key)
(displayln (hash-ref hash-list key))) (displayln (hash-ref messages key)))
;; if no command-line arguments are called this will throw an error, even if the function is never called :(
;;(define (add-to-list)
;; (let ([args (vector-ref (current-command-line-arguments) 1)])
;; (string-append "> Added " args " to list")))
;;(define (remove-from-list)
;; (let ([args (vector-ref (current-command-line-arguments) 1)])
;; (string-append "> Removed " args " from list")))
;; 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-creating (string-append "> Creating a " program-file " file in your " program-path " directory...") 'creating (string-append "> Creating a " program-directory " folder in " program-path "...")
'file-creation-error (string-append "> Error: Could not create " program-file " in your " program-path " directory.\n> This may be due to directory permissions") 'creation-error (string-append "> Error: Could not create " program-directory " in " program-path ".\n> This may be due to directory permissions")
'file-already-exists (string-append "> " program-file " file already exists in " program-path) 'already-exists (string-append "> " program-directory " folder already exists in " program-path)
'file-successfully-created (string-append "> " program-path program-file " have been successfully created") 'successfully-created (string-append "> " program-path program-directory " has been successfully created")
'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]") 'not-found (string-append "> " program-directory " has not been setup in " program-path "\n> Would you like to set it up now? [y/n]")
'item-added "> Added item to list" 'item-added "> Added item to list"
'item-removed "> Added item to list" 'item-removed "> Added item to list"
'terminating (string-append "> Exiting " program-name "...") 'terminating (string-append "> Exiting " program-name "...")
@ -40,29 +29,33 @@
'yes '("yes" "Yes" "y" "Y") 'yes '("yes" "Yes" "y" "Y")
'no '("no" "No" "n" "N"))) 'no '("no" "No" "n" "N")))
;; yet again, less typing for opening a file (define (open/create-file list-name)
(define (open/create-file path) (let ([path (expand-user-path (string-append program-path program-directory list-name))])
(let ([path (expand-user-path path)])
(let ([opened-file (open-output-file path (let ([opened-file (open-output-file path
#:mode 'text #:mode 'text
#:exists 'can-update)]) #:exists 'can-update)])
(close-output-port opened-file)))) (close-output-port opened-file))))
(define (create-folder)
(make-directory (expand-user-path (string-append program-path program-directory))))
(define (check-for-folder)
(directory-exists? (expand-user-path (string-append program-path program-directory))))
;; prompt user for file initial file creation ;; prompt user for file initial file creation
(define (prompt-user chosen-message) (define (prompt-user chosen-message)
(d-hash-ref messages chosen-message) (d-hash-ref chosen-message)
(display "> ") (display "> ")
(let ([user-input (read-line)]) (let ([user-input (read-line)])
(cond (cond
[(member user-input (hash-ref y/n 'yes)) [(member user-input (hash-ref y/n 'yes))
(d-hash-ref messages 'file-creating) (d-hash-ref 'creating)
(open/create-file (string-append program-path program-file)) (create-folder)
(if (file-exists? (expand-user-path (string-append program-path program-file))) (if (check-for-folder)
(d-hash-ref messages 'file-successfully-created) (d-hash-ref 'successfully-created)
(d-hash-ref messages 'file-creation-error))] (d-hash-ref 'creation-error))]
[(member user-input (hash-ref y/n 'no)) [(member user-input (hash-ref y/n 'no))
(d-hash-ref messages 'terminating)] (d-hash-ref 'terminating)]
[else [else
(prompt-user 'choose-y/n)]))) (prompt-user 'choose-y/n)])))
@ -70,19 +63,20 @@
(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 '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 'item-added)]
[(and (equal? args-length 2) (equal? (vector-member "remove" args) 0)) [(and (equal? args-length 2) (equal? (vector-member "remove" args) 0))
(d-hash-ref messages 'item-removed)] (d-hash-ref '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?)]
[else (d-hash-ref 'incorrect-usage)])))
;; does the file exist that holds the list(s?) ;; does the file exist that holds the list(s?)
(define (todo-list-exist?) (define (todo-list-exist?)
(if (file-exists? (expand-user-path (string-append program-path program-file))) (if (check-for-folder)
(d-hash-ref messages 'file-already-exists) (d-hash-ref 'already-exists)
(prompt-user 'file-not-found))) (prompt-user 'not-found)))
(define (main) (define (main)
(check-args (current-command-line-arguments))) (check-args (current-command-line-arguments)))