Class: Amun::Windows::MiniBufferWindow

Inherits:
Base
  • Object
show all
Defined in:
lib/amun/windows/mini_buffer_window.rb

Overview

a minibuffer that when attached will replace the frame echo area and display one line with a label (buffer name) and the value typed by the user (buffer content) it will fire 2 events, done and cancel, you can listen on them to do stuff with the input data, also after both events the buffer will be cleared to allow you to reattach the same window again and reuse it, also it allow passing a block to the initializer to execute it when the user press enter (done event) so you can have a fast usage as such

MiniBufferWindow.new("Are you sure?[Y/N]", "Y") do |window|
    exit if window.buffer.to_s.downcase == 'y'
end.attach(Amun::Application.frame)

that will create a minibuffer and attach it to current active frame also will have a label asking the user “Are you sure?” and a default value “Y” so user can just press enter, or clear it and press any other character when user press enter the block will be executed with the window itself as a parameter so the block will exit amun if the answer to the question is “y”

Instance Attribute Summary collapse

Attributes inherited from Base

#size

Attributes inherited from Object

#events

Instance Method Summary collapse

Constructor Details

#initialize(name, default_value = '', &block) ⇒ MiniBufferWindow

Returns a new instance of MiniBufferWindow.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/amun/windows/mini_buffer_window.rb', line 28

def initialize(name, default_value = '', &block)
  super(default_size)

  @buffer = Buffer.new(name)
  @buffer << default_value
  @buffer.point = default_value.length
  @done_block = block if block_given?
  @text_renderer = TextRenderer.new(text_renderer_size)

  bind "\e", self, :cancel
  bind "\C-g", self, :cancel
  bind "\n", self, :done
  bind "done", self, :exec_done_block
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



26
27
28
# File 'lib/amun/windows/mini_buffer_window.rb', line 26

def buffer
  @buffer
end

Instance Method Details

#attach(frame) ⇒ Object

attach the mini buffer to a frame of your choice, that will make it replace the echo are in this frame



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/amun/windows/mini_buffer_window.rb', line 45

def attach(frame)
  detach if attached?
  self.size = Rect.new(
    top: frame.top + frame.height - 1,
    left: frame.left,
    width: frame.width,
    height: 1
  )
  @frame = frame
  @frame.mini_buffer = self
end

#attached?Boolean

is the mini buffer currently attached to any frame?

Returns:

  • (Boolean)


64
65
66
# File 'lib/amun/windows/mini_buffer_window.rb', line 64

def attached?
  @frame && @frame.mini_buffer == self
end

#cancelObject



83
84
85
86
87
# File 'lib/amun/windows/mini_buffer_window.rb', line 83

def cancel(*)
  detach
  trigger("cancel")
  buffer.clear
end

#detachObject

deatach the mini buffer from its frame



58
59
60
61
# File 'lib/amun/windows/mini_buffer_window.rb', line 58

def detach
  @frame.mini_buffer = nil
  @frame = nil
end

#doneObject



89
90
91
92
93
# File 'lib/amun/windows/mini_buffer_window.rb', line 89

def done(*)
  detach
  trigger("done")
  buffer.clear
end

#exec_done_blockObject



95
96
97
98
# File 'lib/amun/windows/mini_buffer_window.rb', line 95

def exec_done_block(*)
  return unless @done_block
  @done_block.call(self)
end

#renderObject



68
69
70
71
72
73
# File 'lib/amun/windows/mini_buffer_window.rb', line 68

def render
  curses_window.erase
  curses_window << buffer.name
  curses_window.refresh
  @text_renderer.render(buffer)
end

#trigger(event) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/amun/windows/mini_buffer_window.rb', line 75

def trigger(event)
  EventManager.join(
    event,
    events,
    buffer
  )
end