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 ],
  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


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

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



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

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



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

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

#before_leftObject



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

def before_left
  @cursor.x > 0
end

#before_rightObject



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

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

#before_upObject



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

def before_up
  cursor.y > 0
end

#change_focus_lineObject



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

def change_focus_line
  normalize_line
  yield if block_given?
  enhansive_line
end

#collectionObject



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

def collection; [] end

#downObject



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

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



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

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

#draw_at!(y) ⇒ Object



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

def draw_at!(y)
  draw_at y
  refresh
end

#enhansive_lineObject



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

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:



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

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:



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

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

#leftObject



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

def left
  return unless before_left
  cursor.left
  refresh
end

#normalize_lineObject



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

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

#on_input(ch) ⇒ Object



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

def on_input(ch) end

#refreshObject



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

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

#rightObject



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

def right
  return unless before_right
  cursor.right
  refresh
end

#upObject



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

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

#updateObject



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

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

#view_collectionObject



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

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