sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/

lib/sup/modes/text_mode.rb (1778B) - raw

      1 module Redwood
      2 
      3 class TextMode < ScrollMode
      4   attr_reader :text
      5   register_keymap do |k|
      6     k.add :save_to_disk, "Save to disk", 's'
      7     k.add :pipe, "Pipe to process", '|'
      8   end
      9 
     10   def initialize text="", filename=nil
     11     @text = text
     12     @filename = filename
     13     update_lines
     14     buffer.mark_dirty if buffer
     15     super()
     16   end
     17 
     18   def save_to_disk
     19     fn = BufferManager.ask_for_filename :filename, "Save to file: ", @filename
     20     save_to_file(fn) { |f| f.puts text } if fn
     21   end
     22 
     23   def pipe
     24     command = BufferManager.ask(:shell, "pipe command: ")
     25     return if command.nil? || command.empty?
     26 
     27     output, success = pipe_to_process(command) do |stream|
     28       @text.each { |l| stream.puts l }
     29     end
     30 
     31     unless success
     32       BufferManager.flash "Invalid command: '#{command}' is not an executable"
     33       return
     34     end
     35 
     36     if output
     37       BufferManager.spawn "Output of '#{command}'", TextMode.new(output.ascii)
     38     else
     39       BufferManager.flash "'#{command}' done!"
     40     end
     41   end
     42 
     43   def text= t
     44     @text = t
     45     update_lines
     46     if buffer
     47       ensure_mode_validity
     48       buffer.mark_dirty
     49     end
     50   end
     51 
     52   def << line
     53     @lines = [0] if @text.empty?
     54     @text << line.fix_encoding!
     55     @lines << @text.length
     56     if buffer
     57       ensure_mode_validity
     58       buffer.mark_dirty
     59     end
     60   end
     61 
     62   def lines
     63     @lines.length - 1
     64   end
     65 
     66   def [] i
     67     return nil unless i < @lines.length
     68     @text[@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)].normalize_whitespace
     69 #    (@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)).inspect
     70   end
     71 
     72 private
     73 
     74   def update_lines
     75     pos = @text.find_all_positions("\n")
     76     pos.push @text.length unless pos.last == @text.length - 1
     77     @lines = [0] + pos.map { |x| x + 1 }
     78   end
     79 end
     80 
     81 end