-
Notifications
You must be signed in to change notification settings - Fork 96
Interactive Hooks
rthrd edited this page Oct 22, 2014
·
8 revisions
The BufferManager contains several handy dialog functions:
These functions display a prompt at the bottom of the screen; answers may be typed directly or choosen by tab-completion
The names of the functions are quite self-explanatory.
Common arguments are:
token for internal use of BufferManager
string used as prompt
array of possible answers, that can be completed by using the tab key
default value, it is written after the prompt and has to be deleted in order to substitute it!
Of use for writing interacive hooks are namely:
BufferManager::ask_with_completions domain, question, completions, default=nil
BufferManager::ask_many_with_completions domain, question, completions, default=nil
BufferManager::ask_many_emails_with_completions domain, question, completions, default=nil
BufferManager::ask_for_filename domain, question, default=nil, allow_directory=falseer::
BufferManager::ask_for_labels domain, question, default_labels, forbidden_labels=[]
BufferManager::ask_for_contacts domain, question, default_contacts=[]
BufferManager::ask_for_account domain, question
BufferManager::ask_yes_or_no question
This is an example of an interactive signature hook. It prompts the user for a keyword to choose the signature that should be used for this message
### signature.rb -- generate signature
### This example demonstrates how to generate the signature interactively
## Configuration
# Hash containing name => signature pairs
signatures = {
"professional" => """
This is the Grand Professional Signature.
""",
"familiar" => """
This is the Good Familiar Signature.
""",
"private" => """
This is the Great Private Signature.
""",
"no signature" => "",
}
# default signature key
default = "professional"
# prompt
prompt = "Choose signature"
help = "(<tab> for list of choices, empty line for #{default} signature)"
## Globals
# The signature hook is called every time the message has to be generated.
# we'll store our choice in a global hash with the message id as key
# to avoid being asked again and again
$signature = {} unless $signature
## Logic
# ask for signature, if there is none stored for this message_id
signature_key = nil
unless $signature[message_id]
# ensure signature_key is in signatures.keys
until signatures.key? signature_key
# use BufferManager's dialog function (compare lib/sup/buffer.rb)
signature_key = BufferManager.ask_many_with_completions(
:sign, # token for BufferManager's sake.
"#{prompt}: ", # Prompt
signatures.keys) # Array with posible completions
# add help in case the first attempt was not successful
prompt = "#{prompt} #{help}"
# check for empty line and select default signature
if signature_key == ""
signature_key = default
break
end
end
# store chosen signature in global variable for later reuse
$signature[message_id] = signatures[signature_key]
end
# return chosen signature
$signature[message_id]