Module: Swarm::Console
Defined Under Namespace
Modules: Color, NormalKey, VIKey Classes: PopUp
Constant Summary
Constants included from Color
Color::BLACK, Color::BLUE, Color::GREEN, Color::GREY, Color::PURPLE, Color::RED, Color::SILVER, Color::WHITE, Color::YELLOW
Instance Attribute Summary collapse
-
#key_info ⇒ Object
readonly
Returns the value of attribute key_info.
Instance Method Summary collapse
-
#close ⇒ Object
Turn line buffering on and close the
Curses
session. - #draw(tile) ⇒ Object
-
#height ⇒ Integer
height
of console. - #init_colors ⇒ Object
- #init_keys ⇒ Object
- #keypress {|command| ... } ⇒ Object
-
#open ⇒ Object
Initialize the
Curses
environment, configure it and then clean up when the caller is done. -
#update(tiles) ⇒ Object
Redraw all
tiles
given and refresh the console. -
#width ⇒ Integer
width
of console. - #wipe ⇒ Object
Instance Attribute Details
#key_info ⇒ Object (readonly)
Returns the value of attribute key_info
62 63 64 |
# File 'lib/swarm/console.rb', line 62 def key_info @key_info end |
Instance Method Details
#close ⇒ Object
Turn line buffering on and close the Curses
session.
141 142 143 144 145 146 |
# File 'lib/swarm/console.rb', line 141 def close return if closed? nocbreak close_screen end |
#draw(tile) ⇒ Object
129 130 131 132 133 |
# File 'lib/swarm/console.rb', line 129 def draw(tile) setpos *tile.location attron(color_pair tile.color) { addstr tile.icon } end |
#height ⇒ Integer
Returns height
of console
115 116 117 |
# File 'lib/swarm/console.rb', line 115 def height @height ||= lines end |
#init_colors ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/swarm/console.rb', line 85 def init_colors start_color use_default_colors #id #fg #bg init_pair BLACK, BLACK, BLACK init_pair SILVER, SILVER, SILVER init_pair WHITE, WHITE, WHITE init_pair GREY, GREY, GREY init_pair RED, RED, BLACK init_pair PURPLE, PURPLE, BLACK init_pair GREEN, GREEN, BLACK init_pair BLUE, BLUE, BLACK init_pair YELLOW, YELLOW, BLACK end |
#init_keys ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/swarm/console.rb', line 66 def init_keys nonl noecho # don't echo keypresses stdscr.nodelay = -1 # don't wait for input, just listen for it stdscr.keypad true @key_info = {pause: '<SPACEBAR>', quit: ?q} if ENV.fetch('SWARM', 'VIM_MODE') == 'easy' extend NormalKey @key_info.merge! north: '<UP>', south: '<DOWN>', west: '<LEFT>', east: '<RIGHT>' else extend VIKey @key_info.merge! north: ?k, south: ?j, west: ?h, east: ?l end end |
#keypress {|command| ... } ⇒ Object
102 103 104 |
# File 'lib/swarm/console.rb', line 102 def keypress command = getch and yield(command) end |
#open ⇒ Object
Initialize the Curses
environment, configure it and then clean
up when the caller is done. 9 different bg/fg pairs are initialized for the
colored Tile
squares (0-8): (black, red, green, yellow, blue,
magenta, cyan, white, grey)
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/swarm/console.rb', line 152 def open init_screen init_colors init_keys curs_set 0 # hide the cursor cbreak # turn off line buffering yield close end |
#update(tiles) ⇒ Object
Redraw all tiles
given and refresh the console
121 122 123 124 |
# File 'lib/swarm/console.rb', line 121 def update(tiles) tiles.each &method(:draw) refresh end |
#width ⇒ Integer
Returns width
of console
110 111 112 |
# File 'lib/swarm/console.rb', line 110 def width @width ||= cols end |
#wipe ⇒ Object
135 136 137 138 |
# File 'lib/swarm/console.rb', line 135 def wipe clear refresh end |