Class: Redwood::Mode

Inherits:
Object show all
Defined in:
lib/sup/mode.rb

Direct Known Subclasses

ScrollMode

Constant Summary

@@keymaps =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMode

Returns a new instance of Mode



12
13
14
# File 'lib/sup/mode.rb', line 12

def initialize
  @buffer = nil
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer



4
5
6
# File 'lib/sup/mode.rb', line 4

def buffer
  @buffer
end

Class Method Details

.load_all_modes(dir) ⇒ Object



19
20
21
22
23
24
# File 'lib/sup/mode.rb', line 19

def self.load_all_modes dir
  Dir[File.join(dir, "*.rb")].each do |f|
    $stderr.puts "## loading mode #{f}"
    require f
  end
end

.make_name(s) ⇒ Object



16
# File 'lib/sup/mode.rb', line 16

def self.make_name s; s.gsub(/.*::/, "").camel_to_hyphy; end

.register_keymap(keymap = nil, &b) ⇒ Object



7
8
9
10
# File 'lib/sup/mode.rb', line 7

def self.register_keymap keymap=nil, &b
  keymap = Keymap.new(&b) if keymap.nil?
  @@keymaps[self] = keymap
end

Instance Method Details

#blurObject



29
# File 'lib/sup/mode.rb', line 29

def blur; end

#cancel_search!Object



30
# File 'lib/sup/mode.rb', line 30

def cancel_search!; end

#cleanupObject



34
35
36
# File 'lib/sup/mode.rb', line 34

def cleanup
  @buffer = nil
end

#drawObject



27
# File 'lib/sup/mode.rb', line 27

def draw; end

#focusObject



28
# File 'lib/sup/mode.rb', line 28

def focus; end

#handle_input(c) ⇒ Object



47
48
49
50
51
# File 'lib/sup/mode.rb', line 47

def handle_input c
  action = resolve_input(c) or return false
  send action
  true
end

#help_textObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sup/mode.rb', line 53

def help_text
  used_keys = {}
  ancestors.map do |klass|
    km = @@keymaps[klass] or next
    title = "Keybindings from #{Mode.make_name klass.name}"
    s = <<EOS
#{title}
#{'-' * title.length}

#{km.help_text used_keys}
EOS
    begin
      used_keys.merge! km.keysyms.to_boolean_h
    rescue ArgumentError
      raise km.keysyms.inspect
    end
    s
  end.compact.join "\n"
end

#in_search?Boolean

Returns:

  • (Boolean)


31
# File 'lib/sup/mode.rb', line 31

def in_search?; false end

#killable?Boolean

Returns:

  • (Boolean)


26
# File 'lib/sup/mode.rb', line 26

def killable?; true; end

#nameObject



17
# File 'lib/sup/mode.rb', line 17

def name; Mode.make_name self.class.name; end

#pipe_to_process(command) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sup/mode.rb', line 87

def pipe_to_process command
  Open3.popen3(command) do |input, output, error|
    err, data, * = IO.select [error], [input], nil

    unless err.empty?
      message = err.first.read
      if message =~ /^\s*$/
        Redwood::log "error running #{command} (but no error message)"
        BufferManager.flash "Error running #{command}!"
      else
        Redwood::log "error running #{command}: #{message}"
        BufferManager.flash "Error: #{message}"
      end
      return
    end

    data = data.first
    data.sync = false # buffer input

    yield data
    data.close # output will block unless input is closed

    ## BUG?: shows errors or output but not both....
    data, * = IO.select [output, error], nil, nil
    data = data.first

    if data.eof
      BufferManager.flash "'#{command}' done!"
      nil
    else
      data.read
    end
  end
end

#resize(rows, cols) ⇒ Object



33
# File 'lib/sup/mode.rb', line 33

def resize rows, cols; end

#resolve_input(c) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/sup/mode.rb', line 38

def resolve_input c
  ancestors.each do |klass| # try all keymaps in order of ancestry
    next unless @@keymaps.member?(klass)
    action = BufferManager.resolve_input_with_keymap c, @@keymaps[klass]
    return action if action
  end
  nil
end

#save_to_file(fn) ⇒ Object

# helper functions



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sup/mode.rb', line 75

def save_to_file fn
  if File.exists? fn
    return unless BufferManager.ask_yes_or_no "File exists. Overwrite?"
  end
  begin
    File.open(fn, "w") { |f| yield f }
    BufferManager.flash "Successfully wrote #{fn}."
  rescue SystemCallError, IOError => e
    BufferManager.flash "Error writing to file: #{e.message}"
  end
end

#statusObject



32
# File 'lib/sup/mode.rb', line 32

def status; ""; end