2019-05-25 13:49:06 +00:00
|
|
|
#lang racket/base
|
|
|
|
|
|
|
|
(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"))
|
|
|
|
|
|
|
|
(provide (all-defined-out))
|
|
|
|
|
|
|
|
(define (check-for-program-file)
|
|
|
|
(file-exists? config:path-to-file))
|
|
|
|
|
|
|
|
(define (create-program-file)
|
|
|
|
(let ([opened-file (open-output-file config:path-to-file
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'truncate)])
|
|
|
|
(close-output-port opened-file))
|
|
|
|
(file-or-directory-permissions config:path-to-file #o600))
|
|
|
|
|
|
|
|
(define (check-for-program-directory)
|
|
|
|
(directory-exists? config:program-directory))
|
|
|
|
|
|
|
|
(define (create-program-directory)
|
|
|
|
(make-directory config:program-directory)
|
|
|
|
(file-or-directory-permissions config:program-directory #o700))
|
|
|
|
|
|
|
|
(define (display-hash-ref hash-list key)
|
|
|
|
(display (hash-ref hash-list key)))
|
|
|
|
|
|
|
|
(define (file->string-list config:path-to-file-to-file)
|
|
|
|
(let ([todo-list (file:file->lines config:path-to-file-to-file
|
|
|
|
#:mode 'text
|
|
|
|
#:line-mode 'any)])
|
|
|
|
todo-list))
|
|
|
|
|
|
|
|
(define (list-empty? lst)
|
|
|
|
(list:empty? (file->string-list lst)))
|
|
|
|
|
|
|
|
(define (get-removed-item lst args)
|
2019-06-30 21:07:33 +00:00
|
|
|
;; Subtract one from what the user chose, because what they are actually
|
|
|
|
;; viewing is a list starting from "1" rather than "0". Under the hood,
|
|
|
|
;; the real list starts at 0.
|
2019-05-25 13:49:06 +00:00
|
|
|
(list-ref (file->string-list lst) (sub1 (string->number args))))
|
|
|
|
|
2019-07-05 02:20:23 +00:00
|
|
|
(define (surround-with-quotation-marks args)
|
2019-05-25 13:49:06 +00:00
|
|
|
(display (string-append "\"" args "\"")))
|
|
|
|
|
2019-07-05 02:20:23 +00:00
|
|
|
(define (list->dotted-list lst)
|
2019-05-25 13:49:06 +00:00
|
|
|
(string-append lst ". "))
|
|
|
|
|
2019-07-05 02:20:23 +00:00
|
|
|
(define (list->numbered-list lst)
|
2019-06-30 21:07:33 +00:00
|
|
|
;; Take the list made in the first (map), which is
|
|
|
|
;; '(1 2 3 ...), and append that to each item in a list
|
2019-05-25 13:49:06 +00:00
|
|
|
(map string-append
|
|
|
|
;; Note: compose starts from the last element in it's
|
2019-06-30 21:07:33 +00:00
|
|
|
;; list. In this case, it starts at (number->string).
|
2019-07-05 02:20:23 +00:00
|
|
|
(map (compose1 list->dotted-list number->string)
|
2019-06-30 21:07:33 +00:00
|
|
|
(list:range 1 (add1 (length lst))))
|
2019-05-25 13:49:06 +00:00
|
|
|
lst))
|
|
|
|
|
|
|
|
(define (display-prettified-program-file)
|
2019-06-30 21:07:33 +00:00
|
|
|
(display (string:string-join
|
2019-07-05 02:20:23 +00:00
|
|
|
(list->numbered-list (file->string-list config:path-to-file))
|
2019-06-30 21:07:33 +00:00
|
|
|
"\n"
|
|
|
|
#:after-last "\n")))
|
2019-05-25 13:49:06 +00:00
|
|
|
|
2019-06-30 21:07:33 +00:00
|
|
|
;; Don't ask
|
2019-05-25 13:49:06 +00:00
|
|
|
(define (append-item-to-end args lst)
|
|
|
|
(reverse (cons args (reverse (file->string-list lst)))))
|
|
|
|
|
|
|
|
(define (display-item-added args)
|
|
|
|
(display-hash-ref messages:messages 'item-added-prefix)
|
2019-07-05 02:20:23 +00:00
|
|
|
(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)
|
2019-07-05 02:20:23 +00:00
|
|
|
(surround-with-quotation-marks args)
|
2019-05-25 13:49:06 +00:00
|
|
|
(display-hash-ref messages:messages 'item-removed-suffix))
|
|
|
|
|
|
|
|
(define (show-list-from-program-file)
|
|
|
|
(cond [(and
|
2019-06-30 21:07:33 +00:00
|
|
|
(check-for-program-directory)
|
|
|
|
(check-for-program-file))
|
2019-05-25 13:49:06 +00:00
|
|
|
(if
|
2019-06-30 21:07:33 +00:00
|
|
|
(list-empty? config:path-to-file)
|
|
|
|
(display-hash-ref messages:messages 'empty-todo-list)
|
|
|
|
(display-prettified-program-file))]
|
2019-05-25 13:49:06 +00:00
|
|
|
[else
|
2019-06-30 21:07:33 +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
|
|
|
|
|
|
|
(define (write-item-to-program-file args)
|
2019-06-04 02:01:34 +00:00
|
|
|
;; Add an item to the end of a list and write to a file
|
2019-05-25 13:49:06 +00:00
|
|
|
(let ([new-list (append-item-to-end args config:path-to-file)])
|
|
|
|
(file:display-to-file
|
2019-06-30 21:07:33 +00:00
|
|
|
(string:string-join new-list "\n") config:path-to-file
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'truncate)
|
2019-06-04 02:01:34 +00:00
|
|
|
;; After writing to the file, tell the user what was written
|
2019-05-25 13:49:06 +00:00
|
|
|
(display-item-added args)))
|
|
|
|
|
|
|
|
(define (add-item-to-list args)
|
2019-06-04 02:01:34 +00:00
|
|
|
(if (and (check-for-program-directory)
|
|
|
|
(check-for-program-file))
|
2019-06-30 21:07:33 +00:00
|
|
|
;; The cdr here prevents the first command line argument ("add")
|
|
|
|
;; from being added to the file
|
|
|
|
(write-item-to-program-file (string:string-join (cdr (vector->list args))))
|
|
|
|
;; 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
|
|
|
|
|
|
|
(define (remove-item-from-program-file args)
|
|
|
|
(let* ([removed-item (get-removed-item config:path-to-file args)]
|
|
|
|
[new-list (remove removed-item (file->string-list config:path-to-file))])
|
|
|
|
(file:display-to-file
|
2019-06-30 21:07:33 +00:00
|
|
|
(string:string-join new-list "\n")
|
|
|
|
config:path-to-file
|
|
|
|
#:mode 'text
|
|
|
|
#:exists 'truncate)
|
2019-05-25 13:49:06 +00:00
|
|
|
(display-item-removed removed-item)))
|
|
|
|
|
|
|
|
(define (remove-item-from-list args)
|
|
|
|
(cond [(list-empty? config:path-to-file)
|
|
|
|
(display-hash-ref messages:messages 'empty-todo-list)]
|
2019-06-04 02:01:34 +00:00
|
|
|
[(and (check-for-program-directory)
|
|
|
|
(check-for-program-file))
|
2019-05-25 13:49:06 +00:00
|
|
|
(remove-item-from-program-file (vector-ref args 1))]
|
2019-06-04 02:01:34 +00:00
|
|
|
[(and (not (check-for-program-directory))
|
|
|
|
(not (check-for-program-file)))
|
2019-05-25 13:49:06 +00:00
|
|
|
(begin
|
|
|
|
(display-hash-ref messages:messages 'file-not-found)
|
|
|
|
(display-hash-ref messages:messages 'try-init))]))
|