attempting a cleanup of this awful old code haha

cleanup
m455 2021-04-15 09:20:32 -04:00
parent b0976283c9
commit e62642004a
1 changed files with 134 additions and 201 deletions

View File

@ -17,8 +17,8 @@
(define add-command "add") (define add-command "add")
(define program-name "nicethings") (define program-name "nicethings")
(define program-file (string-append "." program-name)) (define program-file (string-append "." program-name))
(define program-path (build-path (find-system-path 'home-dir) program-file)) (define program-path (path->string (build-path (find-system-path 'home-dir) program-file))
(define program-permissions 420) ;; 644/-rw-r--r-- permissions (define read-permissions 444)
(define newline "\n") (define newline "\n")
(define double-newline "\n\n") (define double-newline "\n\n")
@ -26,8 +26,7 @@
;; Messages ;; Messages
;; ------------------------------------------------ ;; ------------------------------------------------
(define messages (define messages
(hash (hash 'error-too-many-arguments
'error-too-many-arguments
(format (string-append "Error: Too many arguments." newline (format (string-append "Error: Too many arguments." newline
"Try running '~a ~a' for more information.") "Try running '~a ~a' for more information.")
program-name program-name
@ -39,14 +38,16 @@
program-name program-name
help-command-1) help-command-1)
'error-file-already-exists 'couldnt-find-file
(format "Error: The file '~a' already exists." program-path) (format (string-append "Error: Couldn't find ~a" newline
"Either the file doesn't exist, or the permissions are incorrect." newline
'error-fake-file-exists "If the file exists, try running chmod 600 ~a")
(format (string-append "Error: The directory '~a' exists." newline program-path
"Try moving, renaming, or deleting this directory.")
program-path) program-path)
'error-something-exists
(format "Error: It looks like ~a already exists." program-path)
'error-file-doesnt-exist 'error-file-doesnt-exist
(format (string-append "Error: '~a' doesn't exist." newline (format (string-append "Error: '~a' doesn't exist." newline
"Try running '~a ~a'.") "Try running '~a ~a'.")
@ -57,6 +58,11 @@
'error-item-not-found 'error-item-not-found
"Error: Item not found." "Error: Item not found."
'error-not-an-option
"Error: not an option."
'error-not-a-number
"Error: not a number."
'warning-permissions 'warning-permissions
(format (string-append "Warning: The permissions on your '~a' file are incorrect." newline (format (string-append "Warning: The permissions on your '~a' file are incorrect." newline
@ -69,25 +75,15 @@
(format "Cancelled the creation of '~a'." program-path) (format "Cancelled the creation of '~a'." program-path)
'init-prompt 'init-prompt
(format (string-append "~a will create '~a'." newline (format (string-append "The file '~a' will be created." newline
"Is this okay? [y/n]") "Is this okay? [y/n]")
program-name
program-path) program-path)
'file-created 'file-created
(format "Successfully created ~a." program-path) (format "Successfully created ~a." program-path)
'empty-list 'empty-list
"There is nothing in your list.")) "There is nothing in your list."
(define formatees
(hash
'error-not-an-option
(string-append "Error: '~a' is not an option." newline
"Please choose 'y' or 'n'.")
'error-not-a-number
(string-append "Error: '~a' is not a number.")
'added 'added
"Added '~a' to your list." "Added '~a' to your list."
@ -101,95 +97,57 @@
(define (messages-ref key) (define (messages-ref key)
(hash-ref messages key)) (hash-ref messages key))
(define (formatees-ref key)
(hash-ref formatees key))
(define (displayln-messages-ref key) (define (displayln-messages-ref key)
(let ([message (messages-ref key)]) (displayln (messages-ref key)))
(displayln message)))
(define (displayln-formatees-ref key string) (define (displayln-format-messages-ref key value)
(let* ([formatee (formatees-ref key)] (displayln (format (messages-ref key) value)))
[formatee-formatted (format formatee string)])
(displayln formatee-formatted)))
(define (create-file string) (define (create-file string)
(close-output-port (close-output-port (open-output-file string)))
(open-output-file string)))
(define (has-program-permissions? string) (define (has-read-permissions? string)
(equal? program-permissions (file-or-directory-permissions string 'bits))) (equal? program-permissions (file-or-directory-permissions string 'bits)))
;; ------------------------------------------------
;; Check conditions
;; ------------------------------------------------
(define (check-conditions)
(cond
[(directory-exists? program-path)
(begin (displayln-messages-ref 'error-fake-file-exists)
(exit))]
[(not (file-exists? program-path))
(begin (displayln-messages-ref 'error-file-doesnt-exist)
(exit))]
[(not (has-program-permissions? program-path))
(displayln-messages-ref 'warning-permissions)]
[else 'do-nothing]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; init ;; init
;; ------------------------------------------------ ;; ------------------------------------------------
(define (init/create-file)
(create-file program-path)
(file-or-directory-permissions program-path
program-permissions)
(displayln-messages-ref 'file-created))
(define (init/cancel) (define (init/cancel)
(displayln-messages-ref 'init-cancelled) (displayln-messages-ref 'init-cancelled)
(exit)) (exit))
(define (init/create-file)
(create-file program-path)
(file-or-directory-permissions program-path read-permissions)
(displayln-messages-ref 'file-created))
(define (init/prompt) (define (init/prompt)
(displayln-messages-ref 'init-prompt) (displayln-messages-ref 'init-prompt)
(display "> ") (display "> ")
(let* ([input (read-line)] (let ([input-symbol (string->symbol (read-line))])
[input-symbol (string->symbol input)])
(case input-symbol (case input-symbol
['y (init/create-file)] ['y (init/create-file)]
['n (init/cancel)] ['n (init/cancel)]
[else (displayln-formatees-ref 'error-not-an-option input)]))) [else (displayln-messages-ref 'error-not-an-option)])))
(define (init) (define (init)
(cond (if (or (file-exists? program-path)
[(file-exists? program-path) (directory-exists? program-path))
(displayln-messages-ref 'error-file-already-exists)] (displayln-messages-ref 'error-something-exists)
(init/prompt)))
[(directory-exists? program-path)
(displayln-messages-ref 'error-fake-file-exists)]
[else (init/prompt)]))
;; ------------------------------------------------ ;; ------------------------------------------------
;; ls ;; ls
;; ------------------------------------------------ ;; ------------------------------------------------
(define (ls/display-list listof-items) (define (ls/display-list listof-items)
;; The add1 in the first binding starts the (let* ([listof-number-strings (map number->string (range (length listof-items)))]
;; listof-numbers at 1 instead of 0 to make the [listof-numbered-items (map (lambda (a b) (string-append a ". " b))
;; list numbering more human-friendly
(let* ([listof-numbers (map add1 (range (length listof-items)))]
[listof-number-strings (map number->string listof-numbers)]
[combine-lists (lambda (a b) (string-append a ". " b))]
[listof-numbered-items (map combine-lists
listof-number-strings listof-number-strings
listof-items)]) listof-items)])
(for ([item listof-numbered-items]) (for ([item listof-numbered-items])
(displayln item)))) (displayln item))))
(define (ls) (define (ls)
(check-conditions)
(let ([listof-items (file->lines program-path)]) (let ([listof-items (file->lines program-path)])
(if (null? listof-items) (if (null? listof-items)
(displayln-messages-ref 'empty-list) (displayln-messages-ref 'empty-list)
@ -198,75 +156,47 @@
;; ------------------------------------------------ ;; ------------------------------------------------
;; rm ;; rm
;; ------------------------------------------------ ;; ------------------------------------------------
(define (rm/remove-item listof-items item-number) (define (rm/remove-nicething item-number)
(let ([listof-items (file->lines program-path)])
(if (and (not (null? listof-items))
(>= item-number 0)
(< item-number (length listof-items)))
(let* ([item-to-remove (list-ref listof-items item-number)] (let* ([item-to-remove (list-ref listof-items item-number)]
[list-without-item (remove item-to-remove listof-items)]) [list-without-item (remove item-to-remove listof-items)])
(display-lines-to-file list-without-item (display-lines-to-file list-without-item program-path #:exists 'truncate)
program-path (displayln-format-messages-ref 'removed item-to-remove))
#:exists 'truncate)
(displayln-formatees-ref 'removed item-to-remove)))
(define (rm/process-string string)
(let* ([listof-items (file->lines program-path)]
[item-number (string->number string)]
;; Subtract 1 from the user's original number,
;; because we want to convert the number from
;; human numbers (1 2 3) to index numbers
;; (0 1 2)
[item-number-sub1 (sub1 item-number)]
[list-length (length listof-items)])
(if (and (not (null? listof-items))
(number? item-number)
(positive? item-number)
;; Don't allow numbers that are equal to
;; or greater than list-length, because
;; the list index starts at 0
;;
;; Example:
;; Length of (1 2 3) = 3
;;
;; To reference the highest number, we
;; use (list-ref (1 2 3) 2)
(< item-number-sub1 list-length))
(rm/remove-item listof-items item-number-sub1)
(displayln-messages-ref 'error-item-not-found)))) (displayln-messages-ref 'error-item-not-found))))
(define (rm string) (define (rm arg)
(check-conditions) (let ([item-number (string->number arg)])
(if (string->number string) (if item-number
(rm/process-string string) (rm/remove-nicething item-number)
(displayln-formatees-ref 'error-not-a-number string))) (displayln-format-messages-ref 'error-not-a-number arg))))
;; ------------------------------------------------ ;; ------------------------------------------------
;; add ;; add
;; ------------------------------------------------ ;; ------------------------------------------------
(define (add string) (define (add nicething)
(check-conditions)
;; The removing and adding of the '\n' is to ;; The removing and adding of the '\n' is to
;; ensure only one '\n' exists at the end of the ;; ensure only one '\n' exists at the end of the
;; item to be added. ;; item to be added.
(let* ([string-no-newline (string-replace string "\n" "")] (let* ([nicething-no-newline (string-replace nicething "\n" "")]
[string-newline (string-append string-no-newline "\n")]) [nicething-newline (string-append nicething-no-newline "\n")])
(display-to-file string-newline (display-to-file nicething-newline program-path #:exists 'append)
program-path (displayln-format-messages-ref 'added nicething-no-newline)))
#:exists 'append)
(displayln-formatees-ref 'added string-no-newline)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; random message ;; random message
;; ------------------------------------------------ ;; ------------------------------------------------
(define (random-message/append-program-file home-directory)
(build-path home-directory program-file))
(define (random-message) (define (random-message)
(let* ([root (find-system-path 'sys-dir)] ;; / (let* ([root-home (build-path (find-system-path 'sys-dir) "home")] ;; /home
[root-home (build-path root "home")] ;; /home
;; `#:build? #t` Builds full paths for all items listed in /home/ ;; `#:build? #t` Builds full paths for all items listed in /home/
[listof-home-directories (directory-list root-home #:build? #t)] [listof-home-directories (directory-list root-home #:build? #t)]
[listof-nicethings-paths (map random-message/append-program-file listof-home-directories)] [listof-nicethings-paths (map (lambda (x) (build-path x program-file))
[directories-with-nicethings (filter file-exists? listof-nicethings-paths)] (listof-home-directories))]
[directories-with-644 (filter has-program-permissions? directories-with-nicethings)] [paths-with-nicethings (filter file-exists? listof-nicethings-paths)]
[listof-nicethings (apply append (map file->lines directories-with-644))] [paths-readable (filter has-read-permissions? paths-with-nicethings)]
[listof-nicethings (apply append (map file->lines paths-readable))]
[list-length (length listof-nicethings)]) [list-length (length listof-nicethings)])
(when (not (zero? list-length)) (when (not (zero? list-length))
(let* ([random-number (random list-length)] (let* ([random-number (random list-length)]
@ -313,6 +243,9 @@
[(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)])) [(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)]))
(define (main vectorof-args) (define (main vectorof-args)
(process-args vectorof-args)) (if (and (file-exists program-file)
(has-read-permissions? program-file))
(process-args vectorof-args)
(displayln-messages-ref 'couldnt-find-file)))
(main (current-command-line-arguments)) (main (current-command-line-arguments))