rodo/util.rkt

135 lines
4.2 KiB
Racket
Raw Normal View History

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