]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/text-mode.rb
Merge branch 'hook-local-vars'
[sup] / lib / sup / modes / text-mode.rb
index be3bc1107952bae9e8c1ab9340bdbce1c2f57a00..7c9e7d87c0c1c6da3bf4e0b3634138ce6880aedd 100644 (file)
@@ -2,14 +2,39 @@ module Redwood
 
 class TextMode < ScrollMode
   attr_reader :text
+  register_keymap do |k|
+    k.add :save_to_disk, "Save to disk", 's'
+    k.add :pipe, "Pipe to process", '|'
+  end
 
-  def initialize text=""
-    @text = text.normalize_whitespace
+  def initialize text="", filename=nil
+    @text = text
+    @filename = filename
     update_lines
     buffer.mark_dirty if buffer
     super()
   end
 
+  def save_to_disk
+    fn = BufferManager.ask_for_filename :filename, "Save to file: ", @filename
+    save_to_file(fn) { |f| f.puts text } if fn
+  end
+
+  def pipe
+    command = BufferManager.ask(:shell, "pipe command: ")
+    return if command.nil? || command.empty?
+
+    output = pipe_to_process(command) do |stream|
+      @text.each { |l| stream.puts l }
+    end
+
+    if output
+      BufferManager.spawn "Output of '#{command}'", TextMode.new(output)
+    else
+      BufferManager.flash "'#{command}' done!"
+    end
+  end
+
   def text= t
     @text = t
     update_lines
@@ -25,7 +50,7 @@ class TextMode < ScrollMode
     @lines << @text.length
     if buffer
       ensure_mode_validity
-      buffer.mark_dirty 
+      buffer.mark_dirty
     end
   end
 
@@ -35,7 +60,7 @@ class TextMode < ScrollMode
 
   def [] i
     return nil unless i < @lines.length
-    @text[@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)]
+    @text[@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)].normalize_whitespace
 #    (@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)).inspect
   end