rodo/util.rkt

124 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))))
2018-04-13 05:02:48 +00:00
(define (quote-item-in-list lst args)
(display
(string-append
"\""
2018-04-15 04:15:10 +00:00
(list-ref lst (string->number args))
2018-04-13 05:02:48 +00:00
"\"")))
2018-04-15 04:15:10 +00:00
(define (quote-item-in-vector args)
(display
(string-append
"\"" args "\"")))
(define (number-list lst)
2018-04-12 18:25:19 +00:00
(map
string-append
(map
number->string
(rest (range (length lst))))
(rest lst)))
2018-04-12 18:25:19 +00:00
2018-04-15 04:15:10 +00:00
(define (indent-list)
2018-04-12 18:25:19 +00:00
(lambda (lst)
(string-append ". " lst)))
2018-04-15 04:15:10 +00:00
(define (prettify-list)
(display
(string-join
(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 ([args (string-append args "\n")])
2018-04-15 04:15:10 +00:00
(display-to-file
args
path
#:mode 'text
#:exists 'append)))
2018-04-12 18:25:19 +00:00
(define (add-item args)
(if
(and
(check-for-folder)
(check-for-file))
(begin
(add-item-to-file (vector-ref args 1))
(d-hash-ref messages 'item-added-prefix)
2018-04-15 04:15:10 +00:00
(quote-item-in-vector (vector-ref args 1))
2018-04-12 18:25:19 +00:00
(d-hash-ref messages 'item-added-suffix))
(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)
2018-04-15 04:15:10 +00:00
(d-hash-ref messages 'item-removed-prefix)
(quote-item-in-list (file->string-list path) args)
2018-04-15 04:15:10 +00:00
(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)))
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))]))