Skip to content

Instantly share code, notes, and snippets.

@scottjad
Created March 28, 2013 13:00
Show Gist options
  • Save scottjad/5262930 to your computer and use it in GitHub Desktop.
Save scottjad/5262930 to your computer and use it in GitHub Desktop.
(in-package :stumpwm)
(defun select-from-dmenu (screen table &optional prompt
(initial-selection 0))
"Prompt the user to select from a menu using dmenu on SCREEN. TABLE can be
a list of values or an alist. If it's an alist, the CAR of each
element is displayed in the menu. What is displayed as menu items
must be strings. Returns the selected element in TABLE or nil if aborted."
(let* ((menu-options (mapcar #'menu-element-name table))
(cmd (format nil "echo \"~{~A\\n~}\" | dmenu"
;; FIXME can't handle two items with same string representation
menu-options))
(selection-string (string-trim '(#\Newline)
(run-shell-command cmd t)))
(selection (find selection-string menu-options
:test (lambda (selection-string item)
(string-equal selection-string (format nil "~A" item))))))
(if (listp (car table))
(assoc selection table)
selection)))
;; (select-from-dmenu nil '(1 2 3))
;; (select-from-dmenu nil '("foo" "bar"))
;; (select-from-dmenu nil '(("a" 1) ("b" 2)))
;; (select-from-dmenu nil (mapcar (lambda (w) (list (format-expand *window-formatters* *window-format* w) w)) (sort-windows (current-group))))
;; (select-from-dmenu nil (cons-listing nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment