changed to an easy-to-maintain numbering system (start at 0 instead of 1)

main
m455 2019-09-23 16:40:26 -04:00
parent 6986920d4d
commit cff79c0dd5
4 changed files with 105 additions and 108 deletions

106
args.rkt
View File

@ -1,53 +1,53 @@
#lang racket/base #lang racket/base
(require (prefix-in vector: racket/vector) (require (prefix-in vector: racket/vector)
(prefix-in list: racket/list) (prefix-in list: racket/list)
(prefix-in config: "config.rkt") (prefix-in config: "config.rkt")
(prefix-in init: "init.rkt") (prefix-in init: "init.rkt")
(prefix-in util: "util.rkt") (prefix-in util: "util.rkt")
(prefix-in messages: "messages.rkt")) (prefix-in messages: "messages.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
(define (check-args args) (define (check-args args)
(let ([args-length (vector-length args)]) (let ([args-length (vector-length args)])
(cond (cond
[(equal? args-length 0) [(equal? args-length 0)
(util:display-hash-ref messages:messages 'show-usage)] (util:display-hash-ref messages:messages 'show-usage)]
;; ls ;; ls
[(and [(and
(equal? args-length 1) (equal? args-length 1)
(equal? (vector:vector-member config:list-command args) 0)) (equal? (vector:vector-member config:list-command args) 0))
(util:show-list-from-program-file)] (util:show-list-from-program-file)]
;; add ;; add
[(and [(and
(or (equal? args-length 2) (>= args-length 2)) (or (equal? args-length 2) (>= args-length 2))
(equal? (vector-ref args 0) config:add-command)) (equal? (vector-ref args 0) config:add-command))
(util:add-item-to-list args)] (util:add-item-to-list args)]
;; rm ;; rm
[(and [(and
(equal? args-length 2) (equal? args-length 2)
(equal? (vector:vector-member config:remove-command args) 0) (equal? (vector-ref args 0) config:remove-command)
(real? (string->number (vector-ref args 1))) (real? (string->number (vector-ref args 1)))
(positive? (string->number (vector-ref args 1))) (positive? (string->number (vector-ref args 1)))
(not (> (string->number (vector-ref args 1)) (length (util:file->string-list config:path-to-file)))) ;; Length subtract one because the numbering starts at zero
(not (< (string->number (vector-ref args 1)) (car (list:range (length (util:file->string-list config:path-to-file))))))) (not (> (string->number (vector-ref args 1)) (sub1 (length (util:file->string-list config:path-to-file)))))
(util:remove-item-from-list args)] (util:remove-item-from-list args))]
;; init ;; init
[(and [(and
(equal? args-length 1) (equal? args-length 1)
(equal? (vector:vector-member config:initialize-command args) 0)) (equal? (vector:vector-member config:initialize-command args) 0))
(init:initialize)] (init:initialize)]
;; help ;; help
[(and [(and
(equal? args-length 1) (equal? args-length 1)
(member (vector-ref args 0) config:help-command)) (member (vector-ref args 0) config:help-command))
(util:display-hash-ref messages:messages 'show-help)] (util:display-hash-ref messages:messages 'show-help)]
[else [else
(util:display-hash-ref messages:messages 'show-usage)]))) (util:display-hash-ref messages:messages 'show-usage)])))

View File

@ -1,12 +1,12 @@
#lang racket/base #lang racket/base
(provide (all-defined-out)) (provide (all-defined-out))
(define program-name "rodo") (define program-name "rodo")
(define program-directory (path->string (expand-user-path "~/.rodo/"))) (define program-directory (path->string (expand-user-path "~/.rodo/")))
(define program-file "to-do.txt") (define program-file "to-do.txt")
(define remove-command "rm") (define remove-command "rm")
(define add-command "add") (define add-command "add")
(define list-command "ls") (define list-command "ls")
(define initialize-command "init") (define initialize-command "init")
(define help-command '("-h" "--help")) (define help-command '("-h" "--help"))
(define path-to-file (string-append program-directory program-file)) (define path-to-file (string-append program-directory program-file))

View File

@ -1,36 +1,36 @@
#lang racket/base #lang racket/base
(require (prefix-in file: racket/file) (require (prefix-in file: racket/file)
(prefix-in config: "config.rkt") (prefix-in config: "config.rkt")
(prefix-in util: "util.rkt") (prefix-in util: "util.rkt")
(prefix-in messages: "messages.rkt")) (prefix-in messages: "messages.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
(define (init-prompt hash-list key) (define (init-prompt hash-list key)
(util:display-hash-ref hash-list key) (util:display-hash-ref hash-list key)
(display "> ") (display "> ")
(let (let
([user-input (read-line)]) ([user-input (read-line)])
(cond (cond
[(member user-input (hash-ref messages:y/n 'yes)) [(member user-input (hash-ref messages:y/n 'yes))
(util:display-hash-ref messages:messages 'creating) (util:display-hash-ref messages:messages 'creating)
(util:create-program-directory) (util:create-program-directory)
(util:create-program-file) (util:create-program-file)
(if (and (if (and
(util:check-for-program-directory) (util:check-for-program-directory)
(util:check-for-program-file)) (util:check-for-program-file))
(util:display-hash-ref messages:messages 'successfully-created) (util:display-hash-ref messages:messages 'successfully-created)
(util:display-hash-ref messages:messages 'creation-error))] (util:display-hash-ref messages:messages 'creation-error))]
[(member user-input (hash-ref messages:y/n 'no)) [(member user-input (hash-ref messages:y/n 'no))
(util:display-hash-ref messages:messages 'terminating)] (util:display-hash-ref messages:messages 'terminating)]
[else [else
(init-prompt messages:messages 'choose-y/n)]))) (init-prompt messages:messages 'choose-y/n)])))
(define (initialize) (define (initialize)
(if (util:check-for-program-file) (if (util:check-for-program-file)
(util:display-hash-ref messages:messages 'file-already-exists) (util:display-hash-ref messages:messages 'file-already-exists)
(begin (begin
(init-prompt messages:messages 'init-y/n)))) (init-prompt messages:messages 'init-y/n))))

View File

@ -38,25 +38,22 @@
(list:empty? (file->string-list lst))) (list:empty? (file->string-list lst)))
(define (get-removed-item lst args) (define (get-removed-item lst args)
;; Subtract one from what the user chose, because what they are actually (list-ref (file->string-list lst) (string->number args)))
;; viewing is a list starting from "1" rather than "0". Under the hood,
;; the real list starts at 0.
(list-ref (file->string-list lst) (sub1 (string->number args))))
(define (surround-with-quotation-marks args) (define (surround-with-quotation-marks args)
(display (string-append "\"" args "\""))) (display (string-append "\"" args "\"")))
(define (list->dotted-list lst) (define (list->dotted-list lst)
(string-append lst ". ")) (string-append lst "." " "))
(define (list->numbered-list lst) (define (list->numbered-list lst)
;; Take the list made in the first (map), which is ;; Take the list made in the first (map), which is
;; '(1 2 3 ...), and append that to each item in a list ;; '(0 1 2 ...), and append that to each item in a list
(map string-append (map string-append
;; Note: compose starts from the last element in it's ;; Note: compose starts from the last element in it's
;; list. In this case, it starts at (number->string). ;; list. In this case, it starts at (number->string).
(map (compose1 list->dotted-list number->string) (map (compose1 list->dotted-list number->string)
(list:range 1 (add1 (length lst)))) (list:range (length lst)))
lst)) lst))
(define (display-prettified-program-file) (define (display-prettified-program-file)