rodo/utils.rkt

121 lines
4.3 KiB
Racket
Raw Normal View History

2019-05-25 13:49:06 +00:00
#lang racket/base
(require (prefix-in file: racket/file)
(prefix-in list: racket/list)
2019-05-25 13:49:06 +00:00
(prefix-in string: racket/string)
(prefix-in config: "config.rkt")
(prefix-in messages: "messages.rkt"))
(provide (all-defined-out))
2019-11-04 02:32:11 +00:00
(define (list-file-exists?)
(file-exists? config:path-to-list-file))
2019-05-25 13:49:06 +00:00
2019-11-04 02:32:11 +00:00
(define (program-directory-exists?)
(directory-exists? config:program-directory))
(define (create-list-file-600 a-file)
(let ([opened-file (open-output-file a-file
2019-05-25 13:49:06 +00:00
#:mode 'text
#:exists 'truncate)])
(close-output-port opened-file))
2019-11-04 02:32:11 +00:00
(file-or-directory-permissions a-file #o600))
2019-05-25 13:49:06 +00:00
2019-11-04 02:32:11 +00:00
(define (create-program-directory-700 a-directory)
(make-directory a-directory)
(file-or-directory-permissions a-directory #o700))
2019-05-25 13:49:06 +00:00
(define (display-hash-ref hash-list key)
(display (hash-ref hash-list key)))
2019-11-04 02:32:11 +00:00
(define (file->string-list a-file)
(let ([todo-list (file:file->lines a-file
#:mode 'text
#:line-mode 'any)])
todo-list))
2019-05-25 13:49:06 +00:00
(define (list-file-empty? a-file)
(list:empty? (file->string-list a-file)))
2019-05-25 13:49:06 +00:00
(define (get-removed-item lst args)
(list-ref (file->string-list lst) (string->number args)))
2019-05-25 13:49:06 +00:00
(define (surround-with-quotation-marks args)
2019-05-25 13:49:06 +00:00
(display (string-append "\"" args "\"")))
2019-11-04 02:32:11 +00:00
(define (list->list-of-dotted-numbers lst)
(map (lambda (x) (string-append x ". "))
(map number->string (list:range (length lst)))))
2019-05-25 13:49:06 +00:00
(define (list->numbered-list lst)
2019-11-04 02:32:11 +00:00
(map (lambda (x y) (string-append x y))
(list->list-of-dotted-numbers lst)
2019-05-25 13:49:06 +00:00
lst))
2019-11-04 02:32:11 +00:00
(define (file->numbered-list-as-lines a-file)
(string:string-join (list->numbered-list (file->string-list a-file))
"\n"
#:after-last "\n"))
2019-05-25 13:49:06 +00:00
2019-11-04 02:32:11 +00:00
(define (append-item-to-list args lst)
2019-05-25 13:49:06 +00:00
(reverse (cons args (reverse (file->string-list lst)))))
(define (display-item-added args)
(display-hash-ref messages:messages 'item-added-prefix)
(surround-with-quotation-marks args)
2019-05-25 13:49:06 +00:00
(display-hash-ref messages:messages 'item-added-suffix))
(define (display-item-removed args)
(display-hash-ref messages:messages 'item-removed-prefix)
(surround-with-quotation-marks args)
2019-05-25 13:49:06 +00:00
(display-hash-ref messages:messages 'item-removed-suffix))
2019-11-04 02:32:11 +00:00
(define (show-list-from-file a-file)
(cond [(and (program-directory-exists?)
(list-file-exists?))
(if (list-file-empty? a-file)
(display-hash-ref messages:messages 'empty-todo-list)
(display (file->numbered-list-as-lines a-file)))]
2019-05-25 13:49:06 +00:00
[else
2019-08-25 22:37:34 +00:00
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init)]))
2019-05-25 13:49:06 +00:00
2019-11-04 02:32:11 +00:00
(define (write-item-to-file a-file args)
(let ([new-list (append-item-to-list args a-file)])
(file:display-to-file (string:string-join new-list "\n")
a-file
#:mode 'text
#:exists 'truncate))
(display-item-added args))
;; The cdr here prevents user commands, such as "add" being added to the file
2019-11-04 02:32:11 +00:00
(define (add-item-to-list a-file args)
(if (and (program-directory-exists?)
(list-file-exists?))
(write-item-to-file a-file (string:string-join (cdr (vector->list args))))
2019-08-25 22:37:34 +00:00
;; Else
(begin
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init))))
2019-05-25 13:49:06 +00:00
2019-11-04 02:32:11 +00:00
(define (remove-item-from-list-file a-file args)
(let* ([removed-item (get-removed-item a-file args)]
[new-list (remove removed-item (file->string-list a-file))])
(file:display-to-file (string:string-join new-list "\n")
a-file
#:mode 'text
#:exists 'truncate)
2019-05-25 13:49:06 +00:00
(display-item-removed removed-item)))
2019-11-04 02:32:11 +00:00
(define (remove-item-from-list a-file args)
(cond [(list-file-empty? a-file)
(display-hash-ref messages:messages 'empty-todo-list)]
2019-11-04 02:32:11 +00:00
[(and (program-directory-exists?)
(list-file-exists?))
(remove-item-from-list-file a-file (vector-ref args 1))]
[(and (not (program-directory-exists?))
(not (list-file-exists?)))
2019-05-25 13:49:06 +00:00
(begin
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init))]))