diff options
author | Colin Okay <okay@toyful.space> | 2022-03-11 08:35:55 -0600 |
---|---|---|
committer | Colin Okay <okay@toyful.space> | 2022-03-11 08:35:55 -0600 |
commit | 08ba2769abb7a36817a725d30d64cfd36f5bcf32 (patch) | |
tree | 3068a5d9e1a4afd06a5f8b236658d4094e30dfef /lib/util.lisp | |
parent | 62afeb3f21d3d8e8db045a001271e686e944a049 (diff) |
separated app and lib modules, -osicat dep, +packages.lisp
Diffstat (limited to 'lib/util.lisp')
-rw-r--r-- | lib/util.lisp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/util.lisp b/lib/util.lisp new file mode 100644 index 0000000..290d541 --- /dev/null +++ b/lib/util.lisp @@ -0,0 +1,37 @@ +;;;; util.lisp + +(in-package :oneliners.cli) + +(defun make-temp-file-name () + "Simply makes a file name for a temp file. Uses +UIOP:TEMPORARY-DIRECTORY for the directory." + (namestring + (merge-pathnames (format nil "~a~a" (gensym "oneliners") (get-universal-time)) + (uiop:temporary-directory)))) + + +(defun string-from-editor (&optional contents) + (let ((filename (make-temp-file-name))) + (when contents (a:write-string-into-file contents filename :if-exists :supersede)) + (unwind-protect + (magic-ed:magic-ed filename :eval nil :output :string) + (uiop:delete-file-if-exists filename)))) + + +(defun executable-on-system-p (name) + "A hack that heuristically determines whether or not an executable +with the provided name is on the system. It is not perfect. It +consults the environment PATH, and looks for the command in any of +the directories that appear in the value of that variable." + #+unix + (loop for path in (str:split ":" (uiop:getenv "PATH")) + for directory = (cons :absolute + (cdr (str:split "/" path))) + thereis (uiop:file-exists-p + (make-pathname :name name :directory directory)))) + +(defun print-to-file (printable-object pathname &optional (if-exists :supersede)) + "Prints an object to a file, ensuring that the containing directory exists first." + (ensure-directories-exist pathname) + (with-open-file (out pathname :direction :output :if-exists if-exists) + (print printable-object out))) |