Class: Redwood::Mode

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

Direct Known Subclasses

ScrollMode

Constant Summary collapse

@@keymaps =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMode

Returns a new instance of Mode.

[View source]

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

def initialize
  @buffer = nil
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.


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

def buffer
  @buffer
end

Class Method Details

.keymapObject

[View source]

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

def self.keymap
  @@keymaps[self] || register_keymap
end

.keymapsObject

[View source]

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

def self.keymaps
  @@keymaps
end

.load_all_modes(dir) ⇒ Object

[View source]

28
29
30
31
32
33
# File 'lib/sup/mode.rb', line 28

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

[View source]

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

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

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

[View source]

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

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

Instance Method Details

#blurObject

[View source]

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

def blur; end

#cancel_search!Object

[View source]

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

def cancel_search!; end

#cleanupObject

[View source]

44
45
46
# File 'lib/sup/mode.rb', line 44

def cleanup
  @buffer = nil
end

#drawObject

[View source]

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

def draw; end

#focusObject

[View source]

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

def focus; end

#handle_input(c) ⇒ Object

[View source]

57
58
59
60
61
# File 'lib/sup/mode.rb', line 57

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

#help_textObject

[View source]

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sup/mode.rb', line 63

def help_text
  used_keys = {}
  self.class.ancestors.map do |klass|
    km = @@keymaps[klass] or next
    title = "Keybindings from #{Mode.make_name klass.name}"
    s = <<EOS
#{title}
#{'-' * title.display_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)
[View source]

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

def in_search?; false end

#killable?Boolean

Returns:

  • (Boolean)
[View source]

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

def killable?; true; end

#nameObject

[View source]

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

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

#pipe_to_process(command) ⇒ Object

[View source]

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sup/mode.rb', line 105

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

      unless err.empty?
        message = err.first.read
        if message =~ /^\s*$/
          warn "error running #{command} (but no error message)"
          BufferManager.flash "Error running #{command}!"
        else
          warn "error running #{command}: #{message}"
          BufferManager.flash "Error: #{message}"
        end
        return nil, false
      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!"
        return nil, true
      else
        return data.read, true
      end
    end
  rescue Errno::ENOENT
    # If the command is invalid
    return nil, false
  end
end

#resize(rows, cols) ⇒ Object

[View source]

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

def resize rows, cols; end

#resolve_input(c) ⇒ Object

[View source]

48
49
50
51
52
53
54
55
# File 'lib/sup/mode.rb', line 48

def resolve_input c
  self.class.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, talk = true) ⇒ Object

helper functions

[View source]

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sup/mode.rb', line 85

def save_to_file fn, talk=true
  FileUtils.mkdir_p File.dirname(fn)
  if File.exist? fn
    unless BufferManager.ask_yes_or_no "File \"#{fn}\" exists. Overwrite?"
      info "Not overwriting #{fn}"
      return
    end
  end
  begin
    File.open(fn, "w") { |f| yield f }
    BufferManager.flash "Successfully wrote #{fn}." if talk
    true
  rescue SystemCallError, IOError => e
    m = "Error writing file: #{e.message}"
    info m
    BufferManager.flash m
    false
  end
end

#statusObject

[View source]

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

def status; ""; end

#unsaved?Boolean

Returns:

  • (Boolean)
[View source]

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

def unsaved?; false end