From e8c97fdba79fed899202112a919b157f78d953e8 Mon Sep 17 00:00:00 2001 From: m455 Date: Sun, 15 Apr 2018 00:15:10 -0400 Subject: [PATCH] updated via script --- config.rkt | 12 +++--- init.rkt | 11 ++++- io.rkt | 26 ++++-------- messages.rkt | 2 +- util.rkt | 112 +++++++++++++++++++++++++-------------------------- 5 files changed, 79 insertions(+), 84 deletions(-) diff --git a/config.rkt b/config.rkt index ac5da0f..7c3fb23 100644 --- a/config.rkt +++ b/config.rkt @@ -1,19 +1,19 @@ #lang racket/base - +(require racket/file) (provide (all-defined-out)) (define program-name "rodo") (define program-directory ".rodo/") (define program-path "~/") (define program-file "todo-list") +(define remove-command "rm") +(define add-command "add") +(define list-command "ls") +(define initialize-command "init") +(define help-command '("-h" "--help")) (define path (expand-user-path (string-append program-path program-directory program-file))) -(define remove-command "rm") -(define add-command "add") -(define list-command "ls") -(define initialize-command "init") -(define help-command '("-h" "--help")) diff --git a/init.rkt b/init.rkt index a4fbb72..529d40d 100644 --- a/init.rkt +++ b/init.rkt @@ -1,12 +1,20 @@ #lang racket/base -(require "config.rkt" +(require racket/file + "config.rkt" "util.rkt" "messages.rkt" "io.rkt") (provide (all-defined-out)) +(define (initialize-file) + (display-to-file + "\n" + path + #:mode 'text + #:exists 'replace)) + (define (init-prompt hash-list key) (d-hash-ref hash-list key) (display "> ") @@ -18,6 +26,7 @@ (d-hash-ref messages 'creating-file) (create-folder) (create-file) + (initialize-file) (if (and (check-for-folder) diff --git a/io.rkt b/io.rkt index 63afefb..86373aa 100644 --- a/io.rkt +++ b/io.rkt @@ -6,28 +6,16 @@ (provide (all-defined-out)) (define (check-for-file) - (file-exists? - (expand-user-path - (string-append - program-path - program-directory - program-file)))) + (file-exists? path)) (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)))) + ([opened-file + (open-output-file + path + #:mode 'text + #:exists 'can-update)]) + (close-output-port opened-file))) (define (check-for-folder) (directory-exists? diff --git a/messages.rkt b/messages.rkt index 73cc225..d536fd8 100644 --- a/messages.rkt +++ b/messages.rkt @@ -40,7 +40,7 @@ "\x09number corresponds to which item to remove it.\n") 'empty-todo-list - "Error> There is nothing in your list yet\n" + "Error > There is nothing in your list \n" 'show-usage (string-append diff --git a/util.rkt b/util.rkt index 98af345..7153c19 100644 --- a/util.rkt +++ b/util.rkt @@ -15,15 +15,28 @@ (define (d-vector-ref args key) (display (vector-ref args key))) +(define (file->string-list path-to-file) + (let + ([todo-list + (file->lines + path-to-file + #:mode 'text + #:line-mode 'any)]) + todo-list)) + (define (quote-item-in-list lst args) (display (string-append "\"" - (list-ref lst - (string->number args)) + (list-ref lst (string->number args)) "\""))) -(define (make-numbered lst) +(define (quote-item-in-vector args) + (display + (string-append + "\"" args "\""))) + +(define (number-list lst) (map string-append (map @@ -31,38 +44,34 @@ (range (length lst))) lst)) -(define (add-spaces) +(define (indent-list) (lambda (lst) (string-append ". " lst))) -(define (show-list-from-file) - (let - ([todo-list - (file->lines path - #:mode 'text - #:line-mode 'linefeed)]) +(define (prettify-list) (display (string-join - (make-numbered (map (add-spaces) todo-list)) + (number-list (map (indent-list) (file->string-list path))) "\n" - #:after-last "\n")))) + #:after-last "\n"))) (define (show-list) (if (and (check-for-folder) (check-for-file)) - (show-list-from-file) + (prettify-list) (begin (d-hash-ref messages 'file-not-found) (d-hash-ref messages 'try-init)))) (define (add-item-to-file args) (let ([args (string-append args "\n")]) - (display-to-file args - path - #:mode 'text - #:exists 'append))) + (display-to-file + args + path + #:mode 'text + #:exists 'append))) (define (add-item args) (if @@ -72,51 +81,40 @@ (begin (add-item-to-file (vector-ref args 1)) (d-hash-ref messages 'item-added-prefix) - (d-vector-ref args 1) + (quote-item-in-vector (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-from-file args) - (let ([todo-list - (file->lines path - #:mode 'text - #:line-mode 'linefeed)]) - (d-hash-ref messages 'item-removed-prefix) - (quote-item-in-list todo-list args) - (d-hash-ref messages 'item-removed-suffix) - (let ([new-list - (remove - (list-ref - todo-list - (string->number - args)) - todo-list)]) - (display-to-file - (string-join new-list "\n") - path - #:mode 'text - #:exists 'replace)))) + (d-hash-ref messages 'item-removed-prefix) + (quote-item-in-list + (file->string-list path) args) + (d-hash-ref messages 'item-removed-suffix) + (let ([new-list + (remove (list-ref + (file->string-list path) + (string->number + args)) + (file->string-list path))]) + (display-to-file + (string-join new-list "\n" #:after-last "\n") + path + #:mode 'text + #:exists 'replace))) (define (remove-item args) - (let ([todo-list - (file->lines - path - #:mode 'text - #:line-mode 'linefeed)]) - (cond - [(< (length todo-list) 1) - (d-hash-ref messages 'empty-todo-list)] - [(and - (number? (string->number (vector-ref args 1))) - (< (string->number (vector-ref args 1)) (vector-length args)) - (check-for-folder) - (check-for-file)) - (remove-item-from-file (vector-ref args 1))] - [(and (not (check-for-folder)) (not (check-for-file))) - (begin - (d-hash-ref messages 'file-not-found) - (d-hash-ref messages 'try-init))] - [(>= (string->number (vector-ref args 1)) (vector-length args)) - (d-hash-ref messages 'not-in-list)]))) + (cond + [(< (length (file->string-list path)) 1) + (d-hash-ref messages 'empty-(file->string-list path))] + [(and + (number? (string->number (vector-ref args 1))) + (< (string->number (vector-ref args 1)) (vector-length args)) + (check-for-folder) + (check-for-file)) + (remove-item-from-file (vector-ref args 1))] + [(and (not (check-for-folder)) (not (check-for-file))) + (begin + (d-hash-ref messages 'file-not-found) + (d-hash-ref messages 'try-init))]))