Skip to content

Commit

Permalink
Merge pull request #3310 from atlas-engineer/refactor-hook-handler-co…
Browse files Browse the repository at this point in the history
…mmands

Refactor `{enable,disable}-hook-handler` commands.
  • Loading branch information
aadcg authored Jan 17, 2024
2 parents 3d94f46 + 10303f5 commit f6701a2
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions source/command-commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ User input is evaluated Lisp."

(define-class hook-source (prompter:source)
((prompter:name "Hooks")
(prompter:constructor (get-hooks))))
(prompter:constructor (get-hooks))
(prompter:actions-on-return (lambda-mapped-command value))))

(defmethod prompter:object-attributes ((hook-description hook-description) (source hook-source))
(declare (ignore source))
`(("Name" ,(name hook-description))
("Value" ,(value hook-description))))
`(("Name" ,(name hook-description))))

(define-class handler-source (prompter:source)
((prompter:name "Handlers")
Expand All @@ -315,32 +315,30 @@ User input is evaluated Lisp."
:documentation "The hook for which to retrieve handlers for.")
(prompter:constructor (lambda (source) (hooks:handlers (hook source))))))

(defmethod prompter:object-attributes ((handler hooks:handler) (source handler-source))
(defmethod prompter:object-attributes ((handler symbol) (source handler-source))
(declare (ignore source))
`(("Name" ,(str:downcase (hooks:name handler)))))

(define-class disabled-handler-source (handler-source)
((prompter:constructor (lambda (source)
(hooks:disabled-handlers (hook source))))))
((prompter:constructor (lambda (source) (hooks:disabled-handlers (hook source))))))

(defun manage-hook-handler (action)
(let ((hook (prompt1 :prompt "Hook"
:sources 'hook-source)))
(funcall (case action
(:enable #'hooks:enable-hook)
(:disable #'hooks:disable-hook))
hook
(prompt1 :prompt "Handler"
:sources (make-instance (case action
(:enable 'disabled-handler-source)
(:disable 'handler-source))
:hook hook)))))

(define-command-global disable-hook-handler ()
"Remove handler(s) from a hook."
(let* ((hook-desc (prompt1
:prompt "Hook where to disable handler"
:sources 'hook-source))
(handler (prompt1
:prompt (format nil "Disable handler from ~a" (name hook-desc))
:sources (make-instance 'handler-source
:hook (value hook-desc)))))
(hooks:disable-hook (value hook-desc) handler)))
"Remove handler of a hook."
(manage-hook-handler :disable))

(define-command-global enable-hook-handler ()
"Enable handler(s) from a hook."
(let* ((hook-desc (prompt1
:prompt "Hook where to enable handler"
:sources 'hook-source))
(handler (prompt1
:prompt (format nil "Enable handler from ~a" (name hook-desc))
:sources (make-instance 'disabled-handler-source
:hook (value hook-desc)))))
(hooks:enable-hook (value hook-desc) handler)))
"Add handler of a hook."
(manage-hook-handler :enable))

0 comments on commit f6701a2

Please sign in to comment.