backing this mess up for now
parent
b3824baba4
commit
da77be104d
154
nicethings.rkt
154
nicethings.rkt
|
@ -1,17 +1,46 @@
|
||||||
#lang racket
|
#lang racket
|
||||||
|
|
||||||
|
;; ------------------------------------------------
|
||||||
|
;; values
|
||||||
|
;; ------------------------------------------------
|
||||||
|
(define nicethings-string ".nicethings")
|
||||||
|
(define home-directory (find-system-path 'home-dir))
|
||||||
|
(define listof-home-contents (directory-list home-directory))
|
||||||
|
(define nicethings-path-local (build-path home-directory nicethings-string))
|
||||||
|
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
;; messages
|
;; messages
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
(define messages
|
(define messages
|
||||||
(hash
|
(hash
|
||||||
'not-found "'~a' wasn't found."
|
'not-found "> '~a' wasn't found."
|
||||||
'repair-prompt "You will need it to use nicethings.\nDo you want to create it? [y/n]\n> "
|
'repair-prompt "> You will need it to use nicethings.\n> Do you want to create it? [y/n]\n> "
|
||||||
'fake-file-found "The directory '~a' was found.\nPlease move this file somewhere else before using nicethings."
|
'fake-file-found "> The directory '~a' was found.\n> Please move this file somewhere else before using nicethings."
|
||||||
'not-a-command "Error: '~a' is not a command."
|
'try (list "> For usage help, try running the following command:"
|
||||||
'cancel-creation "Cancelled nicethings file creation."
|
"nicethings --help")
|
||||||
'file-created "'~a' was successfully created."
|
'cancel-creation "> Cancelled nicethings file creation."
|
||||||
'not-an-option "Error: '~a' is not an option."))
|
'file-created "> '~a' was successfully created."
|
||||||
|
'not-an-option "> Error: '~a' is not an option."
|
||||||
|
'add-expected-arg (list "> Error: Found 'add', but no arguments were found."
|
||||||
|
"> The 'add' command expects one quoted argument after it."
|
||||||
|
"> Example:"
|
||||||
|
"nicethings add \"You are beautiful\".")
|
||||||
|
'rm-expected-arg (list "> Error: Found 'rm', but no arguments were found."
|
||||||
|
"> The 'rm' command expects one number as an argument after it."
|
||||||
|
"> Example:"
|
||||||
|
"nicethings rm 2"
|
||||||
|
"> Note: You may need to use the 'ls' command to see which number correlates to which message.")
|
||||||
|
'ls-expected-no-args (list "> Error: Found 'ls', but also found other arguments."
|
||||||
|
"> The 'ls' command expects no arguments after it."
|
||||||
|
"> Example:"
|
||||||
|
"nicethings ls")
|
||||||
|
;; I don't currently use this message yet:
|
||||||
|
'rm-expected-number (list "> Error: Found '~a' after 'rm'."
|
||||||
|
"> The 'rm' command expects one number as an argument after it."
|
||||||
|
"> Example:"
|
||||||
|
"nicethings rm 2"
|
||||||
|
"> Note: You may need to use the 'ls' command to see which number correlates to which message.")
|
||||||
|
'added "> Added '~a' to the list."))
|
||||||
|
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
;; helpers
|
;; helpers
|
||||||
|
@ -28,42 +57,84 @@
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
;; repair
|
;; repair
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
|
(define (repair/not-an-option user-input)
|
||||||
|
(displayln-format (messages-ref 'not-an-option) user-input)
|
||||||
|
(exit))
|
||||||
|
|
||||||
(define (repair/cancel)
|
(define (repair/cancel)
|
||||||
(displayln (messages-ref 'cancel-creation))
|
(displayln (messages-ref 'cancel-creation))
|
||||||
(exit))
|
(exit))
|
||||||
|
|
||||||
;; Check for a "fake" '.nicethings' file,
|
;; Check for a "fake" '.nicethings' file,
|
||||||
;; which is a directory named '.nicethings'
|
;; which is a directory named '.nicethings'
|
||||||
(define (repair/start dot-nicethings-path)
|
(define (repair/start)
|
||||||
(if (directory-exists? dot-nicethings-path)
|
(if (directory-exists? nicethings-path-local)
|
||||||
(displayln-format (messages-ref 'fake-file-found) dot-nicethings-path)
|
(begin (displayln-format (messages-ref 'fake-file-found) nicethings-path-local)
|
||||||
(begin (close-output-port (open-output-file dot-nicethings-path))
|
(exit))
|
||||||
(displayln-format (messages-ref 'file-created) dot-nicethings-path))))
|
(begin (close-output-port (open-output-file nicethings-path-local))
|
||||||
|
(displayln-format (messages-ref 'file-created) nicethings-path-local))))
|
||||||
|
|
||||||
(define (repair/prompt dot-nicethings-path)
|
(define (repair/prompt)
|
||||||
(display (messages-ref 'repair-prompt))
|
(display (messages-ref 'repair-prompt))
|
||||||
(let ([user-input (read-line)])
|
(let ([user-input (read-line)])
|
||||||
(case (string->symbol user-input)
|
(case (string->symbol user-input)
|
||||||
['y (repair/start dot-nicethings-path)]
|
['y (repair/start)]
|
||||||
['n (repair/cancel)]
|
['n (repair/cancel)]
|
||||||
[else (displayln-format (messages-ref 'not-an-option) user-input)])))
|
[else (repair/not-an-option user-input)])))
|
||||||
|
|
||||||
(define (repair)
|
(define (repair)
|
||||||
(let* ([dot-nicethings-string ".nicethings"]
|
(when (not (file-exists? nicethings-path-local))
|
||||||
[home-directory (find-system-path 'home-dir)]
|
(displayln-format (messages-ref 'not-found) nicethings-path-local)
|
||||||
[listof-home-contents (directory-list home-directory)]
|
(repair/prompt)))
|
||||||
[dot-nicethings-path (build-path home-directory dot-nicethings-string)])
|
|
||||||
(when (not (file-exists? dot-nicethings-path))
|
;; ------------------------------------------------
|
||||||
(displayln-format (messages-ref 'not-found) dot-nicethings-path)
|
;; add message
|
||||||
(repair/prompt dot-nicethings-path))))
|
;; ------------------------------------------------
|
||||||
|
;; The string-cleaned and -remade is incase there
|
||||||
|
;; are multiple newline characters. This ensures
|
||||||
|
;; there is only one newline character.
|
||||||
|
(define (add string)
|
||||||
|
(repair)
|
||||||
|
(let* ([string-no-newline (string-replace string "\n" "")]
|
||||||
|
[string-newline (string-append string-no-newline "\n")])
|
||||||
|
;; tell user item was added
|
||||||
|
(display-to-file string-newline
|
||||||
|
nicethings-path-local
|
||||||
|
#:exists 'append)
|
||||||
|
(displayln-format (messages-ref 'added) string-no-newline)))
|
||||||
|
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
;; random message
|
;; random message
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
(define (random)
|
;; The +1 includes the last element in the list
|
||||||
;; (let* ([home (get-environment-variable "HOME")]
|
(define (random-message)
|
||||||
;; [listof-home-contents (directory home #t)])
|
(repair)
|
||||||
(repair))
|
;; TODO: Don't use local path here. turn into global
|
||||||
|
;; Algo:
|
||||||
|
;; gather all directories in /home/ -> list
|
||||||
|
;; filter file-exists? /home/directory/.nicethings -> list
|
||||||
|
;; NOTES:
|
||||||
|
;; /home
|
||||||
|
;;(define root-home (build-path (find-system-path 'sys-dir) "home"))
|
||||||
|
;; '(/home/username _ ...)
|
||||||
|
;;(map path->complete-path (directory-list root-home))
|
||||||
|
;;;;;;;;;;;;;;
|
||||||
|
;; NOTES2:
|
||||||
|
;;(define root-home (build-path (find-system-path 'sys-dir) "home"))
|
||||||
|
;;(map (lambda (x) (build-path root-home x)) (directory-list root-home))
|
||||||
|
;;;;;;;;;;;;
|
||||||
|
|
||||||
|
(let* ([root-home (build-path (find-system-path 'sys-dir) "home")]
|
||||||
|
[paths-to-nicethings (map (lambda (home-directory) (build-path root-home home-directory nicethings-string))
|
||||||
|
(directory-list root-home))])
|
||||||
|
(filter (lambda (x) (file-exists? x)) paths-to-nicethings)))
|
||||||
|
;; [listof-paths (map (lambda (path) (filter file-exists? path)) paths-to-nicethings)])
|
||||||
|
|
||||||
|
;; (when (not (zero? list-length))
|
||||||
|
;; (let* ([random-number (random list-length)]
|
||||||
|
;; [random-nicething (list-ref listof-nicethings random-number)])
|
||||||
|
;; (displayln random-nicething)))))
|
||||||
|
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
;; help
|
;; help
|
||||||
;; ------------------------------------------------
|
;; ------------------------------------------------
|
||||||
|
@ -73,10 +144,10 @@
|
||||||
" town nicethings [<command>] [<args>]"
|
" town nicethings [<command>] [<args>]"
|
||||||
""
|
""
|
||||||
"Commands:"
|
"Commands:"
|
||||||
" No command - Print a random message."
|
" No command - Print a random nice thing."
|
||||||
" add - Add a message to the list of messages."
|
" add - Add a message to the list of nice things."
|
||||||
" ls - Print a numbered list of the messages you have added."
|
" ls - Print a numbered list of the nice things you have added."
|
||||||
" rm - Remove a message from the list of messages."
|
" rm - Remove a message you have added from the list of nice things."
|
||||||
""
|
""
|
||||||
"Examples:"
|
"Examples:"
|
||||||
" town nicethings"
|
" town nicethings"
|
||||||
|
@ -85,15 +156,24 @@
|
||||||
" town rm 2"))
|
" town rm 2"))
|
||||||
|
|
||||||
(define (process-args vectorof-args)
|
(define (process-args vectorof-args)
|
||||||
|
(define (args-ref number)
|
||||||
|
(vector-ref vectorof-args number))
|
||||||
|
(define (display-message key)
|
||||||
|
(apply displayln-for (messages-ref key)))
|
||||||
(match vectorof-args
|
(match vectorof-args
|
||||||
[(or (vector "-h")
|
;; Proper usage
|
||||||
(vector "--help")
|
[(or '#("-h")
|
||||||
(vector "help")) (help)]
|
'#("--help")
|
||||||
[(vector "add" _) (displayln "todo: make add procedure")]
|
'#("help")) (help)]
|
||||||
[(vector "ls") (displayln "todo: make ls procedure")]
|
[(vector "add" _) (add (args-ref 1))]
|
||||||
[(vector "rm" _) (displayln "todo: make rm procedure")]
|
[(vector "rm" _) (displayln "todo: make rm procedure")]
|
||||||
[(vector _) (displayln-format (messages-ref 'not-a-command) (vector-ref vectorof-args 0))]
|
[(vector "ls") (displayln "todo: make ls procedure")]
|
||||||
[_ (random)]))
|
[(vector) (random-message)]
|
||||||
|
;; Improper usage (Give the user hints if part of the usage is correct)
|
||||||
|
[(vector "ls" _) (display-message 'ls-expected-no-args)]
|
||||||
|
[(vector "add") (display-message 'add-expected-arg)]
|
||||||
|
[(vector "rm") (display-message 'rm-expected-arg)]
|
||||||
|
[(vector _ ...) (display-message 'try)]))
|
||||||
|
|
||||||
(define (main vectorof-args)
|
(define (main vectorof-args)
|
||||||
(process-args vectorof-args))
|
(process-args vectorof-args))
|
||||||
|
|
Loading…
Reference in New Issue