sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 94b05b8147e4373b6c939aad0f0f2f1c1599baa4
parent b15e43d8bb5d6637d878bf7625d585cbce47b978
Author: William Morgan <wmorgan-sup@masanjin.net>
Date:   Sat, 15 Nov 2008 16:43:42 -0800

Merge commit 'c45207/hook-state-bag' into next

Diffstat:
M doc/Hooks.txt | 41 +++++++++++++++++++++++++++++++++++++++--
M lib/sup/hook.rb | 13 ++++++++++++-
2 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/doc/Hooks.txt b/doc/Hooks.txt
@@ -12,8 +12,28 @@ class or method definitions, just the executable code itself.
 
 Information passes from Sup to the hook code via Ruby variables
 (actually method calls), and from the hook code back to Sup via a
-return value. Each hook description lists the variables and return
-value expected, if any.
+return value. The values of variables persists across calls to the
+same hook, but is NOT available to other hooks. To make the value of a
+variable available to other hooks, use the get and set methods.  Each
+hook description lists the variables and return value expected, if
+any.
+
+The following special functions are available to hooks:
+* say msg
+  Displays the string msg to the user at the bottom of the screen.
+* log msg
+  Adds the string msg to the log, which the user can access via the
+  buffer list.
+* ask_yes_or_no question
+  Prompts the user with the string question for a yes or no
+  response. Returns true if the user answered yes, false otherwise.
+* get key
+  Gets the cross-hook value associated with key (which is typically a
+  string). If there is no value for a given key, nil is returned.
+* set key value
+  Sets the cross-hook value associated with key to value. key is
+  typically a string, while value can be whatever type it needs to be,
+  including nil.
 
 Some example hooks:
 
@@ -36,3 +56,19 @@ mime-decode:
       `/usr/bin/w3m -dump -T #{content_type} '#{filename}'`
     end
   end
+
+startup:
+  ## runs a background task
+  @bgtask_pid = fork
+  if @bgtask_pid
+    set 'bgtask_pid' @bgtask_pid
+    Process.detach(@bgtask_pid) # so we don't have to wait on it when we go to kill it
+  else
+    exec "background-task args 2&>1 >> /tmp/logfile"
+  end
+
+after-poll:
+  ## kills the background task after the first poll
+  @bgtask_pid = get 'bgtask_pid'
+  Process.kill("TERM", @bgtask_pid) unless @bgtask_pid == nil
+  set 'bgtask_pid' nil
+\ No newline at end of file
diff --git a/lib/sup/hook.rb b/lib/sup/hook.rb
@@ -52,6 +52,14 @@ class HookManager
       end
     end
 
+    def get tag
+      HookManager.tags[tag]
+    end
+
+    def set tag, value
+      HookManager.tags[tag] = value
+    end
+
     def __binding 
       binding
     end
@@ -68,12 +76,15 @@ class HookManager
     @hooks = {}
     @descs = {}
     @contexts = {}
-    
+    @tags = {}
+
     Dir.mkdir dir unless File.exists? dir
 
     self.class.i_am_the_instance self
   end
 
+  attr_reader :tags
+
   def run name, locals={}
     hook = hook_for(name) or return
     context = @contexts[hook] ||= HookContext.new(name)