rodo/util.rkt

148 lines
4.8 KiB
Racket
Raw Normal View History

2018-10-10 17:55:52 +00:00
#lang racket/base
2018-10-10 18:27:03 +00:00
(require (prefix-in list: racket/list)
(prefix-in file: racket/file)
(prefix-in string: racket/string)
(prefix-in config: "config.rkt")
(prefix-in messages: "messages.rkt"))
2018-10-10 17:55:52 +00:00
(provide (all-defined-out))
(define (check-for-file)
2018-10-10 18:27:03 +00:00
(file-exists? config:path))
2018-10-10 17:55:52 +00:00
(define (create-file)
(let ([opened-file
2018-10-10 18:27:03 +00:00
(open-output-file config:path
#:mode 'text
#:exists 'can-update)])
2018-10-10 17:55:52 +00:00
(close-output-port opened-file)))
(define (check-for-folder)
(directory-exists? (expand-user-path
2018-10-10 18:27:03 +00:00
(string-append
config:program-path
config:program-directory))))
2018-10-10 17:55:52 +00:00
(define (create-folder)
(make-directory (expand-user-path
2018-10-10 18:27:03 +00:00
(string-append
config:program-path
config:program-directory))))
2018-10-10 17:55:52 +00:00
(define (display-hash-ref hash-list key)
(display (hash-ref hash-list key)))
;; Just so I don't have to keep typing
;; "#:mode...#:line-mode..." every time
2018-10-10 18:27:03 +00:00
(define (file->string-list config:path-to-file)
2019-02-01 03:56:48 +00:00
(let ([todo-list (file:file->lines config:path-to-file
2019-02-06 03:03:21 +00:00
#:mode 'text
#:line-mode 'any)])
2018-10-10 17:55:52 +00:00
todo-list))
(define (list-empty? lst)
(list:empty? (file->string-list lst)))
2018-10-10 17:55:52 +00:00
;; Find out which item is being removed by scooping up
;; the number the user entered in the command line
;; argument
2018-10-10 17:55:52 +00:00
(define (get-removed-item lst args)
;; Subtract one from what the user chose, because they are
;; are actually viewing the list numbers as human numbers
;; so (actual-number +1)
(list-ref (file->string-list lst) (sub1 (string->number args))))
2018-10-10 17:55:52 +00:00
(define (surround-in-quotes args)
(display (string-append "\"" args "\"")))
2018-10-10 17:55:52 +00:00
2019-02-06 03:03:21 +00:00
(define (prefix-with-period lst)
(string-append lst ". "))
2018-10-10 17:55:52 +00:00
(define (prefix-with-number lst)
2019-02-01 03:56:48 +00:00
(map string-append
;; Note: compose starts from the last element in it's
;; list, as if it were nested, so that would be add1 here
(map (compose prefix-with-period number->string add1)
(list:range (length lst)))
lst))
2018-10-10 17:55:52 +00:00
2019-02-06 03:03:21 +00:00
(define (display-prettified-list)
2018-10-10 17:55:52 +00:00
(display
2019-02-06 03:03:21 +00:00
(string:string-join
(prefix-with-number (file->string-list config:path))
2019-02-01 03:56:48 +00:00
"\n"
#:after-last "\n")))
2018-10-10 17:55:52 +00:00
2019-02-06 03:03:21 +00:00
;; This is a bit of ugly scheme sorcery
2018-10-10 17:55:52 +00:00
(define (append-to-end args lst)
(reverse (cons args (reverse (file->string-list lst)))))
(define (display-item-added args)
2018-10-10 18:27:03 +00:00
(display-hash-ref messages:messages 'item-added-prefix)
(surround-in-quotes args)
2018-10-10 18:27:03 +00:00
(display-hash-ref messages:messages 'item-added-suffix))
2018-10-10 17:55:52 +00:00
(define (display-item-removed args)
2018-10-10 18:27:03 +00:00
(display-hash-ref messages:messages 'item-removed-prefix)
(surround-in-quotes args)
2018-10-10 18:27:03 +00:00
(display-hash-ref messages:messages 'item-removed-suffix))
2018-10-10 17:55:52 +00:00
(define (show-list)
(cond [(and
2018-10-10 18:27:03 +00:00
(check-for-folder)
(check-for-file))
(if
2019-02-06 03:03:21 +00:00
;; If file exists, see if it's empty, if so
;; tell the user
2018-10-10 18:27:03 +00:00
(list-empty? config:path)
(display-hash-ref messages:messages 'empty-todo-list)
2019-02-06 03:03:21 +00:00
;; If file isn't empty, display a pretty list
(display-prettified-list))]
;; If file doesn't exist, tell the user
2018-10-10 18:27:03 +00:00
[else
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init)]))
2018-10-10 17:55:52 +00:00
(define (add-item-to-file args)
2019-02-06 03:03:21 +00:00
;; Add item to end of list and write to file
2018-10-10 18:27:03 +00:00
(let ([new-list (append-to-end args config:path)])
2019-02-01 03:56:48 +00:00
(file:display-to-file
2019-02-06 03:03:21 +00:00
(string:string-join new-list "\n")
2018-10-10 18:27:03 +00:00
config:path
#:mode 'text
#:exists 'replace)
2018-10-10 17:55:52 +00:00
(display-item-added args)))
(define (add-item args)
(if (and
2018-10-10 18:27:03 +00:00
(check-for-folder)
(check-for-file))
(add-item-to-file (vector-ref args 1))
(begin
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init))))
2018-10-10 17:55:52 +00:00
(define (remove-item-from-file args)
(let* ([removed-item (get-removed-item config:path args)]
[new-list (remove removed-item (file->string-list config:path))])
2019-02-01 03:56:48 +00:00
(file:display-to-file
2019-02-06 03:03:21 +00:00
(string:string-join new-list "\n")
2018-10-10 18:27:03 +00:00
config:path
#:mode 'text
#:exists 'replace)
2018-10-10 17:55:52 +00:00
(display-item-removed removed-item)))
(define (remove-item args)
2018-10-10 18:27:03 +00:00
(cond [(list-empty? config:path)
(display-hash-ref messages:messages 'empty-todo-list)]
2019-02-01 03:56:48 +00:00
[(and
2018-10-10 18:27:03 +00:00
(check-for-folder)
(check-for-file))
2019-02-01 03:56:48 +00:00
(remove-item-from-file (vector-ref args 1))]
[(and (not (check-for-folder)) (not (check-for-file)))
(begin
(display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init))]))