rodo/utils.rkt

130 lines
4.9 KiB
Racket
Raw Normal View History

2019-05-25 13:49:06 +00:00
#lang racket/base
2019-11-15 12:37:40 +00:00
(require (prefix-in file: racket/file)
(prefix-in list: racket/list)
(prefix-in string: racket/string)
(prefix-in config: "config.rkt")
2019-05-25 13:49:06 +00:00
(prefix-in messages: "messages.rkt"))
(provide (all-defined-out))
2019-11-15 12:37:40 +00:00
(define (create-file-600 a-file)
(let ([opened-file (open-output-file a-file #:mode 'text #:exists 'truncate)])
2019-05-25 13:49:06 +00:00
(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-15 12:37:40 +00:00
(define (create-directory-700 a-directory)
2019-11-04 02:32:11 +00:00
(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-15 12:37:40 +00:00
(define (list->ascending-numbers lst)
(list:range (length lst)))
2019-05-25 13:49:06 +00:00
2019-11-15 12:37:40 +00:00
(define (list->dotted-ascending-numbers lst)
2019-11-04 02:32:11 +00:00
(map (lambda (x) (string-append x ". "))
2019-11-15 12:37:40 +00:00
(map number->string (list->ascending-numbers 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))
2019-11-15 12:37:40 +00:00
(list->dotted-ascending-numbers lst)
2019-05-25 13:49:06 +00:00
lst))
2019-11-15 12:37:40 +00:00
(define (file->vertically-numbered-list a-file)
(string:string-join
(list->numbered-list (file:file->lines a-file))
"\n"
#:after-last "\n"))
2019-05-25 13:49:06 +00:00
2019-11-15 12:37:40 +00:00
(define (surround-with-quotation-marks item)
(string-append "\"" item "\""))
2019-05-25 13:49:06 +00:00
2019-11-15 12:37:40 +00:00
(define (display-item-added item)
2019-05-25 13:49:06 +00:00
(display-hash-ref messages:messages 'item-added-prefix)
2019-11-15 12:37:40 +00:00
(display (surround-with-quotation-marks item))
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)
2019-11-15 12:37:40 +00:00
(display (surround-with-quotation-marks args))
2019-05-25 13:49:06 +00:00
(display-hash-ref messages:messages 'item-removed-suffix))
2019-11-15 12:37:40 +00:00
;; TODO: Turn into a check-show-list-conditions and then break
;; the rest down into separate functions
2019-11-04 02:32:11 +00:00
(define (show-list-from-file a-file)
2019-11-15 12:37:40 +00:00
(cond
;; If exists and not empty
[(and (file-exists? config:list-file)
(not (null? (file:file->lines a-file))))
(display (file->vertically-numbered-list a-file))]
;; If exists and empty
[(and (file-exists? config:list-file)
(null? (file:file->lines a-file)))
(display-hash-ref messages:messages 'empty-todo-list)]
;; If not exist
[(and (not (file-exists? config:list-file)))
(begin
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init))]
;; Otherwise
[else (display-hash-ref messages:messages 'show-usage)]))
;; TODO: Change this to just use append with lst and (list args)
(define (append-item-to-list args lst)
(reverse (cons args (reverse (file:file->lines lst)))))
;; TODO: Turn into a check-add-conditions and then break
;; the rest down into separate functions
2019-11-04 02:32:11 +00:00
(define (add-item-to-list a-file args)
2019-11-15 12:37:40 +00:00
(if (and (file-exists? config:list-file))
(let* ([item (string:string-join (cdr args) " ")]
[new-list (append-item-to-list item config:list-file)])
(file:display-to-file (string:string-join new-list "\n")
config:list-file
#:mode 'text
#:exists 'truncate)
(display-item-added item))
2019-08-25 22:37:34 +00:00
(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-15 12:37:40 +00:00
;; TODO: Turn into a check-remove-conditions and then break
;; the rest down into separate functions
(define (remove-item-from-list args)
(cond
;; If directory and file exist, but file is empty
[(and (directory-exists? config:program-directory)
(file-exists? config:list-file)
(null? config:list-file))
(display-hash-ref messages:messages 'empty-todo-list)]
;; If directory and file exist, and file is not empty
[(and (directory-exists? config:program-directory)
(file-exists? config:list-file)
(not (null? config:list-file)))
(let ([user-args (string->number (list-ref args 1))]
;; Length subtract one because the numbering starts at zero
[list-length (sub1 (length (file:file->lines config:list-file)))])
(if (not (> user-args list-length))
(let* ([item-to-remove (list-ref (file:file->lines config:list-file) user-args)]
[new-list (remove item-to-remove (file:file->lines config:list-file))])
(file:display-to-file (string:string-join new-list "\n")
config:list-file
#:mode 'text
#:exists 'truncate)
(display-item-removed item-to-remove))
;; Else
(display-hash-ref messages:messages 'item-not-found)))]
;; If directory and file don't exist
[(and (not (directory-exists? config:program-directory))
(not (file-exists? config:list-file)))
(begin
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init))]))