changed a lot of functions
parent
91eed7fee9
commit
b69788bd91
64
args.rkt
64
args.rkt
|
@ -11,44 +11,38 @@
|
|||
|
||||
(define (check-args args)
|
||||
(let ([args-length (vector-length args)])
|
||||
(cond
|
||||
[(equal? args-length 0)
|
||||
(util:display-hash-ref messages:messages 'show-usage)]
|
||||
(cond [(equal? args-length 0)
|
||||
(util:display-hash-ref messages:messages 'show-usage)]
|
||||
|
||||
;; ls
|
||||
[(and
|
||||
(equal? args-length 1)
|
||||
(equal? (vector:vector-member config:list-command args) 0))
|
||||
(util:show-list-from-program-file)]
|
||||
;; ls
|
||||
[(and (equal? args-length 1)
|
||||
(equal? (vector:vector-member config:list-command args) 0))
|
||||
(util:show-list-from-file config:path-to-list-file)]
|
||||
|
||||
;; add
|
||||
[(and
|
||||
(or (equal? args-length 2) (>= args-length 2))
|
||||
(equal? (vector-ref args 0) config:add-command))
|
||||
(util:add-item-to-list args)]
|
||||
;; add
|
||||
[(and (or (equal? args-length 2) (>= args-length 2))
|
||||
(equal? (vector-ref args 0) config:add-command))
|
||||
(util:add-item-to-list config:path-to-list-file args)]
|
||||
|
||||
;; rm
|
||||
[(and
|
||||
(equal? args-length 2)
|
||||
(equal? (vector-ref args 0) config:remove-command)
|
||||
(real? (string->number (vector-ref args 1)))
|
||||
(or (positive? (string->number (vector-ref args 1)))
|
||||
(zero? (string->number (vector-ref args 1))))
|
||||
;; Length subtract one because the numbering starts at zero
|
||||
(not (> (string->number (vector-ref args 1)) (sub1 (length (util:file->string-list config:path-to-file)))))
|
||||
(util:remove-item-from-list args))]
|
||||
;; rm
|
||||
[(and (equal? args-length 2)
|
||||
(equal? (vector-ref args 0) config:remove-command)
|
||||
(real? (string->number (vector-ref args 1)))
|
||||
(or (positive? (string->number (vector-ref args 1)))
|
||||
(zero? (string->number (vector-ref args 1))))
|
||||
;; Length subtract one because the numbering starts at zero
|
||||
(not (> (string->number (vector-ref args 1)) (sub1 (length (util:file->string-list config:path-to-list-file))))))
|
||||
(util:remove-item-from-list config:path-to-list-file args)]
|
||||
|
||||
;; init
|
||||
[(and
|
||||
(equal? args-length 1)
|
||||
(equal? (vector:vector-member config:initialize-command args) 0))
|
||||
(init:initialize)]
|
||||
;; init
|
||||
[(and (equal? args-length 1)
|
||||
(equal? (vector:vector-member config:initialize-command args) 0))
|
||||
(init:initialize)]
|
||||
|
||||
;; help
|
||||
[(and
|
||||
(equal? args-length 1)
|
||||
(member (vector-ref args 0) config:help-command))
|
||||
(util:display-hash-ref messages:messages 'show-help)]
|
||||
;; help
|
||||
[(and (equal? args-length 1)
|
||||
(member (vector-ref args 0) config:help-command))
|
||||
(util:display-hash-ref messages:messages 'show-help)]
|
||||
|
||||
[else
|
||||
(util:display-hash-ref messages:messages 'show-usage)])))
|
||||
[else
|
||||
(util:display-hash-ref messages:messages 'show-usage)])))
|
||||
|
|
10
config.rkt
10
config.rkt
|
@ -2,11 +2,15 @@
|
|||
(provide (all-defined-out))
|
||||
|
||||
(define program-name "rodo")
|
||||
(define program-directory (path->string (expand-user-path "~/.rodo/")))
|
||||
(define program-file "todo.txt")
|
||||
(define program-directory
|
||||
(path->string
|
||||
(expand-user-path
|
||||
(string-append "~/." program-name "/"))))
|
||||
(define list-file "todo.txt")
|
||||
(define remove-command "rm")
|
||||
(define add-command "add")
|
||||
(define list-command "ls")
|
||||
(define initialize-command "init")
|
||||
(define help-command '("-h" "--help"))
|
||||
(define path-to-file (string-append program-directory program-file))
|
||||
(define path-to-list-file
|
||||
(string-append program-directory list-file))
|
||||
|
|
34
init.rkt
34
init.rkt
|
@ -10,27 +10,23 @@
|
|||
(define (init-prompt hash-list key)
|
||||
(util:display-hash-ref hash-list key)
|
||||
(display "> ")
|
||||
(let
|
||||
([user-input (read-line)])
|
||||
(cond
|
||||
[(member user-input (hash-ref messages:y/n 'yes))
|
||||
(util:display-hash-ref messages:messages 'creating)
|
||||
(util:create-program-directory)
|
||||
(util:create-program-file)
|
||||
(if (and
|
||||
(util:check-for-program-directory)
|
||||
(util:check-for-program-file))
|
||||
(util:display-hash-ref messages:messages 'successfully-created)
|
||||
(util:display-hash-ref messages:messages 'creation-error))]
|
||||
|
||||
[(member user-input (hash-ref messages:y/n 'no))
|
||||
(util:display-hash-ref messages:messages 'terminating)]
|
||||
|
||||
[else
|
||||
(init-prompt messages:messages 'choose-y/n)])))
|
||||
(let ([user-input (read-line)])
|
||||
(cond [(member user-input (hash-ref messages:y/n 'yes))
|
||||
(begin
|
||||
(util:display-hash-ref messages:messages 'creating)
|
||||
(util:create-program-directory-700 config:program-directory)
|
||||
(util:create-list-file-600 config:path-to-list-file)
|
||||
(if (and (util:program-directory-exists?)
|
||||
(util:list-file-exists?))
|
||||
(util:display-hash-ref messages:messages 'successfully-created)
|
||||
(util:display-hash-ref messages:messages 'creation-error)))]
|
||||
[(member user-input (hash-ref messages:y/n 'no))
|
||||
(util:display-hash-ref messages:messages 'terminating)]
|
||||
[else
|
||||
(init-prompt messages:messages 'choose-y/n)])))
|
||||
|
||||
(define (initialize)
|
||||
(if (util:check-for-program-file)
|
||||
(if (util:list-file-exists?)
|
||||
(util:display-hash-ref messages:messages 'file-already-exists)
|
||||
(begin
|
||||
(init-prompt messages:messages 'init-y/n))))
|
||||
|
|
14
messages.rkt
14
messages.rkt
|
@ -31,7 +31,7 @@
|
|||
(format "~a\n" config:initialize-command)
|
||||
"====\n"
|
||||
(format "Creates ~a in ~a. This is where your todo list will be stored.\n\n"
|
||||
config:program-file
|
||||
config:list-file
|
||||
config:program-directory)
|
||||
|
||||
"Example:\n"
|
||||
|
@ -80,31 +80,31 @@
|
|||
config:program-name)
|
||||
|
||||
'creating (format "> Creating ~a in ~a.\n"
|
||||
config:program-file
|
||||
config:list-file
|
||||
config:program-directory)
|
||||
|
||||
'creation-error (string-append
|
||||
(format "> Error: Could not create a(n) ~a in ~a.\n"
|
||||
config:program-file
|
||||
config:list-file
|
||||
config:program-directory)
|
||||
"> This might be due to directory permissions.\n")
|
||||
|
||||
'file-already-exists (format "> Error: ~a already exists in ~a~a.\n"
|
||||
config:program-name
|
||||
config:program-directory
|
||||
config:program-file)
|
||||
config:list-file)
|
||||
|
||||
'successfully-created (format "> ~a has been successfully created in ~a.\n"
|
||||
config:program-directory
|
||||
config:program-file)
|
||||
config:list-file)
|
||||
|
||||
'file-not-found (format "> Error: Could not find ~a~a\n"
|
||||
config:program-directory
|
||||
config:program-file)
|
||||
config:list-file)
|
||||
|
||||
'init-y/n (string-append
|
||||
(format "> ~a will be created in ~a.\n"
|
||||
config:program-file
|
||||
config:list-file
|
||||
config:program-directory)
|
||||
"> Are you sure you want to continue? [y/n]\n")
|
||||
|
||||
|
|
127
util.rkt
127
util.rkt
|
@ -8,33 +8,33 @@
|
|||
|
||||
(provide (all-defined-out))
|
||||
|
||||
(define (check-for-program-file)
|
||||
(file-exists? config:path-to-file))
|
||||
(define (list-file-exists?)
|
||||
(file-exists? config:path-to-list-file))
|
||||
|
||||
(define (create-program-file)
|
||||
(let ([opened-file (open-output-file config:path-to-file
|
||||
(define (program-directory-exists?)
|
||||
(directory-exists? config:program-directory))
|
||||
|
||||
(define (create-list-file-600 a-file)
|
||||
(let ([opened-file (open-output-file a-file
|
||||
#:mode 'text
|
||||
#:exists 'truncate)])
|
||||
(close-output-port opened-file))
|
||||
(file-or-directory-permissions config:path-to-file #o600))
|
||||
(file-or-directory-permissions a-file #o600))
|
||||
|
||||
(define (check-for-program-directory)
|
||||
(directory-exists? config:program-directory))
|
||||
|
||||
(define (create-program-directory)
|
||||
(make-directory config:program-directory)
|
||||
(file-or-directory-permissions config:program-directory #o700))
|
||||
(define (create-program-directory-700 a-directory)
|
||||
(make-directory a-directory)
|
||||
(file-or-directory-permissions a-directory #o700))
|
||||
|
||||
(define (display-hash-ref hash-list key)
|
||||
(display (hash-ref hash-list key)))
|
||||
|
||||
(define (file->string-list config:path-to-file-to-file)
|
||||
(let ([todo-list (file:file->lines config:path-to-file-to-file
|
||||
#:mode 'text
|
||||
#:line-mode 'any)])
|
||||
(define (file->string-list a-file)
|
||||
(let ([todo-list (file:file->lines a-file
|
||||
#:mode 'text
|
||||
#:line-mode 'any)])
|
||||
todo-list))
|
||||
|
||||
(define (list-empty? lst)
|
||||
(define (list-file-empty? lst)
|
||||
(list:empty? (file->string-list lst)))
|
||||
|
||||
(define (get-removed-item lst args)
|
||||
|
@ -43,26 +43,21 @@
|
|||
(define (surround-with-quotation-marks args)
|
||||
(display (string-append "\"" args "\"")))
|
||||
|
||||
(define (list->dotted-list lst)
|
||||
(string-append lst "." " "))
|
||||
(define (list->list-of-dotted-numbers lst)
|
||||
(map (lambda (x) (string-append x ". "))
|
||||
(map number->string (list:range (length lst)))))
|
||||
|
||||
(define (list->numbered-list lst)
|
||||
;; Take the list made in the first (map), which is
|
||||
;; '(0 1 2 ...), and append that to each item in a list
|
||||
(map string-append
|
||||
;; Note: compose starts from the last element in it's
|
||||
;; list. In this case, it starts at (number->string).
|
||||
(map (compose1 list->dotted-list number->string)
|
||||
(list:range (length lst)))
|
||||
(map (lambda (x y) (string-append x y))
|
||||
(list->list-of-dotted-numbers lst)
|
||||
lst))
|
||||
|
||||
(define (display-prettified-program-file)
|
||||
(display (string:string-join
|
||||
(list->numbered-list (file->string-list config:path-to-file))
|
||||
"\n"
|
||||
#:after-last "\n")))
|
||||
(define (file->numbered-list-as-lines a-file)
|
||||
(string:string-join (list->numbered-list (file->string-list a-file))
|
||||
"\n"
|
||||
#:after-last "\n"))
|
||||
|
||||
(define (append-item-to-end args lst)
|
||||
(define (append-item-to-list args lst)
|
||||
(reverse (cons args (reverse (file->string-list lst)))))
|
||||
|
||||
(define (display-item-added args)
|
||||
|
@ -75,57 +70,51 @@
|
|||
(surround-with-quotation-marks args)
|
||||
(display-hash-ref messages:messages 'item-removed-suffix))
|
||||
|
||||
(define (show-list-from-program-file)
|
||||
(cond [(and
|
||||
(check-for-program-directory)
|
||||
(check-for-program-file))
|
||||
(if
|
||||
(list-empty? config:path-to-file)
|
||||
(display-hash-ref messages:messages 'empty-todo-list)
|
||||
(display-prettified-program-file))]
|
||||
(define (show-list-from-file a-file)
|
||||
(cond [(and (program-directory-exists?)
|
||||
(list-file-exists?))
|
||||
(if (list-file-empty? a-file)
|
||||
(display-hash-ref messages:messages 'empty-todo-list)
|
||||
(display (file->numbered-list-as-lines a-file)))]
|
||||
[else
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init)]))
|
||||
|
||||
(define (write-item-to-program-file args)
|
||||
;; Add an item to the end of a list and write to a file
|
||||
(let ([new-list (append-item-to-end args config:path-to-file)])
|
||||
(file:display-to-file
|
||||
(string:string-join new-list "\n") config:path-to-file
|
||||
#:mode 'text
|
||||
#:exists 'truncate)
|
||||
;; After writing to the file, tell the user what was written
|
||||
(display-item-added args)))
|
||||
(define (write-item-to-file a-file args)
|
||||
(let ([new-list (append-item-to-list args a-file)])
|
||||
(file:display-to-file (string:string-join new-list "\n")
|
||||
a-file
|
||||
#:mode 'text
|
||||
#:exists 'truncate))
|
||||
(display-item-added args))
|
||||
|
||||
(define (add-item-to-list args)
|
||||
(if (and (check-for-program-directory)
|
||||
(check-for-program-file))
|
||||
;; The cdr here prevents user commands, such as "add"
|
||||
;; from being added to the file
|
||||
(write-item-to-program-file (string:string-join (cdr (vector->list args))))
|
||||
;; The cdr here prevents user commands, such as "add" from being added to the file
|
||||
(define (add-item-to-list a-file args)
|
||||
(if (and (program-directory-exists?)
|
||||
(list-file-exists?))
|
||||
(write-item-to-file a-file (string:string-join (cdr (vector->list args))))
|
||||
;; Else
|
||||
(begin
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init))))
|
||||
|
||||
(define (remove-item-from-program-file args)
|
||||
(let* ([removed-item (get-removed-item config:path-to-file args)]
|
||||
[new-list (remove removed-item (file->string-list config:path-to-file))])
|
||||
(file:display-to-file
|
||||
(string:string-join new-list "\n")
|
||||
config:path-to-file
|
||||
#:mode 'text
|
||||
#:exists 'truncate)
|
||||
(define (remove-item-from-list-file a-file args)
|
||||
(let* ([removed-item (get-removed-item a-file args)]
|
||||
[new-list (remove removed-item (file->string-list a-file))])
|
||||
(file:display-to-file (string:string-join new-list "\n")
|
||||
a-file
|
||||
#:mode 'text
|
||||
#:exists 'truncate)
|
||||
(display-item-removed removed-item)))
|
||||
|
||||
(define (remove-item-from-list args)
|
||||
(cond [(list-empty? config:path-to-file)
|
||||
(define (remove-item-from-list a-file args)
|
||||
(cond [(list-file-empty? a-file)
|
||||
(display-hash-ref messages:messages 'empty-todo-list)]
|
||||
[(and (check-for-program-directory)
|
||||
(check-for-program-file))
|
||||
(remove-item-from-program-file (vector-ref args 1))]
|
||||
[(and (not (check-for-program-directory))
|
||||
(not (check-for-program-file)))
|
||||
[(and (program-directory-exists?)
|
||||
(list-file-exists?))
|
||||
(remove-item-from-list-file a-file (vector-ref args 1))]
|
||||
[(and (not (program-directory-exists?))
|
||||
(not (list-file-exists?)))
|
||||
(begin
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init))]))
|
||||
|
|
Loading…
Reference in New Issue