prefixed util.rkt

main
m455 2018-10-10 14:27:03 -04:00
parent 38e14b967e
commit 2e6091c1c8
2 changed files with 66 additions and 67 deletions

View File

@ -1,5 +1,4 @@
#lang racket/base #lang racket/base
(require racket/file)
(provide (all-defined-out)) (provide (all-defined-out))
(define program-name "rodo") (define program-name "rodo")

132
util.rkt
View File

@ -1,34 +1,34 @@
#lang racket/base #lang racket/base
(require racket/list (require (prefix-in list: racket/list)
racket/file (prefix-in file: racket/file)
racket/string (prefix-in string: racket/string)
"config.rkt" (prefix-in config: "config.rkt")
"messages.rkt") (prefix-in messages: "messages.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
(define (check-for-file) (define (check-for-file)
(file-exists? path)) (file-exists? config:path))
(define (create-file) (define (create-file)
(let ([opened-file (let ([opened-file
(open-output-file path (open-output-file config:path
#:mode 'text #:mode 'text
#:exists 'can-update)]) #:exists 'can-update)])
(close-output-port opened-file))) (close-output-port opened-file)))
(define (check-for-folder) (define (check-for-folder)
(directory-exists? (expand-user-path (directory-exists? (expand-user-path
(string-append (string-append
program-path config:program-path
program-directory)))) config:program-directory))))
(define (create-folder) (define (create-folder)
(make-directory (expand-user-path (make-directory (expand-user-path
(string-append (string-append
program-path config:program-path
program-directory)))) config:program-directory))))
(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)))
@ -36,99 +36,99 @@
(define (d-vector-ref args key) (define (d-vector-ref args key)
(display (vector-ref args key))) (display (vector-ref args key)))
(define (file->string-list path-to-file) (define (file->string-list config:path-to-file)
(let ([todo-list (file->lines path-to-file (let ([todo-list (file:file->lines config:path-to-file
#:mode 'text #:mode 'text
#:line-mode 'any)]) #:line-mode 'any)])
todo-list)) todo-list))
(define (list-empty? lst) (define (list-empty? lst)
(empty? (rest (file->string-list lst)))) (list:empty? (list:rest (file->string-list lst))))
(define (get-removed-item lst args) (define (get-removed-item lst args)
(list-ref lst (string->number args))) (list-ref lst (string->number args)))
(define (quote-item args) (define (quote-item args)
(display (display
(string-append "\"" args "\""))) (string-append "\"" args "\"")))
(define (prefix-with-number lst) (define (prefix-with-number lst)
(map string-append (map string-append
(map number->string (rest (range (length lst)))) (map number->string (list:rest (list:range (length lst))))
(rest lst))) (list:rest lst)))
(define (prefix-with-period lst) (define (prefix-with-period lst)
(string-append ". " lst)) (string-append ". " lst))
(define (prettify-list) (define (prettify-list)
(display (display
(string-join (prefix-with-number (map prefix-with-period (file->string-list path))) (string:string-join (prefix-with-number (map prefix-with-period (file->string-list config:path)))
"\n" "\n"
#:after-last "\n"))) #:after-last "\n")))
(define (append-to-end args lst) (define (append-to-end args lst)
(reverse (cons args (reverse (file->string-list lst))))) (reverse (cons args (reverse (file->string-list lst)))))
(define (display-item-added args) (define (display-item-added args)
(display-hash-ref messages 'item-added-prefix) (display-hash-ref messages:messages 'item-added-prefix)
(quote-item args) (quote-item args)
(display-hash-ref 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 'item-removed-prefix) (display-hash-ref messages:messages 'item-removed-prefix)
(quote-item args) (quote-item args)
(display-hash-ref messages 'item-removed-suffix)) (display-hash-ref messages:messages 'item-removed-suffix))
(define (show-list) (define (show-list)
(cond [(and (cond [(and
(check-for-folder) (check-for-folder)
(check-for-file)) (check-for-file))
(if (if
(list-empty? path) (list-empty? config:path)
(display-hash-ref messages 'empty-todo-list) (display-hash-ref messages:messages 'empty-todo-list)
(prettify-list))] (prettify-list))]
[else [else
(display-hash-ref messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages 'try-init)])) (display-hash-ref messages:messages 'try-init)]))
(define (add-item-to-file args) (define (add-item-to-file args)
(let ([new-list (append-to-end args path)]) (let ([new-list (append-to-end args config:path)])
(display-to-file (file:display-to-file
(string-join new-list "\n" #:after-last "\n") (string:string-join new-list "\n" #:after-last "\n")
path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'replace)
(display-item-added args))) (display-item-added args)))
(define (add-item args) (define (add-item args)
(if (and (if (and
(check-for-folder) (check-for-folder)
(check-for-file)) (check-for-file))
(add-item-to-file (vector-ref args 1)) (add-item-to-file (vector-ref args 1))
(begin (begin
(display-hash-ref messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages 'try-init)))) (display-hash-ref messages:messages 'try-init))))
(define (remove-item-from-file args) (define (remove-item-from-file args)
(let ([removed-item (get-removed-item (file->string-list path) args)] (let ([removed-item (get-removed-item (file->string-list config:path) args)]
[new-list (remove [new-list (remove
(list-ref (file->string-list path) (string->number args)) (list-ref (file->string-list config:path) (string->number args))
(file->string-list path))]) (file->string-list config:path))])
(display-to-file (file:display-to-file
(string-join new-list "\n" #:after-last "\n") (string:string-join new-list "\n" #:after-last "\n")
path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'replace)
(display-item-removed removed-item))) (display-item-removed removed-item)))
(define (remove-item args) (define (remove-item args)
(cond [(list-empty? path) (cond [(list-empty? config:path)
(display-hash-ref messages 'empty-todo-list)] (display-hash-ref messages:messages 'empty-todo-list)]
[(and [(and
(check-for-folder) (check-for-folder)
(check-for-file)) (check-for-file))
(remove-item-from-file (vector-ref args 1))] (remove-item-from-file (vector-ref args 1))]
[(and (not (check-for-folder)) (not (check-for-file))) [(and (not (check-for-folder)) (not (check-for-file)))
(begin (begin
(display-hash-ref messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages 'try-init))])) (display-hash-ref messages:messages 'try-init))]))