made a new branch for rewriting rodo
parent
1d601ee4c7
commit
ee112a9c71
27
args.rkt
27
args.rkt
|
@ -2,45 +2,44 @@
|
||||||
|
|
||||||
(require (prefix-in config: "config.rkt")
|
(require (prefix-in config: "config.rkt")
|
||||||
(prefix-in init: "init.rkt")
|
(prefix-in init: "init.rkt")
|
||||||
|
(prefix-in file: racket/file)
|
||||||
(prefix-in messages: "messages.rkt")
|
(prefix-in messages: "messages.rkt")
|
||||||
(prefix-in utils: "utils.rkt"))
|
(prefix-in utils: "utils.rkt"))
|
||||||
|
|
||||||
(provide (all-defined-out))
|
(provide (all-defined-out))
|
||||||
|
|
||||||
(define (check-args args)
|
(define (check-args args)
|
||||||
(let ([args-length (vector-length args)])
|
(let ([args-length (length args)])
|
||||||
(cond
|
(cond
|
||||||
[(equal? args-length 0)
|
[(equal? args-length 0)
|
||||||
(utils:display-hash-ref messages:messages 'show-usage)]
|
(utils:display-hash-ref messages:messages 'show-usage)]
|
||||||
|
|
||||||
;; help
|
;; help
|
||||||
[(and (equal? args-length 1)
|
[(and (equal? args-length 1)
|
||||||
(member (vector-ref args 0) config:help-command))
|
(member (list-ref args 0) config:help-command))
|
||||||
(utils:display-hash-ref messages:messages 'show-help)]
|
(utils:display-hash-ref messages:messages 'show-help)]
|
||||||
|
|
||||||
;; init
|
;; init
|
||||||
[(and (equal? args-length 1)
|
[(and (equal? args-length 1)
|
||||||
(member (vector-ref args 0) config:initialize-command))
|
(member (list-ref args 0) config:initialize-command))
|
||||||
(init:initialize)]
|
(init:initialize)]
|
||||||
|
|
||||||
;; add
|
;; add
|
||||||
[(and (or (equal? args-length 2) (>= args-length 2))
|
[(and (or (equal? args-length 2) (>= args-length 2))
|
||||||
(member (vector-ref args 0) config:add-command))
|
(member (list-ref args 0) config:add-command))
|
||||||
(utils:add-item-to-list config:path-to-list-file args)]
|
(utils:add-item-to-list config:list-file args)]
|
||||||
|
|
||||||
;; ls
|
;; ls
|
||||||
[(and (equal? args-length 1)
|
[(and (equal? args-length 1)
|
||||||
(member (vector-ref args 0) config:list-command))
|
(member (list-ref args 0) config:list-command))
|
||||||
(utils:show-list-from-file config:path-to-list-file)]
|
(utils:show-list-from-file config:list-file)]
|
||||||
|
|
||||||
;; rm
|
;; rm
|
||||||
[(and (equal? args-length 2)
|
[(and (equal? args-length 2)
|
||||||
(member (vector-ref args 0) config:remove-command)
|
(member (list-ref args 0) config:remove-command)
|
||||||
(real? (string->number (vector-ref args 1)))
|
(real? (string->number (list-ref args 1)))
|
||||||
(or (positive? (string->number (vector-ref args 1)))
|
(or (positive? (string->number (list-ref args 1)))
|
||||||
(zero? (string->number (vector-ref args 1))))
|
(zero? (string->number (list-ref args 1)))))
|
||||||
;; Length subtract one because the numbering starts at zero
|
(utils:remove-item-from-list args)]
|
||||||
(not (> (string->number (vector-ref args 1)) (sub1 (length (utils:file->string-list config:path-to-list-file))))))
|
|
||||||
(utils:remove-item-from-list config:path-to-list-file args)]
|
|
||||||
|
|
||||||
[else (utils:display-hash-ref messages:messages 'show-usage)])))
|
[else (utils:display-hash-ref messages:messages 'show-usage)])))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
(provide (all-defined-out))
|
(provide (all-defined-out))
|
||||||
|
|
||||||
(define list-file "todo.txt")
|
(define file-name "todo.txt")
|
||||||
(define program-name "rodo")
|
(define program-name "rodo")
|
||||||
|
|
||||||
(define program-directory
|
(define program-directory
|
||||||
|
@ -9,8 +9,8 @@
|
||||||
(expand-user-path
|
(expand-user-path
|
||||||
(string-append "~/." program-name "/"))))
|
(string-append "~/." program-name "/"))))
|
||||||
|
|
||||||
(define path-to-list-file
|
(define list-file
|
||||||
(string-append program-directory list-file))
|
(string-append program-directory file-name))
|
||||||
|
|
||||||
(define help-command '("-h"
|
(define help-command '("-h"
|
||||||
"--help"
|
"--help"
|
||||||
|
|
10
init.rkt
10
init.rkt
|
@ -13,10 +13,10 @@
|
||||||
(cond [(member user-input (hash-ref messages:y/n 'yes))
|
(cond [(member user-input (hash-ref messages:y/n 'yes))
|
||||||
(begin
|
(begin
|
||||||
(utils:display-hash-ref messages:messages 'creating)
|
(utils:display-hash-ref messages:messages 'creating)
|
||||||
(utils:create-program-directory-700 config:program-directory)
|
(utils:create-directory-700 config:program-directory)
|
||||||
(utils:create-list-file-600 config:path-to-list-file)
|
(utils:create-file-600 config:list-file)
|
||||||
(if (and (utils:program-directory-exists?)
|
(if (and (directory-exists? config:program-directory)
|
||||||
(utils:list-file-exists?))
|
(file-exists? config:list-file))
|
||||||
(utils:display-hash-ref messages:messages 'successfully-created)
|
(utils:display-hash-ref messages:messages 'successfully-created)
|
||||||
(utils:display-hash-ref messages:messages 'creation-error)))]
|
(utils:display-hash-ref messages:messages 'creation-error)))]
|
||||||
[(member user-input (hash-ref messages:y/n 'no))
|
[(member user-input (hash-ref messages:y/n 'no))
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
(init-prompt messages:messages 'choose-y/n)])))
|
(init-prompt messages:messages 'choose-y/n)])))
|
||||||
|
|
||||||
(define (initialize)
|
(define (initialize)
|
||||||
(if (utils:list-file-exists?)
|
(if (file-exists? config:list-file)
|
||||||
(utils:display-hash-ref messages:messages 'file-already-exists)
|
(utils:display-hash-ref messages:messages 'file-already-exists)
|
||||||
(begin
|
(begin
|
||||||
(init-prompt messages:messages 'init-y/n))))
|
(init-prompt messages:messages 'init-y/n))))
|
||||||
|
|
|
@ -102,6 +102,8 @@
|
||||||
config:program-directory
|
config:program-directory
|
||||||
config:list-file)
|
config:list-file)
|
||||||
|
|
||||||
|
'item-not-found "> Error: Could not find that item\n"
|
||||||
|
|
||||||
'init-y/n (string-append
|
'init-y/n (string-append
|
||||||
(format "> ~a will be created in ~a.\n"
|
(format "> ~a will be created in ~a.\n"
|
||||||
config:list-file
|
config:list-file
|
||||||
|
@ -110,7 +112,7 @@
|
||||||
|
|
||||||
'try-init (format "> Try typing `~a ~a` to set it up (without the grave accents).\n"
|
'try-init (format "> Try typing `~a ~a` to set it up (without the grave accents).\n"
|
||||||
config:program-name
|
config:program-name
|
||||||
config:initialize-command)
|
(car config:initialize-command))
|
||||||
|
|
||||||
'terminating (format "> Exiting ~a\n" config:program-name)
|
'terminating (format "> Exiting ~a\n" config:program-name)
|
||||||
|
|
||||||
|
|
2
rodo.rkt
2
rodo.rkt
|
@ -5,4 +5,4 @@
|
||||||
(define (main args)
|
(define (main args)
|
||||||
(args:check-args args))
|
(args:check-args args))
|
||||||
|
|
||||||
(main (current-command-line-arguments))
|
(main (vector->list (current-command-line-arguments)))
|
||||||
|
|
143
utils.rkt
143
utils.rkt
|
@ -8,113 +8,122 @@
|
||||||
|
|
||||||
(provide (all-defined-out))
|
(provide (all-defined-out))
|
||||||
|
|
||||||
(define (list-file-exists?)
|
(define (create-file-600 a-file)
|
||||||
(file-exists? config:path-to-list-file))
|
(let ([opened-file (open-output-file a-file #:mode 'text #:exists 'truncate)])
|
||||||
|
|
||||||
(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))
|
(close-output-port opened-file))
|
||||||
(file-or-directory-permissions a-file #o600))
|
(file-or-directory-permissions a-file #o600))
|
||||||
|
|
||||||
(define (create-program-directory-700 a-directory)
|
(define (create-directory-700 a-directory)
|
||||||
(make-directory a-directory)
|
(make-directory a-directory)
|
||||||
(file-or-directory-permissions a-directory #o700))
|
(file-or-directory-permissions a-directory #o700))
|
||||||
|
|
||||||
(define (display-hash-ref hash-list key)
|
(define (display-hash-ref hash-list key)
|
||||||
(display (hash-ref hash-list key)))
|
(display (hash-ref hash-list key)))
|
||||||
|
|
||||||
(define (file->string-list a-file)
|
(define (list->ascending-numbers lst)
|
||||||
(let ([todo-list (file:file->lines a-file
|
(list:range (length lst)))
|
||||||
#:mode 'text
|
|
||||||
#:line-mode 'any)])
|
|
||||||
todo-list))
|
|
||||||
|
|
||||||
(define (list-file-empty? a-file)
|
(define (list->dotted-ascending-numbers lst)
|
||||||
(list:empty? (file->string-list a-file)))
|
|
||||||
|
|
||||||
(define (get-removed-item lst args)
|
|
||||||
(list-ref (file->string-list lst) (string->number args)))
|
|
||||||
|
|
||||||
(define (surround-with-quotation-marks args)
|
|
||||||
(display (string-append "\"" args "\"")))
|
|
||||||
|
|
||||||
(define (list->list-of-dotted-numbers lst)
|
|
||||||
(map (lambda (x) (string-append x ". "))
|
(map (lambda (x) (string-append x ". "))
|
||||||
(map number->string (list:range (length lst)))))
|
(map number->string (list->ascending-numbers lst))))
|
||||||
|
|
||||||
(define (list->numbered-list lst)
|
(define (list->numbered-list lst)
|
||||||
(map (lambda (x y) (string-append x y))
|
(map (lambda (x y) (string-append x y))
|
||||||
(list->list-of-dotted-numbers lst)
|
(list->dotted-ascending-numbers lst)
|
||||||
lst))
|
lst))
|
||||||
|
|
||||||
(define (file->numbered-list-as-lines a-file)
|
(define (file->vertically-numbered-list a-file)
|
||||||
(string:string-join (list->numbered-list (file->string-list a-file))
|
(string:string-join
|
||||||
|
(list->numbered-list (file:file->lines a-file))
|
||||||
"\n"
|
"\n"
|
||||||
#:after-last "\n"))
|
#:after-last "\n"))
|
||||||
|
|
||||||
(define (append-item-to-list args lst)
|
(define (surround-with-quotation-marks item)
|
||||||
(reverse (cons args (reverse (file->string-list lst)))))
|
(string-append "\"" item "\""))
|
||||||
|
|
||||||
(define (display-item-added args)
|
(define (display-item-added item)
|
||||||
(display-hash-ref messages:messages 'item-added-prefix)
|
(display-hash-ref messages:messages 'item-added-prefix)
|
||||||
(surround-with-quotation-marks args)
|
(display (surround-with-quotation-marks item))
|
||||||
(display-hash-ref messages:messages 'item-added-suffix))
|
(display-hash-ref messages:messages 'item-added-suffix))
|
||||||
|
|
||||||
(define (display-item-removed args)
|
(define (display-item-removed args)
|
||||||
(display-hash-ref messages:messages 'item-removed-prefix)
|
(display-hash-ref messages:messages 'item-removed-prefix)
|
||||||
(surround-with-quotation-marks args)
|
(display (surround-with-quotation-marks args))
|
||||||
(display-hash-ref messages:messages 'item-removed-suffix))
|
(display-hash-ref messages:messages 'item-removed-suffix))
|
||||||
|
|
||||||
|
;; TODO: Turn into a check-show-list-conditions and then break
|
||||||
|
;; the rest down into separate functions
|
||||||
(define (show-list-from-file a-file)
|
(define (show-list-from-file a-file)
|
||||||
(cond [(and (program-directory-exists?)
|
(cond
|
||||||
(list-file-exists?))
|
;; If exists and not empty
|
||||||
(if (list-file-empty? a-file)
|
[(and (file-exists? config:list-file)
|
||||||
(display-hash-ref messages:messages 'empty-todo-list)
|
(not (null? (file:file->lines a-file))))
|
||||||
(display (file->numbered-list-as-lines a-file)))]
|
(display (file->vertically-numbered-list a-file))]
|
||||||
[else
|
|
||||||
|
;; If exists and empty
|
||||||
|
[(and (file-exists? config:list-file)
|
||||||
|
(null? (file:file->lines a-file)))
|
||||||
|
(display-hash-ref messages:messages 'empty-todo-list)]
|
||||||
|
|
||||||
|
;; If not exist
|
||||||
|
[(and (not (file-exists? config:list-file)))
|
||||||
|
(begin
|
||||||
(display-hash-ref messages:messages 'file-not-found)
|
(display-hash-ref messages:messages 'file-not-found)
|
||||||
(display-hash-ref messages:messages 'try-init)]))
|
(display-hash-ref messages:messages 'try-init))]
|
||||||
|
|
||||||
(define (write-item-to-file a-file args)
|
;; Otherwise
|
||||||
(let ([new-list (append-item-to-list args a-file)])
|
[else (display-hash-ref messages:messages 'show-usage)]))
|
||||||
(file:display-to-file (string:string-join new-list "\n")
|
|
||||||
a-file
|
|
||||||
#:mode 'text
|
|
||||||
#:exists 'truncate))
|
|
||||||
(display-item-added args))
|
|
||||||
|
|
||||||
;; The cdr here prevents user commands, such as "add" being added to the file
|
;; TODO: Change this to just use append with lst and (list args)
|
||||||
|
(define (append-item-to-list args lst)
|
||||||
|
(reverse (cons args (reverse (file:file->lines lst)))))
|
||||||
|
|
||||||
|
;; TODO: Turn into a check-add-conditions and then break
|
||||||
|
;; the rest down into separate functions
|
||||||
(define (add-item-to-list a-file args)
|
(define (add-item-to-list a-file args)
|
||||||
(if (and (program-directory-exists?)
|
(if (and (file-exists? config:list-file))
|
||||||
(list-file-exists?))
|
(let* ([item (string:string-join (cdr args) " ")]
|
||||||
(write-item-to-file a-file (string:string-join (cdr (vector->list args))))
|
[new-list (append-item-to-list item config:list-file)])
|
||||||
;; Else
|
(file:display-to-file (string:string-join new-list "\n")
|
||||||
|
config:list-file
|
||||||
|
#:mode 'text
|
||||||
|
#:exists 'truncate)
|
||||||
|
(display-item-added item))
|
||||||
(begin
|
(begin
|
||||||
(display-hash-ref messages:messages 'file-not-found)
|
(display-hash-ref messages:messages 'file-not-found)
|
||||||
(display-hash-ref messages:messages 'try-init))))
|
(display-hash-ref messages:messages 'try-init))))
|
||||||
|
|
||||||
(define (remove-item-from-list-file a-file args)
|
;; TODO: Turn into a check-remove-conditions and then break
|
||||||
(let* ([removed-item (get-removed-item a-file args)]
|
;; the rest down into separate functions
|
||||||
[new-list (remove removed-item (file->string-list a-file))])
|
(define (remove-item-from-list args)
|
||||||
|
(cond
|
||||||
|
;; If directory and file exist, but file is empty
|
||||||
|
[(and (directory-exists? config:program-directory)
|
||||||
|
(file-exists? config:list-file)
|
||||||
|
(null? config:list-file))
|
||||||
|
(display-hash-ref messages:messages 'empty-todo-list)]
|
||||||
|
|
||||||
|
;; If directory and file exist, and file is not empty
|
||||||
|
[(and (directory-exists? config:program-directory)
|
||||||
|
(file-exists? config:list-file)
|
||||||
|
(not (null? config:list-file)))
|
||||||
|
(let ([user-args (string->number (list-ref args 1))]
|
||||||
|
;; Length subtract one because the numbering starts at zero
|
||||||
|
[list-length (sub1 (length (file:file->lines config:list-file)))])
|
||||||
|
(if (not (> user-args list-length))
|
||||||
|
(let* ([item-to-remove (list-ref (file:file->lines config:list-file) user-args)]
|
||||||
|
[new-list (remove item-to-remove (file:file->lines config:list-file))])
|
||||||
(file:display-to-file (string:string-join new-list "\n")
|
(file:display-to-file (string:string-join new-list "\n")
|
||||||
a-file
|
config:list-file
|
||||||
#:mode 'text
|
#:mode 'text
|
||||||
#:exists 'truncate)
|
#:exists 'truncate)
|
||||||
(display-item-removed removed-item)))
|
(display-item-removed item-to-remove))
|
||||||
|
;; Else
|
||||||
|
(display-hash-ref messages:messages 'item-not-found)))]
|
||||||
|
|
||||||
(define (remove-item-from-list a-file args)
|
;; If directory and file don't exist
|
||||||
(cond [(list-file-empty? a-file)
|
[(and (not (directory-exists? config:program-directory))
|
||||||
(display-hash-ref messages:messages 'empty-todo-list)]
|
(not (file-exists? config:list-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
|
(begin
|
||||||
(display-hash-ref messages:messages 'file-not-found)
|
(display-hash-ref messages:messages 'file-not-found)
|
||||||
(display-hash-ref messages:messages 'try-init))]))
|
(display-hash-ref messages:messages 'try-init))]))
|
||||||
|
|
Loading…
Reference in New Issue