rodo/utils.rkt

117 lines
4.2 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
2020-01-03 19:21:08 +00:00
(define (display-messages key-list)
2019-11-22 18:35:25 +00:00
(for ([key key-list])
2020-01-27 00:56:19 +00:00
(display (hash-ref messages:messages key))))
2019-11-22 18:35:25 +00:00
2020-01-27 00:56:19 +00:00
(define (list->ascending-numbers-list lst)
2019-11-15 12:37:40 +00:00
(list:range (length lst)))
2019-05-25 13:49:06 +00:00
2020-01-27 00:56:19 +00:00
(define (list->dotted-ascending-numbers-list lst)
2019-11-04 02:32:11 +00:00
(map (lambda (x) (string-append x ". "))
2020-01-27 00:56:19 +00:00
(map number->string (list->ascending-numbers-list 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))
2020-01-27 00:56:19 +00:00
(list->dotted-ascending-numbers-list 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)
2020-01-27 00:56:19 +00:00
(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-22 18:35:25 +00:00
(define (display-item-added item-to-add)
(display (format (hash-ref messages:messages 'item-added) item-to-add)))
2019-05-25 13:49:06 +00:00
2019-11-22 18:35:25 +00:00
(define (display-item-removed item-to-remove)
(display (format (hash-ref messages:messages 'item-removed) item-to-remove)))
2019-05-25 13:49:06 +00:00
2019-11-22 18:35:25 +00:00
(define (check-list-conditions)
(let ([file-exists? (lambda () (file-exists? config:list-file))]
[file-null? (lambda () (null? (file:file->lines config:list-file)))])
(cond
[(and (file-exists?)
(file-null?))
(display-messages '(empty-list))]
2019-11-15 12:37:40 +00:00
[(and (file-exists?)
(not (file-null?)))
(display (file->vertically-numbered-list config:list-file))]
[(not (file-exists?))
(display-messages '(file-not-found try-init))]
2019-11-15 12:37:40 +00:00
[else (display-messages '(show-usage))])))
2019-11-15 12:37:40 +00:00
2019-11-22 18:35:25 +00:00
(define (append-element-to-end-of-list lst item-to-add)
(reverse (cons item-to-add (reverse (file:file->lines lst)))))
(define (add-item-to-list args)
(let* ([item-to-add (string:string-join (cdr args) " ")]
[new-list (append-element-to-end-of-list config:list-file item-to-add)])
(file:display-lines-to-file new-list
config:list-file
#:mode 'text
#:exists 'truncate)
(display-item-added item-to-add)))
2019-11-15 12:37:40 +00:00
2019-11-22 18:35:25 +00:00
(define (check-add-conditions args)
2019-11-15 12:37:40 +00:00
(if (and (file-exists? config:list-file))
2020-01-27 00:56:19 +00:00
(add-item-to-list args)
;; Otherwise
(display-messages '(file-not-found
try-init))))
2019-11-22 18:35:25 +00:00
(define (remove-item-from-list args)
(let* ([item-to-remove (list-ref (file:file->lines config:list-file) args)]
2019-11-22 18:35:25 +00:00
[new-list (remove item-to-remove (file:file->lines config:list-file))])
2020-01-27 00:56:19 +00:00
(file:display-lines-to-file new-list config:list-file #:mode 'text #:exists 'truncate)
2019-11-22 18:35:25 +00:00
(display-item-removed item-to-remove)))
(define (check-remove-conditions args)
2019-11-15 12:37:40 +00:00
(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))
2020-01-03 19:21:08 +00:00
(display-messages '(empty-list))]
2019-11-15 12:37:40 +00:00
;; 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 ([args (string->number (list-ref args 1))]
2019-11-15 12:37:40 +00:00
;; Length subtract one because the numbering starts at zero
[list-length (sub1 (length (file:file->lines config:list-file)))])
(if (not (> args list-length))
(remove-item-from-list args)
2020-01-27 00:56:19 +00:00
;; Otherwise
(display-messages '(item-not-found))))]
2019-11-15 12:37:40 +00:00
;; If directory and file don't exist
[(and (not (directory-exists? config:program-directory))
(not (file-exists? config:list-file)))
2020-01-27 00:56:19 +00:00
(display-messages '(file-not-found
try-init))]))