diff --git a/README.md b/README.md index fd65831..7ca3033 100755 --- a/README.md +++ b/README.md @@ -12,4 +12,3 @@ You can try it out while it's being developed (**note: it requires that you have `./rodo add "go to the bank"` -> add "go to the bank" to the list `./rodo ls` -> list your todo list items - diff --git a/args.rkt b/args.rkt new file mode 100644 index 0000000..7b83294 --- /dev/null +++ b/args.rkt @@ -0,0 +1,37 @@ +#lang racket/base + +(require racket/vector + "config.rkt" + "todo-file.rkt" + "init.rkt" + "list-hash.rkt" + "messages.rkt") + +(provide (all-defined-out)) + +(define (check-args args) + (let + ([args-length (vector-length args)]) + (cond + [(and + (equal? args-length 1) + (equal? (vector-member list-command args) 0)) + (show-list)] + + [(and + (equal? args-length 2) + (equal? (vector-member add-command args) 0)) + (add-item args)] + + [(and + (equal? args-length 2) + (equal? (vector-member remove-command args) 0)) + (remove-item args)] + + [(and + (equal? args-length 1) + (equal? (vector-member initialize-command args) 0)) + (initialize)] + + [else + (d-hash-ref messages 'show-usage)]))) diff --git a/config.rkt b/config.rkt index e94491e..c0b963a 100644 --- a/config.rkt +++ b/config.rkt @@ -1,4 +1,5 @@ #lang racket/base + (provide (all-defined-out)) (define program-name "rodo") @@ -9,4 +10,3 @@ (define add-command "add") (define list-command "ls") (define initialize-command "init") - diff --git a/init.rkt b/init.rkt new file mode 100644 index 0000000..22e588f --- /dev/null +++ b/init.rkt @@ -0,0 +1,38 @@ +#lang racket/base + +(require "config.rkt" + "list-hash.rkt" + "messages.rkt" + "io.rkt") + +(provide (all-defined-out)) + +(define (init-prompt hash-list key) + (d-hash-ref hash-list key) + (display "> ") + (let + ([user-input (read-line)]) + (cond + [(member user-input (hash-ref y/n 'yes)) + (d-hash-ref messages 'creating-folder) + (d-hash-ref messages 'creating-file) + (create-folder) + (create-file) + (if + (and + (check-for-folder) + (check-for-file)) + (d-hash-ref messages 'successfully-created) + (d-hash-ref messages 'creation-error))] + + [(member user-input (hash-ref y/n 'no)) + (d-hash-ref messages 'terminating)] + + [else + (init-prompt messages 'choose-y/n)]))) + +(define (initialize) + (if (check-for-file) + (d-hash-ref messages 'file-already-exists) + (begin + (init-prompt messages 'init-y/n)))) diff --git a/io.rkt b/io.rkt new file mode 100644 index 0000000..b3c0509 --- /dev/null +++ b/io.rkt @@ -0,0 +1,43 @@ +#lang racket/base + +(require "config.rkt") + +(provide (all-defined-out)) + +(define (check-for-file) + (file-exists? + (expand-user-path + (string-append + program-path + program-directory + program-file)))) + +(define (create-file) + (let + ([path + (expand-user-path + (string-append + program-path + program-directory + program-file))]) + (let + ([opened-file + (open-output-file + path + #:mode 'text + #:exists 'can-update)]) + (close-output-port opened-file)))) + +(define (check-for-folder) + (directory-exists? + (expand-user-path + (string-append + program-path + program-directory)))) + +(define (create-folder) + (make-directory + (expand-user-path + (string-append + program-path + program-directory)))) diff --git a/list-hash.rkt b/list-hash.rkt new file mode 100644 index 0000000..b64b254 --- /dev/null +++ b/list-hash.rkt @@ -0,0 +1,11 @@ +#lang racket/base + +(require "config.rkt") + +(provide (all-defined-out)) + +(define (d-hash-ref hash-list key) + (display (hash-ref hash-list key))) + +(define (d-vector-ref args key) + (display (vector-ref args key))) diff --git a/messages.rkt b/messages.rkt index b0132e0..fcb9875 100644 --- a/messages.rkt +++ b/messages.rkt @@ -1,105 +1,106 @@ #lang racket/base + (require "config.rkt") + (provide (all-defined-out)) -(define messages - (hash - 'show-usage - (string-append - "> For usage type " - "`" program-name " -h`" - " or " +(define messages + (hash + 'show-usage + (string-append + "> For usage type " + "`" program-name " -h`" + " or " "`" program-name " --help`\n") - 'creating-folder - (string-append - "> creating a " - program-directory - " folder in " + 'creating-folder + (string-append + "> creating a " + program-directory + " folder in " program-path " ...\n") - 'creating-file - (string-append - "> creating a " - program-file + 'creating-file + (string-append + "> creating a " + program-file " file in " program-path program-directory " ...\n") - 'creation-error + 'creation-error (string-append - "> Error: Could not create " + "> Error: Could not create " program-file " in " - program-directory - program-path ".\n" + program-directory + program-path ".\n" "> This may be due to directory permissions\n") - 'file-already-exists - (string-append - "> Error: " - program-name - " already exists in " - program-path - program-directory + 'file-already-exists + (string-append + "> Error: " + program-name + " already exists in " + program-path + program-directory program-file "\n") - 'successfully-created - (string-append - "> " - program-path - program-directory + 'successfully-created + (string-append + "> " + program-path + program-directory program-file - " has been successfully created\n") + " has been successfully created\n") - 'file-not-found - (string-append - "> Error: Could not find " - program-path - program-directory + 'file-not-found + (string-append + "> Error: Could not find " + program-path + program-directory program-file "\n") - 'init-y/n + 'init-y/n (string-append "> A " - program-file + program-file " file will be created in " program-path program-directory "\n" "> Are you sure you want to continue? [y/n]\n") - 'try-init + 'try-init (string-append "> Try typing " "`" program-name " init` " "to set it up\n") - 'terminating - (string-append - "> Exiting " - program-name + 'terminating + (string-append + "> Exiting " + program-name " ...\n") 'choose-y/n "> Error: Please choose y or n\n" - 'item-added-prefix - "> Added " + 'item-added-prefix + "> Added " - 'item-added-suffix - " to list\n" + 'item-added-suffix + " to list\n" - 'item-removed-prefix + 'item-removed-prefix "> Removed " - 'item-removed-suffix + 'item-removed-suffix " from list\n")) -(define y/n +(define y/n (hash - 'yes + 'yes '("yes" "Yes" "y" "Y") - 'no + 'no '("no" "No" "n" "N"))) - diff --git a/rodo.rkt b/rodo.rkt index 354d55b..04f7b96 100755 --- a/rodo.rkt +++ b/rodo.rkt @@ -1,196 +1,7 @@ -#! /usr/bin/env racket +#! /usr/bin/env racket #lang racket/base -(require racket/vector - racket/file - racket/string - racket/list - "config.rkt" - "messages.rkt") - -(define (d-hash-ref hash-list key) - (display (hash-ref hash-list key))) - -(define (d-vector-ref args key) - (display (vector-ref args key))) - -(define (check-for-file) - (file-exists? - (expand-user-path - (string-append - program-path - program-directory - program-file)))) - -(define (create-file) - (let - ([path - (expand-user-path - (string-append - program-path - program-directory - program-file))]) - (let - ([opened-file - (open-output-file - path - #:mode 'text - #:exists 'can-update)]) - (close-output-port opened-file)))) - -(define (check-for-folder) - (directory-exists? - (expand-user-path - (string-append - program-path - program-directory)))) - -(define (create-folder) - (make-directory - (expand-user-path - (string-append - program-path - program-directory)))) - -(define (make-numbered lst) - (map - string-append - (map - number->string - (range (length lst))) - lst)) - -(define (add-spaces) - (lambda (lst) - (string-append ". " lst))) - -(define (show-list-from-file) - (let - ([path - (expand-user-path - (string-append - program-path - program-directory - program-file))]) - (let - ([todo-list - (file->lines path - #:mode 'text - #:line-mode 'linefeed)]) - (display - (string-join - (make-numbered (map (add-spaces) todo-list)) - "\n" - #:after-last "\n"))))) - -(define (show-list) - (if - (and - (check-for-folder) - (check-for-file)) - (show-list-from-file) - (begin - (d-hash-ref messages 'file-not-found) - (d-hash-ref messages 'try-init)))) - -(define (add-item-to-file item) - (let ([item (string-append item "\n")]) - (let - ([path - (expand-user-path - (string-append - program-path - program-directory - program-file))]) - (display-to-file item - path - #:mode 'text - #:exists 'append)))) - -(define (add-item args) - (if - (and - (check-for-folder) - (check-for-file)) - (begin - (add-item-to-file (vector-ref args 1)) - (d-hash-ref messages 'item-added-prefix) - (d-vector-ref args 1) - (d-hash-ref messages 'item-added-suffix)) - (begin - (d-hash-ref messages 'file-not-found) - (d-hash-ref messages 'try-init)))) - -(define (remove-item args) - (if - (and - (check-for-folder) - (check-for-file)) - (begin - ;; TODO (remove-item-from-file (vector-ref args 1)) - (d-hash-ref messages 'item-removed-prefix) - (d-vector-ref args 1) - (d-hash-ref messages 'item-removed-suffix)) - (begin - (d-hash-ref messages 'file-not-found) - (d-hash-ref messages 'try-init)))) - -(define (check-args args) - (let - ([args-length (vector-length args)]) - (cond - [(and - (equal? args-length 1) - (equal? (vector-member list-command args) 0)) - (show-list)] - - [(and - (equal? args-length 2) - (equal? (vector-member add-command args) 0)) - (add-item args)] - - [(and - (equal? args-length 2) - (equal? (vector-member remove-command args) 0)) - (remove-item args)] - - [(and - (equal? args-length 1) - (equal? (vector-member initialize-command args) 0)) - (initialize)] - - [else - (d-hash-ref messages 'show-usage)]))) - -(define (init-prompt hash-list key) - (d-hash-ref hash-list key) - (display "> ") - (let - ([user-input (read-line)]) - (cond - [(member user-input (hash-ref y/n 'yes)) - (d-hash-ref messages 'creating-folder) - (d-hash-ref messages 'creating-file) - (create-folder) - (create-file) - (if - (and - (check-for-folder) - (check-for-file)) - (d-hash-ref messages 'successfully-created) - (d-hash-ref messages 'creation-error))] - - [(member user-input (hash-ref y/n 'no)) - (d-hash-ref messages 'terminating)] - - [else - (init-prompt messages 'choose-y/n)]))) - -(define (initialize) - (if (check-for-file) - (d-hash-ref messages 'file-already-exists) - (begin - (init-prompt messages 'init-y/n)))) +(require "args.rkt") (define (main) (check-args (current-command-line-arguments))) diff --git a/todo-file.rkt b/todo-file.rkt new file mode 100644 index 0000000..a550668 --- /dev/null +++ b/todo-file.rkt @@ -0,0 +1,94 @@ +#lang racket/base + +(require racket/list + racket/file + racket/string + "config.rkt" + "io.rkt" + "list-hash.rkt" + "messages.rkt") + +(provide (all-defined-out)) + +(define (make-numbered lst) + (map + string-append + (map + number->string + (range (length lst))) + lst)) + +(define (add-spaces) + (lambda (lst) + (string-append ". " lst))) + +(define (show-list-from-file) + (let + ([path + (expand-user-path + (string-append + program-path + program-directory + program-file))]) + (let + ([todo-list + (file->lines path + #:mode 'text + #:line-mode 'linefeed)]) + (display + (string-join + (make-numbered (map (add-spaces) todo-list)) + "\n" + #:after-last "\n"))))) + +(define (show-list) + (if + (and + (check-for-folder) + (check-for-file)) + (show-list-from-file) + (begin + (d-hash-ref messages 'file-not-found) + (d-hash-ref messages 'try-init)))) + +(define (add-item-to-file item) + (let ([item (string-append item "\n")]) + (let + ([path + (expand-user-path + (string-append + program-path + program-directory + program-file))]) + (display-to-file item + path + #:mode 'text + #:exists 'append)))) + +(define (add-item args) + (if + (and + (check-for-folder) + (check-for-file)) + (begin + (add-item-to-file (vector-ref args 1)) + (d-hash-ref messages 'item-added-prefix) + (d-vector-ref args 1) + (d-hash-ref messages 'item-added-suffix)) + (begin + (d-hash-ref messages 'file-not-found) + (d-hash-ref messages 'try-init)))) + +(define (remove-item args) + (if + (and + (check-for-folder) + (check-for-file)) + (begin + ;; TODO (remove-item-from-file (vector-ref args 1)) + (d-hash-ref messages 'item-removed-prefix) + (d-vector-ref args 1) + (d-hash-ref messages 'item-removed-suffix)) + (begin + (d-hash-ref messages 'file-not-found) + (d-hash-ref messages 'try-init))))