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

View File

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

View File

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

View File

@ -38,25 +38,22 @@
(list:empty? (file->string-list lst)))
(define (get-removed-item lst args)
;; 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.
(list-ref (file->string-list lst) (sub1 (string->number args))))
(list-ref (file->string-list lst) (string->number args)))
(define (surround-with-quotation-marks args)
(display (string-append "\"" args "\"")))
(define (list->dotted-list lst)
(string-append lst ". "))
(string-append lst "." " "))
(define (list->numbered-list lst)
;; 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
;; Note: compose starts from the last element in it's
;; list. In this case, it starts at (number->string).
(map (compose1 list->dotted-list number->string)
(list:range 1 (add1 (length lst))))
(list:range (length lst)))
lst))
(define (display-prettified-program-file)