Class: Hackmac::Graph::Display
- Inherits:
-
Object
- Object
- Hackmac::Graph::Display
- Defined in:
- lib/hackmac/graph/display.rb
Defined Under Namespace
Classes: Cell
Constant Summary collapse
- ANSI =
Term::ANSIColor
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #-(old) ⇒ Object
- #at(y, x) ⇒ Object
- #bottom ⇒ Object
- #centered ⇒ Object
- #clear ⇒ Object
- #color(color) ⇒ Object
- #columns ⇒ Object
- #dimensions ⇒ Object
- #each(&block) ⇒ Object
- #get ⇒ Object
-
#initialize(lines, columns) ⇒ Display
constructor
A new instance of Display.
- #inspect ⇒ Object
- #left ⇒ Object
- #lines ⇒ Object
- #on_color(on_color) ⇒ Object
- #put(char) ⇒ Object
- #reset ⇒ Object
- #right ⇒ Object
- #styled(*s) ⇒ Object
- #to_s ⇒ Object
- #top ⇒ Object
- #write(string) ⇒ Object
- #write_centered(string) ⇒ Object
Constructor Details
#initialize(lines, columns) ⇒ Display
Returns a new instance of Display.
20 21 22 23 24 |
# File 'lib/hackmac/graph/display.rb', line 20 def initialize(lines, columns) @lines_range = 1..lines @columns_range = 1..columns clear end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
80 81 82 |
# File 'lib/hackmac/graph/display.rb', line 80 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
82 83 84 |
# File 'lib/hackmac/graph/display.rb', line 82 def y @y end |
Instance Method Details
#-(old) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/hackmac/graph/display.rb', line 58 def -(old) dimensions != old.dimensions and raise ArgumentError, "old dimensions #{old.dimensions.inspect} don't match #{dimensions.inspect}" result = +'' each.zip(old.each) do |(my, mx, me), (_, _, old)| if me != old result << ANSI.move_to(my, mx) << me.to_s end end result end |
#at(y, x) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/hackmac/graph/display.rb', line 104 def at(y, x) @lines_range.include?(y) or raise ArgumentError, "y #{y} out of lines range #@lines_range" @columns_range.include?(x) or raise ArgumentError, "x #{x} out of columns range #@columns_range" @y, @x = y, x self end |
#bottom ⇒ Object
136 137 138 |
# File 'lib/hackmac/graph/display.rb', line 136 def bottom at(lines, x) end |
#centered ⇒ Object
148 149 150 |
# File 'lib/hackmac/graph/display.rb', line 148 def centered at(lines / 2, columns / 2) end |
#clear ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hackmac/graph/display.rb', line 26 def clear @x = 1 @y = 1 @color = 15 @on_color = 0 @styles = [] @cells = Array.new(lines) { Array.new(columns) { Cell.new(' ', @color, @on_color, @styles) } } reset end |
#color(color) ⇒ Object
160 161 162 163 |
# File 'lib/hackmac/graph/display.rb', line 160 def color(color) @color = attribute!(color) self end |
#columns ⇒ Object
88 89 90 |
# File 'lib/hackmac/graph/display.rb', line 88 def columns @columns_range.end end |
#dimensions ⇒ Object
92 93 94 |
# File 'lib/hackmac/graph/display.rb', line 92 def dimensions [ lines, columns ] end |
#each(&block) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/hackmac/graph/display.rb', line 48 def each(&block) Enumerator.new do |enum| @lines_range.each do |y| @columns_range.each do |x| enum.yield y, x, @cells[y - 1][x - 1] end end end.each(&block) end |
#get ⇒ Object
156 157 158 |
# File 'lib/hackmac/graph/display.rb', line 156 def get @cells[@y - 1][@x - 1] end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/hackmac/graph/display.rb', line 76 def inspect "#<#{self.class}: #{columns}×#{lines}>" end |
#left ⇒ Object
140 141 142 |
# File 'lib/hackmac/graph/display.rb', line 140 def left at(y, 1) end |
#lines ⇒ Object
84 85 86 |
# File 'lib/hackmac/graph/display.rb', line 84 def lines @lines_range.end end |
#on_color(on_color) ⇒ Object
165 166 167 168 |
# File 'lib/hackmac/graph/display.rb', line 165 def on_color(on_color) @on_color = attribute!(on_color) self end |
#put(char) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/hackmac/graph/display.rb', line 113 def put(char) char.size == 1 or raise ArgumentError, "#{char} is not single character" @cells[@y - 1][@x - 1] = Cell.new(char, @color, @on_color, @styles) self end |
#reset ⇒ Object
41 42 43 44 45 46 |
# File 'lib/hackmac/graph/display.rb', line 41 def reset @color = 15 @on_color = 0 @styles = [] self end |
#right ⇒ Object
144 145 146 |
# File 'lib/hackmac/graph/display.rb', line 144 def right at(y, columns) end |
#styled(*s) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/hackmac/graph/display.rb', line 96 def styled(*s) if nope = s.find { !style?(_1) } raise ArgumentError, "#{nope} is not a style" end @styles = s self end |
#to_s ⇒ Object
70 71 72 73 74 |
# File 'lib/hackmac/graph/display.rb', line 70 def to_s each.inject(ANSI.clear_screen) do |s, (y, x, c)| s << ANSI.move_to(y, x) << c.to_s end end |
#top ⇒ Object
132 133 134 |
# File 'lib/hackmac/graph/display.rb', line 132 def top at(1, x) end |
#write(string) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/hackmac/graph/display.rb', line 120 def write(string) string.each_char do |char| put(char) if x < columns - 1 at(y, x + 1) else break end end self end |
#write_centered(string) ⇒ Object
152 153 154 |
# File 'lib/hackmac/graph/display.rb', line 152 def write_centered(string) at(@y, (columns - string.size) / 2).write(string) end |