Class: RubyAnything::BaseWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-anything/base_window.rb

Overview

prefix ‘c_` is curses

Direct Known Subclasses

ItemsWindow, PromptWindow, TextWindow, Windows

Constant Summary collapse

KEYS =

Public: Hash has name as key and has array of keycode as value

{
  up: [ Curses::Key::UP, 16 ],
  down: [ Curses::Key::DOWN, 14 ],
  left: [ Curses::Key::LEFT, 2 ],
  right: [ Curses::Key::RIGHT, 6 ],
  enter: [ 10 ],
  backspace: [ 127 ],
  interrupt: [ 3, 4 ]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, opt) ⇒ BaseWindow

Public: initialize BaseWindow

parent - The Curses::Window or RubyAnything::Window is parent window for self opt - The Hash is options for drawing self

:h height
:w width
:y y-axis
:x x-axis


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-anything/base_window.rb', line 27

def initialize(parent, opt)
  @parent = parent
  @c_window = @parent.subwin(
    opt[:h] || @parent.maxy,
    opt[:w] || @parent.maxx,
    opt[:y] || 0,
    opt[:x] || 0
  )
  @cursor = Cursor.new
  @top = 0
  update
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

delegate to instance of Curses::Window



146
147
148
149
150
151
152
# File 'lib/ruby-anything/base_window.rb', line 146

def method_missing(name, *args)
  if @c_window.respond_to? name.to_s
    @c_window.send name.to_s, *args
  else
    super
  end
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



7
8
9
# File 'lib/ruby-anything/base_window.rb', line 7

def cursor
  @cursor
end

#topObject

Returns the value of attribute top.



6
7
8
# File 'lib/ruby-anything/base_window.rb', line 6

def top
  @top
end

Instance Method Details

#before_downObject



74
75
76
# File 'lib/ruby-anything/base_window.rb', line 74

def before_down
  cursor.y + 1 < view_collection.size
end

#before_leftObject



66
67
68
# File 'lib/ruby-anything/base_window.rb', line 66

def before_left
  @cursor.x > 0
end

#before_rightObject



70
71
72
# File 'lib/ruby-anything/base_window.rb', line 70

def before_right
  @cursor.x < view_collection[@cursor.y].size
end

#before_upObject



78
79
80
# File 'lib/ruby-anything/base_window.rb', line 78

def before_up
  cursor.y > 0
end

#change_focus_lineObject



110
111
112
113
114
# File 'lib/ruby-anything/base_window.rb', line 110

def change_focus_line
  normalize_line
  yield if block_given?
  enhansive_line
end

#collectionObject



40
# File 'lib/ruby-anything/base_window.rb', line 40

def collection; [] end

#downObject



82
83
84
85
86
87
88
89
90
# File 'lib/ruby-anything/base_window.rb', line 82

def down
  unless before_down
    @top += 1 if collection.size > @top + maxy
    update
  else
    change_focus_line { cursor.down }
    refresh
  end
end

#draw_at(y) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/ruby-anything/base_window.rb', line 53

def draw_at(y)
  setpos(y, 0)
  clrtoeol
  if (line = view_collection[y])
    addstr line[0..(maxx - 1)]
  end
end

#draw_at!(y) ⇒ Object



61
62
63
64
# File 'lib/ruby-anything/base_window.rb', line 61

def draw_at!(y)
  draw_at y
  refresh
end

#enhansive_lineObject



121
122
123
124
125
126
# File 'lib/ruby-anything/base_window.rb', line 121

def enhansive_line
  in_color(RubyAnything::ENHANSIVE_COLOR) {
    in_pos(cursor.y, 0) { draw_at(cursor.y) }
    refresh
  }
end

#in_color(color) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



134
135
136
137
138
# File 'lib/ruby-anything/base_window.rb', line 134

def in_color(color)
  attron(Curses.color_pair(color))
  yield self if block_given?
  attroff(Curses::A_COLOR)
end

#in_pos(y, x) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



128
129
130
131
132
# File 'lib/ruby-anything/base_window.rb', line 128

def in_pos(y, x)
  setpos(y, x)
  yield self if block_given?
  setpos(y, x)
end

#leftObject



98
99
100
101
102
# File 'lib/ruby-anything/base_window.rb', line 98

def left
  return unless before_left
  cursor.left
  refresh
end

#normalize_lineObject



116
117
118
119
# File 'lib/ruby-anything/base_window.rb', line 116

def normalize_line
  in_pos(cursor.y, 0) { draw_at(cursor.y) }
  refresh
end

#on_input(ch) ⇒ Object



51
# File 'lib/ruby-anything/base_window.rb', line 51

def on_input(ch) end

#refreshObject



140
141
142
143
# File 'lib/ruby-anything/base_window.rb', line 140

def refresh
  setpos cursor.y, cursor.x
  @c_window.refresh
end

#rightObject



104
105
106
107
108
# File 'lib/ruby-anything/base_window.rb', line 104

def right
  return unless before_right
  cursor.right
  refresh
end

#upObject



92
93
94
95
96
# File 'lib/ruby-anything/base_window.rb', line 92

def up
  return unless before_up
  change_focus_line { cursor.up }
  refresh
end

#updateObject



43
44
45
46
47
48
49
# File 'lib/ruby-anything/base_window.rb', line 43

def update
  in_pos(0, 0) {
    view_collection.each_with_index do |item, index|
      draw_at index
    end
  }
end

#view_collectionObject



41
# File 'lib/ruby-anything/base_window.rb', line 41

def view_collection; collection[@top..@top + maxy - 1] || [] end