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))
|
|
|
|
|
2018-04-16 13:57:38 +00:00
|
|
|
(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)
|
2018-04-18 13:42:50 +00:00
|
|
|
(cond
|
|
|
|
[(number? args)
|
|
|
|
(display
|
|
|
|
(string-append
|
|
|
|
"\""
|
|
|
|
(list-ref lst (string->number args))
|
|
|
|
"\""))]
|
|
|
|
[(string? args)
|
|
|
|
(display
|
|
|
|
(string-append
|
|
|
|
"\""
|
|
|
|
args
|
|
|
|
"\""))]))
|
2018-04-15 04:15:10 +00:00
|
|
|
|
|
|
|
(define (number-list lst)
|
2018-04-12 18:25:19 +00:00
|
|
|
(map
|
|
|
|
string-append
|
|
|
|
(map
|
|
|
|
number->string
|
2018-04-16 13:57:38 +00:00
|
|
|
(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)
|
2018-04-16 13:57:38 +00:00
|
|
|
(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)
|
2018-04-16 13:57:38 +00:00
|
|
|
(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)
|
2018-04-16 13:57:38 +00:00
|
|
|
(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)
|
2018-04-18 13:42:50 +00:00
|
|
|
(begin
|
|
|
|
(d-hash-ref messages 'item-added-prefix)
|
|
|
|
(quote-item-in-list (file->string-list path) args)
|
|
|
|
(d-hash-ref messages 'item-added-suffix))
|
|
|
|
(let ([new-list
|
|
|
|
(reverse
|
|
|
|
(append (list args)
|
|
|
|
(reverse
|
|
|
|
(file->string-list path))))])
|
2018-04-15 04:15:10 +00:00
|
|
|
(display-to-file
|
2018-04-18 13:42:50 +00:00
|
|
|
(string-join new-list "\n" #:after-last "\n")
|
2018-04-15 04:15:10 +00:00
|
|
|
path
|
|
|
|
#:mode 'text
|
2018-04-18 13:42:50 +00:00
|
|
|
#:exists 'replace)))
|
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))
|
|
|
|
(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)
|
2018-04-16 13:57:38 +00:00
|
|
|
(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
|
2018-04-16 13:57:38 +00:00
|
|
|
[(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))]))
|