2018-04-06 01:44:21 +00:00
|
|
|
#! /usr/bin/env racket
|
|
|
|
#lang racket/base
|
2018-04-02 19:16:01 +00:00
|
|
|
|
2018-04-06 14:51:42 +00:00
|
|
|
(require racket/vector racket/file)
|
2018-03-12 14:02:54 +00:00
|
|
|
|
2018-03-26 17:43:17 +00:00
|
|
|
(define program-name "rodo")
|
2018-04-06 00:10:10 +00:00
|
|
|
(define program-directory ".rodo/")
|
2018-04-02 18:56:59 +00:00
|
|
|
(define program-path "~/")
|
2018-04-06 14:30:38 +00:00
|
|
|
(define program-file "todo-list")
|
2018-03-26 17:43:17 +00:00
|
|
|
|
2018-04-06 00:38:58 +00:00
|
|
|
(define (d-hash-ref hash-list key)
|
2018-04-06 03:48:41 +00:00
|
|
|
(display (hash-ref hash-list key)))
|
2018-04-06 00:38:58 +00:00
|
|
|
|
|
|
|
(define (d-vector-ref args key)
|
|
|
|
(display (vector-ref args key)))
|
2018-04-02 18:56:59 +00:00
|
|
|
|
2018-03-26 17:43:17 +00:00
|
|
|
(define messages
|
|
|
|
(hash
|
2018-04-06 03:56:08 +00:00
|
|
|
'incorrect-usage
|
|
|
|
(string-append
|
|
|
|
"> For usage type "
|
|
|
|
"`" program-name " -h`"
|
|
|
|
" or "
|
|
|
|
"`" program-name " --help`\n")
|
|
|
|
|
2018-04-06 14:30:38 +00:00
|
|
|
'creating-folder
|
2018-04-06 03:56:08 +00:00
|
|
|
(string-append
|
2018-04-06 14:30:38 +00:00
|
|
|
"> creating a "
|
2018-04-06 03:56:08 +00:00
|
|
|
program-directory
|
|
|
|
" folder in "
|
|
|
|
program-path " ...\n")
|
|
|
|
|
2018-04-06 14:30:38 +00:00
|
|
|
'creating-file
|
|
|
|
(string-append
|
|
|
|
"> creating a "
|
|
|
|
program-file
|
2018-04-07 14:33:34 +00:00
|
|
|
" file in "
|
2018-04-06 14:30:38 +00:00
|
|
|
program-path
|
|
|
|
program-directory " ...\n")
|
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
'creation-error
|
|
|
|
(string-append
|
|
|
|
"> Error: Could not create "
|
|
|
|
program-directory
|
|
|
|
" in "
|
|
|
|
program-path ".\n"
|
|
|
|
"> This may be due to directory permissions\n")
|
|
|
|
|
|
|
|
'already-exists
|
|
|
|
(string-append
|
|
|
|
"> "
|
|
|
|
program-directory
|
|
|
|
" folder already exists in "
|
|
|
|
program-path "\n")
|
|
|
|
|
|
|
|
'successfully-created
|
|
|
|
(string-append
|
|
|
|
"> "
|
2018-04-06 14:30:38 +00:00
|
|
|
program-path
|
|
|
|
program-directory
|
|
|
|
program-file
|
2018-04-06 03:56:08 +00:00
|
|
|
" has been successfully created\n")
|
|
|
|
|
|
|
|
'not-found
|
|
|
|
(string-append
|
|
|
|
"> "
|
|
|
|
program-directory
|
|
|
|
" has not been setup in "
|
|
|
|
program-path "\n")
|
|
|
|
|
|
|
|
'setup-y/n
|
|
|
|
(string-append
|
|
|
|
"> Would you like to set it up now? [y/n]\n")
|
|
|
|
|
|
|
|
'try-initializing
|
|
|
|
(string-append
|
|
|
|
"> Try typing "
|
|
|
|
"`" program-name
|
|
|
|
" init` "
|
|
|
|
"to set it up\n")
|
|
|
|
|
2018-04-06 04:12:58 +00:00
|
|
|
'terminating
|
|
|
|
(string-append
|
|
|
|
"> Exiting "
|
|
|
|
program-name
|
|
|
|
" ...\n")
|
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
'choose-y/n
|
|
|
|
"> Error: Please choose y or n\n"
|
|
|
|
|
|
|
|
'item-added-prefix
|
|
|
|
"> Added "
|
|
|
|
|
|
|
|
'item-added-suffix
|
|
|
|
" to list\n"
|
|
|
|
|
|
|
|
'item-removed-prefix
|
|
|
|
"> Removed "
|
|
|
|
|
|
|
|
'item-removed-suffix
|
2018-04-06 04:12:58 +00:00
|
|
|
" from list\n"))
|
2018-03-26 17:43:17 +00:00
|
|
|
|
|
|
|
(define y/n
|
|
|
|
(hash
|
2018-04-06 03:56:08 +00:00
|
|
|
'yes
|
|
|
|
'("yes" "Yes" "y" "Y")
|
|
|
|
|
|
|
|
'no
|
|
|
|
'("no" "No" "n" "N")))
|
2018-03-14 14:45:05 +00:00
|
|
|
|
2018-04-06 14:30:38 +00:00
|
|
|
(define (create-file)
|
2018-04-06 03:56:08 +00:00
|
|
|
(let
|
|
|
|
([path
|
|
|
|
(expand-user-path
|
|
|
|
(string-append
|
|
|
|
program-path
|
2018-04-06 14:30:38 +00:00
|
|
|
program-directory
|
|
|
|
program-file))])
|
2018-04-06 03:56:08 +00:00
|
|
|
(let
|
|
|
|
([opened-file
|
|
|
|
(open-output-file path
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'can-update)])
|
2018-04-06 14:51:42 +00:00
|
|
|
;;TODO:
|
2018-04-06 14:41:49 +00:00
|
|
|
;;I think i can write to file with
|
|
|
|
;;a function right here...
|
2018-04-06 14:51:42 +00:00
|
|
|
;;NOTE: this works when doing `./rodo.rkt init`,
|
|
|
|
;;just gotta separate it into another function
|
|
|
|
(write-to-file "test"
|
|
|
|
path
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'append)
|
2018-03-19 20:56:52 +00:00
|
|
|
(close-output-port opened-file))))
|
2018-03-13 16:39:10 +00:00
|
|
|
|
2018-04-06 00:10:10 +00:00
|
|
|
(define (create-folder)
|
2018-04-06 03:56:08 +00:00
|
|
|
(make-directory
|
|
|
|
(expand-user-path
|
|
|
|
(string-append
|
|
|
|
program-path
|
|
|
|
program-directory))))
|
2018-04-06 00:10:10 +00:00
|
|
|
|
2018-04-06 14:30:38 +00:00
|
|
|
(define (check-for-file)
|
|
|
|
(file-exists?
|
|
|
|
(expand-user-path
|
|
|
|
(string-append
|
|
|
|
program-path
|
|
|
|
program-directory
|
|
|
|
program-file))))
|
|
|
|
|
2018-04-06 00:10:10 +00:00
|
|
|
(define (check-for-folder)
|
2018-04-06 03:56:08 +00:00
|
|
|
(directory-exists?
|
|
|
|
(expand-user-path
|
|
|
|
(string-append
|
|
|
|
program-path
|
|
|
|
program-directory))))
|
2018-04-06 00:10:10 +00:00
|
|
|
|
2018-04-06 04:23:30 +00:00
|
|
|
(define (prompt-user hash-list key)
|
|
|
|
(d-hash-ref hash-list key)
|
2018-03-26 17:43:17 +00:00
|
|
|
(display "> ")
|
2018-04-06 03:56:08 +00:00
|
|
|
(let
|
|
|
|
([user-input (read-line)])
|
2018-03-13 02:19:16 +00:00
|
|
|
(cond
|
2018-03-16 13:53:12 +00:00
|
|
|
[(member user-input (hash-ref y/n 'yes))
|
2018-04-06 14:30:38 +00:00
|
|
|
(d-hash-ref messages 'creating-folder)
|
|
|
|
(d-hash-ref messages 'creating-file)
|
2018-04-06 00:10:10 +00:00
|
|
|
(create-folder)
|
2018-04-06 14:30:38 +00:00
|
|
|
(create-file)
|
|
|
|
(if
|
|
|
|
(and
|
|
|
|
(check-for-folder)
|
|
|
|
(check-for-file))
|
2018-04-06 00:38:58 +00:00
|
|
|
(d-hash-ref messages 'successfully-created)
|
|
|
|
(d-hash-ref messages 'creation-error))]
|
2018-04-06 02:08:33 +00:00
|
|
|
|
2018-03-16 13:53:12 +00:00
|
|
|
[(member user-input (hash-ref y/n 'no))
|
2018-04-06 00:38:58 +00:00
|
|
|
(d-hash-ref messages 'terminating)]
|
2018-04-06 02:08:33 +00:00
|
|
|
|
2018-03-13 02:19:16 +00:00
|
|
|
[else
|
2018-04-06 04:23:30 +00:00
|
|
|
(prompt-user messages 'choose-y/n)])))
|
2018-04-02 15:37:33 +00:00
|
|
|
|
2018-04-06 02:42:18 +00:00
|
|
|
(define (add-item args)
|
2018-04-06 14:34:40 +00:00
|
|
|
(if
|
|
|
|
(and
|
|
|
|
(check-for-folder)
|
|
|
|
(check-for-file))
|
2018-04-06 02:42:18 +00:00
|
|
|
(begin
|
|
|
|
(d-hash-ref messages 'item-added-prefix)
|
|
|
|
(d-vector-ref args 1)
|
|
|
|
(d-hash-ref messages 'item-added-suffix))
|
|
|
|
(begin
|
|
|
|
(d-hash-ref messages 'not-found)
|
|
|
|
(d-hash-ref messages 'try-initializing))))
|
|
|
|
|
2018-04-06 04:12:58 +00:00
|
|
|
(define (remove-item args)
|
2018-04-06 14:34:40 +00:00
|
|
|
(if
|
|
|
|
(and
|
|
|
|
(check-for-folder)
|
|
|
|
(check-for-file))
|
2018-04-06 04:12:58 +00:00
|
|
|
(begin
|
|
|
|
(d-hash-ref messages 'item-removed-prefix)
|
|
|
|
(d-vector-ref args 1)
|
|
|
|
(d-hash-ref messages 'item-removed-suffix))
|
|
|
|
(begin
|
|
|
|
(d-hash-ref messages 'not-found)
|
|
|
|
(d-hash-ref messages 'try-initializing))))
|
|
|
|
|
2018-04-02 15:37:33 +00:00
|
|
|
(define (check-args args)
|
2018-04-06 03:56:08 +00:00
|
|
|
(let
|
|
|
|
([args-length (vector-length args)])
|
2018-04-02 17:51:08 +00:00
|
|
|
(cond
|
2018-04-06 03:56:08 +00:00
|
|
|
[(and
|
|
|
|
(equal? args-length 2)
|
|
|
|
(equal? (vector-member "add" args) 0))
|
2018-04-06 02:42:18 +00:00
|
|
|
(add-item args)]
|
2018-04-06 00:51:22 +00:00
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
[(and
|
|
|
|
(equal? args-length 2)
|
|
|
|
(equal? (vector-member "remove" args) 0))
|
2018-04-06 04:12:58 +00:00
|
|
|
(remove-item args)]
|
2018-04-06 00:51:22 +00:00
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
[(and
|
|
|
|
(equal? args-length 1)
|
|
|
|
(equal? (vector-member "init" args) 0))
|
2018-04-06 14:30:38 +00:00
|
|
|
(todo-already-exists?)]
|
2018-04-06 00:51:22 +00:00
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
[else
|
|
|
|
(d-hash-ref messages 'incorrect-usage)])))
|
2018-04-02 15:37:33 +00:00
|
|
|
|
2018-04-06 14:30:38 +00:00
|
|
|
(define (todo-already-exists?)
|
2018-04-06 00:10:10 +00:00
|
|
|
(if (check-for-folder)
|
2018-04-06 00:38:58 +00:00
|
|
|
(d-hash-ref messages 'already-exists)
|
2018-04-06 02:42:18 +00:00
|
|
|
(begin
|
|
|
|
(d-hash-ref messages 'not-found)
|
2018-04-06 04:23:30 +00:00
|
|
|
(prompt-user messages 'setup-y/n))))
|
2018-03-12 14:02:54 +00:00
|
|
|
|
2018-04-06 02:43:03 +00:00
|
|
|
(define (main)
|
|
|
|
(check-args (current-command-line-arguments)))
|
2018-04-02 15:37:33 +00:00
|
|
|
|
2018-04-06 02:43:03 +00:00
|
|
|
(main)
|