rodo/rodo.rkt

98 lines
3.6 KiB
Racket
Raw Normal View History

2018-03-12 14:02:54 +00:00
#! /usr/bin/env racket
#lang racket/base
2018-04-02 19:16:01 +00:00
2018-04-02 17:51:08 +00:00
(require json racket/vector)
2018-03-12 14:02:54 +00:00
(define program-name "rodo")
2018-04-02 18:56:59 +00:00
(define program-file ".rodo")
(define program-path "~/")
2018-04-02 18:24:06 +00:00
(define (d-hash-ref hash-list key)
2018-03-13 02:19:16 +00:00
(displayln (hash-ref hash-list key)))
2018-04-03 02:10:59 +00:00
;; if no command-line arguments are called this will throw an error, even if the function is never called :(
2018-04-02 18:56:59 +00:00
;;(define (add-to-list)
;; (let ([args (vector-ref (current-command-line-arguments) 1)])
;; (string-append "> Added " args " to list")))
2018-04-02 17:51:08 +00:00
2018-04-02 18:56:59 +00:00
;;(define (remove-from-list)
;; (let ([args (vector-ref (current-command-line-arguments) 1)])
;; (string-append "> Removed " args " from list")))
2018-04-02 17:51:08 +00:00
2018-04-02 15:37:33 +00:00
;; define all messages for quick access
2018-04-02 18:56:59 +00:00
(define messages
(hash
'incorrect-usage (string-append "> For usage type `" program-name " -h` or `" program-name " --help`")
2018-04-02 18:56:59 +00:00
2018-04-02 19:20:49 +00:00
'file-creating (string-append "> Creating a " program-file " file in your " program-path " directory...")
'file-creation-error (string-append "> Error: Could not create " program-file " in your " program-path " directory.\n> This may be due to directory permissions")
'file-already-exists (string-append "> " program-file " file already exists in " program-path)
'file-successfully-created (string-append "> " program-path program-file " have been successfully created")
2018-04-02 18:56:59 +00:00
'file-not-found (string-append "> " program-file " has not been setup in your " program-path " directory\n> Would you like to set it up now? [y/n]")
2018-04-02 15:37:33 +00:00
2018-04-02 17:51:08 +00:00
'item-added "> Added item to list"
'item-removed "> Added item to list"
2018-04-02 15:37:33 +00:00
'terminating (string-append "> Exiting " program-name "...")
'choose-y/n "> Error: Please choose y or n"))
2018-04-02 15:37:33 +00:00
;; some possible user-input related "mistakes" that will be accepted for input
(define y/n
(hash
'yes '("yes" "Yes" "y" "Y")
'no '("no" "No" "n" "N")))
2018-04-02 15:37:33 +00:00
;; yet again, less typing for opening a file
(define (open/create-file path)
(let ([path (expand-user-path path)])
(let ([opened-file (open-output-file path
#:mode 'text
#:exists 'can-update)])
(close-output-port opened-file))))
2018-03-13 16:39:10 +00:00
2018-04-03 02:10:59 +00:00
;; prompt user for file initial file creation
2018-04-03 01:48:03 +00:00
(define (prompt-user chosen-message)
2018-04-03 01:44:30 +00:00
(d-hash-ref messages chosen-message)
(display "> ")
2018-03-13 02:19:16 +00:00
(let ([user-input (read-line)])
(cond
2018-03-16 13:53:12 +00:00
[(member user-input (hash-ref y/n 'yes))
2018-04-02 19:20:49 +00:00
(d-hash-ref messages 'file-creating)
2018-04-02 18:56:59 +00:00
(open/create-file (string-append program-path program-file))
(if (file-exists? (expand-user-path (string-append program-path program-file)))
2018-04-02 19:20:49 +00:00
(d-hash-ref messages 'file-successfully-created)
(d-hash-ref messages 'file-creation-error))]
2018-04-03 02:10:59 +00:00
2018-03-16 13:53:12 +00:00
[(member user-input (hash-ref y/n 'no))
2018-04-02 18:24:06 +00:00
(d-hash-ref messages 'terminating)]
2018-04-03 02:10:59 +00:00
2018-03-13 02:19:16 +00:00
[else
2018-04-03 01:48:03 +00:00
(prompt-user 'choose-y/n)])))
2018-04-02 15:37:33 +00:00
(define (check-args args)
2018-04-02 17:51:08 +00:00
(let ([args-length (vector-length args)])
(cond
[(or (equal? args-length 0) (> args-length 2))
2018-04-02 18:24:06 +00:00
(d-hash-ref messages 'incorrect-usage)]
2018-04-02 18:56:59 +00:00
2018-04-02 17:51:08 +00:00
[(and (equal? args-length 2) (equal? (vector-member "add" args) 0))
2018-04-02 18:24:06 +00:00
(d-hash-ref messages 'item-added)]
2018-04-02 18:56:59 +00:00
[(and (equal? args-length 2) (equal? (vector-member "remove" args) 0))
(d-hash-ref messages 'item-removed)]
2018-04-02 17:51:08 +00:00
[(and (equal? args-length 1) (equal? (vector-member "init" args) 0))
(todo-list-exist?)])))
2018-04-02 15:37:33 +00:00
2018-04-03 02:10:59 +00:00
;; does the file exist that holds the list(s?)
2018-03-12 14:02:54 +00:00
(define (todo-list-exist?)
2018-04-02 18:56:59 +00:00
(if (file-exists? (expand-user-path (string-append program-path program-file)))
2018-04-02 19:20:49 +00:00
(d-hash-ref messages 'file-already-exists)
2018-04-03 01:48:03 +00:00
(prompt-user 'file-not-found)))
2018-03-12 14:02:54 +00:00
2018-04-02 17:51:08 +00:00
(define (main)
2018-04-02 17:55:36 +00:00
(check-args (current-command-line-arguments)))
2018-04-02 15:37:33 +00:00
2018-04-02 17:51:08 +00:00
(main)