rodo/util.rkt

117 lines
2.8 KiB
Racket
Raw Normal View History

2018-04-12 18:25:19 +00:00
#lang racket/base
(require racket/list
racket/file
racket/string
"config.rkt"
"io.rkt"
"messages.rkt")
(provide (all-defined-out))
2018-04-12 18:37:01 +00:00
(define (d-hash-ref hash-list key)
(display (hash-ref hash-list key)))
(define (d-vector-ref args key)
(display (vector-ref args key)))
2018-04-15 04:15:10 +00:00
(define (file->string-list path-to-file)
(let
([todo-list
(file->lines
path-to-file
#:mode 'text
#:line-mode 'any)])
todo-list))
(define (list-empty? lst)
(empty? (rest (file->string-list lst))))
(define (get-removed-item lst args)
2018-04-28 13:31:39 +00:00
(list-ref lst (string->number args)))
(define (quote-item args)
(display
2018-04-28 13:31:39 +00:00
(string-append "\"" args "\"")))
2018-04-15 04:15:10 +00:00
(define (number-list lst)
(map string-append
(map number->string (rest (range (length lst))))
2018-05-19 03:36:46 +00:00
(rest lst)))
2018-04-12 18:25:19 +00:00
2018-05-19 03:36:46 +00:00
(define (indent-list lst)
(string-append ". " lst))
2018-04-12 18:25:19 +00:00
2018-04-15 04:15:10 +00:00
(define (prettify-list)
(display
(string-join
2018-05-19 03:36:46 +00:00
(number-list (map indent-list (file->string-list path)))
"\n"
#:after-last "\n")))
2018-04-12 18:25:19 +00:00
(define (show-list)
(cond
[(and
(check-for-folder)
(check-for-file))
(if
(list-empty? path)
(d-hash-ref messages 'empty-todo-list)
(prettify-list))]
[else
2018-04-12 18:25:19 +00:00
(d-hash-ref messages 'file-not-found)
(d-hash-ref messages 'try-init)]))
2018-04-12 18:25:19 +00:00
2018-04-13 05:02:48 +00:00
(define (add-item-to-file args)
(let ([new-list
(reverse
(cons args
(reverse (file->string-list path))))])
2018-04-15 04:15:10 +00:00
(display-to-file
(string-join new-list "\n" #:after-last "\n")
2018-04-15 04:15:10 +00:00
path
#:mode 'text
#:exists 'replace)
2018-04-28 13:31:39 +00:00
(d-hash-ref messages 'item-added-prefix)
(quote-item args)
(d-hash-ref messages 'item-added-suffix)))
2018-04-12 18:25:19 +00:00
(define (add-item args)
(if
(and
(check-for-folder)
(check-for-file))
(add-item-to-file (vector-ref args 1))
2018-04-12 18:25:19 +00:00
(begin
(d-hash-ref messages 'file-not-found)
(d-hash-ref messages 'try-init))))
2018-04-13 05:02:48 +00:00
(define (remove-item-from-file args)
(let ([removed-item
2018-04-28 13:31:39 +00:00
(get-removed-item (file->string-list path) args)]
[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)
(d-hash-ref messages 'item-removed-prefix)
(quote-item removed-item)
(d-hash-ref messages 'item-removed-suffix)))
2018-04-13 05:02:48 +00:00
2018-04-12 18:25:19 +00:00
(define (remove-item args)
2018-04-15 04:15:10 +00:00
(cond
[(list-empty? path)
(d-hash-ref messages 'empty-todo-list)]
2018-04-15 04:15:10 +00:00
[(and
(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))]))