hopefully fixed this dumpster fire of code up?

cleanup
m455 2021-04-15 09:31:07 -04:00
parent cf4af31cd7
commit 525326c5dd
1 changed files with 31 additions and 22 deletions

View File

@ -41,9 +41,12 @@
'couldnt-find-file 'couldnt-find-file
(format (string-append "Error: Couldn't find ~a" newline (format (string-append "Error: Couldn't find ~a" newline
"Either the file doesn't exist, or the permissions are incorrect." newline "Either the file doesn't exist, or the permissions are incorrect." newline
"If the file exists, try running chmod 600 ~a") "If the file exists, try running chmod 600 ~a"
"If the file doesn't exist, try running ~a ~a")
program-path program-path
program-path) program-path
program-name
init-command)
'error-something-exists 'error-something-exists
(format "Error: It looks like ~a already exists." program-path) (format "Error: It looks like ~a already exists." program-path)
@ -107,7 +110,7 @@
(close-output-port (open-output-file string))) (close-output-port (open-output-file string)))
(define (has-read-permissions? string) (define (has-read-permissions? string)
(equal? program-permissions (file-or-directory-permissions string 'bits))) (equal? read-permissions (file-or-directory-permissions string 'bits)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; init ;; init
@ -148,10 +151,13 @@
(displayln item)))) (displayln item))))
(define (ls) (define (ls)
(let ([listof-items (file->lines program-path)]) (if (and (file-exists? program-file)
(if (null? listof-items) (has-read-permissions? program-file))
(let ([listof-items (file->lines program-path)])
(if (null? listof-items)
(displayln-messages-ref 'empty-list) (displayln-messages-ref 'empty-list)
(ls/display-list listof-items)))) (ls/display-list listof-items)))
(displayln-messages-ref 'couldnt-file-file)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; rm ;; rm
@ -168,22 +174,28 @@
(displayln-messages-ref 'error-item-not-found)))) (displayln-messages-ref 'error-item-not-found))))
(define (rm arg) (define (rm arg)
(let ([item-number (string->number arg)]) (if (and (file-exists? program-file)
(if item-number (has-read-permissions? program-file))
(rm/remove-nicething item-number) (let ([item-number (string->number arg)])
(displayln-format-messages-ref 'error-not-a-number arg)))) (if item-number
(rm/remove-nicething item-number)
(displayln-format-messages-ref 'error-not-a-number arg)))
(displayln-messages-ref 'couldnt-find-file)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; add ;; add
;; ------------------------------------------------ ;; ------------------------------------------------
(define (add nicething) (define (add nicething)
;; The removing and adding of the '\n' is to (if (and (file-exists? program-file)
;; ensure only one '\n' exists at the end of the (has-read-permissions? program-file))
;; item to be added. ;; The removing and adding of the '\n' is to
(let* ([nicething-no-newline (string-replace nicething "\n" "")] ;; ensure only one '\n' exists at the end of the
[nicething-newline (string-append nicething-no-newline "\n")]) ;; item to be added.
(display-to-file nicething-newline program-path #:exists 'append) (let* ([nicething-no-newline (string-replace nicething "\n" "")]
(displayln-format-messages-ref 'added nicething-no-newline))) [nicething-newline (string-append nicething-no-newline "\n")])
(display-to-file nicething-newline program-path #:exists 'append)
(displayln-format-messages-ref 'added nicething-no-newline))
(displayln-messages-ref 'couldnt-find-file)))
;; ------------------------------------------------ ;; ------------------------------------------------
;; random message ;; random message
@ -193,7 +205,7 @@
;; `#: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 (lambda (x) (build-path x program-file)) [listof-nicethings-paths (map (lambda (x) (build-path x program-file))
(listof-home-directories))] listof-home-directories)]
[paths-with-nicethings (filter file-exists? listof-nicethings-paths)] [paths-with-nicethings (filter file-exists? listof-nicethings-paths)]
[paths-readable (filter has-read-permissions? paths-with-nicethings)] [paths-readable (filter has-read-permissions? paths-with-nicethings)]
[listof-nicethings (apply append (map file->lines paths-readable))] [listof-nicethings (apply append (map file->lines paths-readable))]
@ -243,9 +255,6 @@
[(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)])) [(vector _ ...) (displayln-messages-ref 'error-incorrect-usage)]))
(define (main vectorof-args) (define (main vectorof-args)
(if (and (file-exists program-file) (process-args vectorof-args))
(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))