Subject: [sup-talk] [PATCH 1/5] console mode

From: [email-masked] (Rich Lane)
Date: Sun, 16 Aug 2009 23:39:11 -0700

---
 bin/sup                       |    4 +++
 lib/sup.rb                    |    1 +
 lib/sup/modes/console-mode.rb |   42 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 0 deletions(-)
 create mode 100644 lib/sup/modes/console-mode.rb

diff --git a/bin/sup b/bin/sup
index a9f0b95..1dc322a 100755
--- a/bin/sup
+++ b/bin/sup
@@ -78,6 +78,7 @@ global_keymap = Keymap.new do |k|
   k.add :compose, "Compose new message", 'm', 'c'
   k.add :nothing, "Do nothing", :ctrl_g
   k.add :recall_draft, "Edit most recent draft message", 'R'
+  k.add :show_console, "Show the Console buffer", '~'
 end
 
 ## the following magic enables wide characters when used with a ruby
@@ -286,6 +287,9 @@ begin
         b, new = BufferManager.spawn_unless_exists("All drafts") { LabelSearchResultsMode.new [:draft] }
         b.mode.load_threads :num => b.content_height if new
       end
+    when :show_console
+      b, new = bm.spawn_unless_exists("Console", :system => true) { ConsoleMode.new }
+      b.mode.run
     when :nothing, InputSequenceAborted
     when :redraw
       bm.completely_redraw_screen
diff --git a/lib/sup.rb b/lib/sup.rb
index cfa93fc..9e0c33a 100644
--- a/lib/sup.rb
+++ b/lib/sup.rb
@@ -295,6 +295,7 @@ require "sup/modes/buffer-list-mode"
 require "sup/modes/poll-mode"
 require "sup/modes/file-browser-mode"
 require "sup/modes/completion-mode"
+require "sup/modes/console-mode"
 require "sup/sent"
 
 $:.each do |base|
diff --git a/lib/sup/modes/console-mode.rb b/lib/sup/modes/console-mode.rb
new file mode 100644
index 0000000..d06d37b
--- /dev/null
+++ b/lib/sup/modes/console-mode.rb
@@ -0,0 +1,42 @@
+require 'pp'
+
+module Redwood
+
+class Console
+  def initialize mode
+    @mode = mode
+  end
+end
+
+class ConsoleMode < LogMode
+  def initialize
+    super
+    @binding = Console.new(self).instance_eval { binding }
+  end
+
+  def execute cmd
+    begin
+      self << ">> #{cmd}\n"
+      ret = eval cmd, @binding
+      self << "=> #{ret.pretty_inspect}\n"
+    rescue Exception
+      self << "#{$!.class}: #{$!.message}\n"
+      clean_backtrace = []
+      $!.backtrace.each { |l| break if l =~ /console-mode/; clean_backtrace << l }
+      clean_backtrace.each { |l| self << "#{l}\n" }
+    end
+  end
+
+  def prompt
+    BufferManager.ask :console, "eval: "
+  end
+
+  def run
+    while true
+      cmd = prompt or return
+      execute cmd
+    end
+  end
+end
+
+end
-- 
1.6.4

---

From: [email-masked] (Rich Lane)
Date: Sun, 16 Aug 2009 23:39:13 -0700

---
 lib/sup/modes/console-mode.rb |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/lib/sup/modes/console-mode.rb b/lib/sup/modes/console-mode.rb
index a91bbbf..c794a4c 100644
--- a/lib/sup/modes/console-mode.rb
+++ b/lib/sup/modes/console-mode.rb
@@ -18,6 +18,9 @@ class Console
   def remove_labels(query, *labels)
     query(query).each { |m| m.labels -= labels; m.save Index }
   end
+
+  def xapian; Index.instance.instance_variable_get :@xapian; end
+  def ferret; Index.instance.instance_variable_get :@index; end
 end
 
 class ConsoleMode < LogMode
-- 
1.6.4

---

From: [email-masked] (Rich Lane)
Date: Sun, 16 Aug 2009 23:39:12 -0700

---
 lib/sup/modes/console-mode.rb |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/lib/sup/modes/console-mode.rb b/lib/sup/modes/console-mode.rb
index d06d37b..a91bbbf 100644
--- a/lib/sup/modes/console-mode.rb
+++ b/lib/sup/modes/console-mode.rb
@@ -6,6 +6,18 @@ class Console
   def initialize mode
     @mode = mode
   end
+
+  def query(query)
+    Enumerable::Enumerator.new(Index, :each_message, Index.parse_query(query))
+  end
+
+  def add_labels(query, *labels)
+    query(query).each { |m| m.labels += labels; m.save Index }
+  end
+
+  def remove_labels(query, *labels)
+    query(query).each { |m| m.labels -= labels; m.save Index }
+  end
 end

---

From: [email-masked] (Rich Lane)
Date: Sun, 16 Aug 2009 23:39:14 -0700

---
 lib/sup/modes/console-mode.rb |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/lib/sup/modes/console-mode.rb b/lib/sup/modes/console-mode.rb
index c794a4c..c344fa6 100644
--- a/lib/sup/modes/console-mode.rb
+++ b/lib/sup/modes/console-mode.rb
@@ -21,6 +21,36 @@ class Console
 
   def xapian; Index.instance.instance_variable_get :@xapian; end
   def ferret; Index.instance.instance_variable_get :@index; end
+
+  ## files that won't cause problems when reloaded
+  ## TODO expand this list / convert to blacklist
+  RELOAD_WHITELIST = %w(sup/xapian_index.rb sup/modes/console-mode.rb)
+
+  def reload
+    old_verbose = $VERBOSE
+    $VERBOSE = nil
+    old_features = $".dup
+    begin
+      fs = $".grep(/^sup\//)
+      fs.reject! { |f| not RELOAD_WHITELIST.member? f }
+      fs.each { |f| $".delete f }
+      fs.each do |f|
+        @mode << "reloading #{f}\n"
+        begin
+          require f
+        rescue LoadError => e
+          raise unless e.message =~ /no such file to load/
+        end
+      end
+    rescue Exception
+      $".clear
+      $".concat old_features
+      raise
+    ensure
+      $VERBOSE = old_verbose
+    end
+    true
+  end
 end

---

From: [email-masked] (Rich Lane)
Date: Sun, 16 Aug 2009 23:39:15 -0700

---
 lib/sup/hook.rb               |    2 ++
 lib/sup/modes/console-mode.rb |    5 +++++
 2 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/lib/sup/hook.rb b/lib/sup/hook.rb
index 33a97b2..099158d 100644
--- a/lib/sup/hook.rb
+++ b/lib/sup/hook.rb
@@ -130,6 +130,8 @@ EOS
 
   def enabled? name; !hook_for(name).nil? end
 
+  def clear; @hooks.clear; end
+
 private
 
   def hook_for name
diff --git a/lib/sup/modes/console-mode.rb b/lib/sup/modes/console-mode.rb
index c344fa6..372a466 100644
--- a/lib/sup/modes/console-mode.rb
+++ b/lib/sup/modes/console-mode.rb
@@ -51,6 +51,11 @@ class Console
     end
     true
   end
+
+  def clear_hooks
+    HookManager.clear
+    nil
+  end
 end

---

From: [email-masked] (William Morgan)
Date: Tue, 18 Aug 2009 11:37:38 -0700

Cool idea. Branch console-mode, merged into next.
-- 
William <wmorgan-sup at masanjin.net>