okay i think i figured it out, i needed lambdas to do delay the evaluation!
parent
948d1ff3f0
commit
457af43ca2
36
utils.rkt
36
utils.rkt
|
@ -48,23 +48,21 @@
|
||||||
(display (format (hash-ref messages:messages 'item-removed) item-to-remove)))
|
(display (format (hash-ref messages:messages 'item-removed) item-to-remove)))
|
||||||
|
|
||||||
(define (check-list-conditions)
|
(define (check-list-conditions)
|
||||||
(cond
|
(let ([file-exists? (lambda () (file-exists? config:list-file))]
|
||||||
;; If list exists and not empty
|
[file-null? (lambda () (null? (file:file->lines config:list-file)))])
|
||||||
[(and (file-exists? config:list-file)
|
(cond
|
||||||
(not (null? (file:file->lines config:list-file))))
|
[(and (file-exists?)
|
||||||
(display (file->vertically-numbered-list config:list-file))]
|
(file-null?))
|
||||||
|
(display-messages '(empty-list))]
|
||||||
|
|
||||||
;; If list exists and empty
|
[(and (file-exists?)
|
||||||
[(and (file-exists? config:list-file)
|
(not (file-null?)))
|
||||||
(null? (file:file->lines config:list-file)))
|
(display (file->vertically-numbered-list config:list-file))]
|
||||||
(display-messages '(empty-list))]
|
|
||||||
|
|
||||||
;; If not exist
|
[(not (file-exists?))
|
||||||
[(and (not (file-exists? config:list-file)))
|
(display-messages '(file-not-found try-init))]
|
||||||
(display-messages '(file-not-found
|
|
||||||
try-init))]
|
|
||||||
|
|
||||||
[else (display-messages '(show-usage))]))
|
[else (display-messages '(show-usage))])))
|
||||||
|
|
||||||
(define (append-element-to-end-of-list lst item-to-add)
|
(define (append-element-to-end-of-list lst item-to-add)
|
||||||
(reverse (cons item-to-add (reverse (file:file->lines lst)))))
|
(reverse (cons item-to-add (reverse (file:file->lines lst)))))
|
||||||
|
@ -85,8 +83,8 @@
|
||||||
(display-messages '(file-not-found
|
(display-messages '(file-not-found
|
||||||
try-init))))
|
try-init))))
|
||||||
|
|
||||||
(define (remove-item-from-list user-args)
|
(define (remove-item-from-list args)
|
||||||
(let* ([item-to-remove (list-ref (file:file->lines config:list-file) user-args)]
|
(let* ([item-to-remove (list-ref (file:file->lines config:list-file) args)]
|
||||||
[new-list (remove item-to-remove (file:file->lines config:list-file))])
|
[new-list (remove item-to-remove (file:file->lines config:list-file))])
|
||||||
(file:display-lines-to-file new-list config:list-file #:mode 'text #:exists 'truncate)
|
(file:display-lines-to-file new-list config:list-file #:mode 'text #:exists 'truncate)
|
||||||
(display-item-removed item-to-remove)))
|
(display-item-removed item-to-remove)))
|
||||||
|
@ -103,11 +101,11 @@
|
||||||
[(and (directory-exists? config:program-directory)
|
[(and (directory-exists? config:program-directory)
|
||||||
(file-exists? config:list-file)
|
(file-exists? config:list-file)
|
||||||
(not (null? config:list-file)))
|
(not (null? config:list-file)))
|
||||||
(let ([user-args (string->number (list-ref args 1))]
|
(let ([args (string->number (list-ref args 1))]
|
||||||
;; Length subtract one because the numbering starts at zero
|
;; Length subtract one because the numbering starts at zero
|
||||||
[list-length (sub1 (length (file:file->lines config:list-file)))])
|
[list-length (sub1 (length (file:file->lines config:list-file)))])
|
||||||
(if (not (> user-args list-length))
|
(if (not (> args list-length))
|
||||||
(remove-item-from-list user-args)
|
(remove-item-from-list args)
|
||||||
;; Otherwise
|
;; Otherwise
|
||||||
(display-messages '(item-not-found))))]
|
(display-messages '(item-not-found))))]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue