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-08 04:48:16 +00:00
|
|
|
(require racket/vector
|
2018-04-09 23:19:56 +00:00
|
|
|
racket/file
|
2018-04-10 16:43:59 +00:00
|
|
|
racket/string
|
|
|
|
racket/list)
|
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-04-08 04:48:16 +00:00
|
|
|
(define bullet "*")
|
2018-04-10 00:03:12 +00:00
|
|
|
(define remove-command "rm")
|
|
|
|
(define add-command "add")
|
|
|
|
(define list-command "ls")
|
|
|
|
(define initialize-command "init")
|
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-08 04:48:16 +00:00
|
|
|
'show-usage
|
2018-04-06 03:56:08 +00:00
|
|
|
(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 "
|
2018-04-08 04:48:16 +00:00
|
|
|
program-file
|
|
|
|
" in "
|
2018-04-06 03:56:08 +00:00
|
|
|
program-directory
|
|
|
|
program-path ".\n"
|
|
|
|
"> This may be due to directory permissions\n")
|
|
|
|
|
2018-04-08 04:48:16 +00:00
|
|
|
'file-already-exists
|
2018-04-06 03:56:08 +00:00
|
|
|
(string-append
|
2018-04-08 04:48:16 +00:00
|
|
|
"> Error: "
|
|
|
|
program-name
|
|
|
|
" already exists in "
|
|
|
|
program-path
|
2018-04-06 03:56:08 +00:00
|
|
|
program-directory
|
2018-04-08 04:48:16 +00:00
|
|
|
program-file "\n")
|
2018-04-06 03:56:08 +00:00
|
|
|
|
|
|
|
'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")
|
|
|
|
|
2018-04-08 04:48:16 +00:00
|
|
|
'file-not-found
|
2018-04-06 03:56:08 +00:00
|
|
|
(string-append
|
2018-04-08 04:48:16 +00:00
|
|
|
"> Error: Could not find "
|
|
|
|
program-path
|
2018-04-06 03:56:08 +00:00
|
|
|
program-directory
|
2018-04-08 04:48:16 +00:00
|
|
|
program-file "\n")
|
2018-04-06 03:56:08 +00:00
|
|
|
|
2018-04-08 04:48:16 +00:00
|
|
|
'init-y/n
|
2018-04-06 03:56:08 +00:00
|
|
|
(string-append
|
2018-04-08 04:48:16 +00:00
|
|
|
"> A "
|
|
|
|
program-file
|
|
|
|
" file will be created in "
|
|
|
|
program-path
|
|
|
|
program-directory "\n"
|
|
|
|
"> Are you sure you want to continue? [y/n]\n")
|
2018-04-06 03:56:08 +00:00
|
|
|
|
2018-04-08 04:48:16 +00:00
|
|
|
'try-init
|
2018-04-06 03:56:08 +00:00
|
|
|
(string-append
|
|
|
|
"> Try typing "
|
2018-04-08 04:48:16 +00:00
|
|
|
"`" program-name " init` "
|
2018-04-06 03:56:08 +00:00
|
|
|
"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-10 00:25:00 +00:00
|
|
|
(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
|
2018-04-10 01:35:13 +00:00
|
|
|
(open-output-file
|
|
|
|
path
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'can-update)])
|
2018-04-10 00:25:00 +00:00
|
|
|
(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))))
|
|
|
|
|
2018-04-10 16:43:59 +00:00
|
|
|
(define (make-numbered lst)
|
|
|
|
(map
|
|
|
|
string-append
|
|
|
|
(map
|
|
|
|
number->string
|
|
|
|
(range (length lst)))
|
|
|
|
lst))
|
|
|
|
|
|
|
|
(define (add-spaces)
|
2018-04-09 23:40:47 +00:00
|
|
|
(lambda (lst)
|
2018-04-10 16:43:59 +00:00
|
|
|
(string-append ". " lst)))
|
2018-04-09 23:40:47 +00:00
|
|
|
|
2018-04-10 00:09:29 +00:00
|
|
|
(define (show-list-from-file)
|
2018-04-09 23:40:47 +00:00
|
|
|
(let
|
|
|
|
([path
|
|
|
|
(expand-user-path
|
|
|
|
(string-append
|
|
|
|
program-path
|
|
|
|
program-directory
|
|
|
|
program-file))])
|
|
|
|
(let
|
2018-04-10 16:43:59 +00:00
|
|
|
([todo-list
|
2018-04-09 23:40:47 +00:00
|
|
|
(file->lines path
|
|
|
|
#:mode 'text
|
|
|
|
#:line-mode 'linefeed)])
|
2018-04-09 23:19:56 +00:00
|
|
|
(display
|
|
|
|
(string-join
|
2018-04-10 16:43:59 +00:00
|
|
|
(make-numbered (map (add-spaces) todo-list))
|
2018-04-10 01:35:13 +00:00
|
|
|
"\n"
|
2018-04-10 00:03:12 +00:00
|
|
|
#:after-last "\n")))))
|
2018-04-09 23:19:56 +00:00
|
|
|
|
2018-04-10 00:25:00 +00:00
|
|
|
(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))))
|
|
|
|
|
2018-04-07 14:52:19 +00:00
|
|
|
(define (add-item-to-file item)
|
2018-04-09 23:19:56 +00:00
|
|
|
(let ([item (string-append item "\n")])
|
2018-04-07 14:46:43 +00:00
|
|
|
(let
|
|
|
|
([path
|
|
|
|
(expand-user-path
|
|
|
|
(string-append
|
|
|
|
program-path
|
|
|
|
program-directory
|
|
|
|
program-file))])
|
2018-04-07 14:52:19 +00:00
|
|
|
(display-to-file item
|
2018-04-08 04:48:16 +00:00
|
|
|
path
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'append))))
|
2018-04-07 14:46:43 +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
|
2018-04-07 14:46:43 +00:00
|
|
|
(add-item-to-file (vector-ref args 1))
|
2018-04-06 02:42:18 +00:00
|
|
|
(d-hash-ref messages 'item-added-prefix)
|
|
|
|
(d-vector-ref args 1)
|
|
|
|
(d-hash-ref messages 'item-added-suffix))
|
|
|
|
(begin
|
2018-04-08 04:48:16 +00:00
|
|
|
(d-hash-ref messages 'file-not-found)
|
|
|
|
(d-hash-ref messages 'try-init))))
|
2018-04-06 02:42:18 +00:00
|
|
|
|
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
|
2018-04-08 04:48:16 +00:00
|
|
|
;; TODO (remove-item-from-file (vector-ref args 1))
|
2018-04-06 04:12:58 +00:00
|
|
|
(d-hash-ref messages 'item-removed-prefix)
|
|
|
|
(d-vector-ref args 1)
|
|
|
|
(d-hash-ref messages 'item-removed-suffix))
|
|
|
|
(begin
|
2018-04-08 04:48:16 +00:00
|
|
|
(d-hash-ref messages 'file-not-found)
|
|
|
|
(d-hash-ref messages 'try-init))))
|
2018-04-06 04:12:58 +00:00
|
|
|
|
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-10 00:03:12 +00:00
|
|
|
[(and
|
|
|
|
(equal? args-length 1)
|
|
|
|
(equal? (vector-member list-command args) 0))
|
|
|
|
(show-list)]
|
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
[(and
|
|
|
|
(equal? args-length 2)
|
2018-04-10 00:03:12 +00:00
|
|
|
(equal? (vector-member add-command 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)
|
2018-04-10 00:03:12 +00:00
|
|
|
(equal? (vector-member remove-command 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)
|
2018-04-10 00:03:12 +00:00
|
|
|
(equal? (vector-member initialize-command args) 0))
|
2018-04-08 04:48:16 +00:00
|
|
|
(initialize)]
|
2018-04-06 00:51:22 +00:00
|
|
|
|
2018-04-06 03:56:08 +00:00
|
|
|
[else
|
2018-04-08 04:48:16 +00:00
|
|
|
(d-hash-ref messages 'show-usage)])))
|
2018-04-02 15:37:33 +00:00
|
|
|
|
2018-04-10 00:25:00 +00:00
|
|
|
(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)])))
|
|
|
|
|
2018-04-08 04:48:16 +00:00
|
|
|
(define (initialize)
|
|
|
|
(if (check-for-file)
|
|
|
|
(d-hash-ref messages 'file-already-exists)
|
2018-04-06 02:42:18 +00:00
|
|
|
(begin
|
2018-04-09 23:19:56 +00:00
|
|
|
(init-prompt messages 'init-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)
|