Class: Game2048::Terminal
- Inherits:
-
Object
- Object
- Game2048::Terminal
- Defined in:
- lib/game_2048/terminal.rb
Overview
VT100 terminal
Constant Summary collapse
- CSI =
"\e["- CUU =
'A'- CUD =
'B'- CUF =
'C'- CUB =
'D'- CUP =
'H'- ED =
'J'- SGR =
'm'- CPR =
'6n'- CPA =
'R'- HIDE_CURSOR =
'?25l'- SHOW_CURSOR =
'?25h'- ALT_SCREEN_ON =
'?1049h'- ALT_SCREEN_OFF =
'?1049l'- RESET =
0- BOLD =
1- DIM =
2- BLINK =
5- REVERSE =
7- UNDERSCORE =
4- COLOR_FG =
30- COLOR_BG =
40- COLORS =
{ black: 0, red: 1, green: 2, yellow: 3, blue: 4, magenta: 5, cyan: 6, white: 7 }.freeze
- KEY_MAP =
{ CUU => :up, CUD => :down, CUF => :right, CUB => :left }.freeze
- CHARS_MAP =
{ "\x03" => :ctrl_c, "\x0c" => :ctrl_l, "\x1a" => :ctrl_z }.freeze
Instance Method Summary collapse
- #alt_screen_off ⇒ Object
- #alt_screen_on ⇒ Object
- #bg_color(color) ⇒ Object
- #blink ⇒ Object
- #bold ⇒ Object
- #cooked_mode ⇒ Object
- #dim ⇒ Object
- #display_size ⇒ Object
- #erase_display ⇒ Object
- #fg_color(color) ⇒ Object
- #hide_cursor ⇒ Object
-
#initialize(input: $stdin, output: $stdout) ⇒ Terminal
constructor
A new instance of Terminal.
- #move_home ⇒ Object
- #move_to(x, y) ⇒ Object
- #raw_mode ⇒ Object
- #read ⇒ Object
- #reset ⇒ Object
- #reverse ⇒ Object
- #show_cursor ⇒ Object
- #underscore ⇒ Object
- #write(text = '') ⇒ Object
Constructor Details
#initialize(input: $stdin, output: $stdout) ⇒ Terminal
Returns a new instance of Terminal.
58 59 60 61 62 |
# File 'lib/game_2048/terminal.rb', line 58 def initialize(input: $stdin, output: $stdout) @input = input @output = output @sgr = [] end |
Instance Method Details
#alt_screen_off ⇒ Object
123 124 125 |
# File 'lib/game_2048/terminal.rb', line 123 def alt_screen_off @output.write("#{CSI}#{ALT_SCREEN_OFF}") end |
#alt_screen_on ⇒ Object
119 120 121 |
# File 'lib/game_2048/terminal.rb', line 119 def alt_screen_on @output.write("#{CSI}#{ALT_SCREEN_ON}") end |
#bg_color(color) ⇒ Object
81 82 83 84 |
# File 'lib/game_2048/terminal.rb', line 81 def bg_color(color) color_exist?(color) @sgr << COLORS[color] + COLOR_BG end |
#blink ⇒ Object
98 99 100 |
# File 'lib/game_2048/terminal.rb', line 98 def blink @sgr << BLINK end |
#bold ⇒ Object
86 87 88 |
# File 'lib/game_2048/terminal.rb', line 86 def bold @sgr << BOLD end |
#cooked_mode ⇒ Object
171 172 173 |
# File 'lib/game_2048/terminal.rb', line 171 def cooked_mode @input&.cooked! end |
#dim ⇒ Object
94 95 96 |
# File 'lib/game_2048/terminal.rb', line 94 def dim @sgr << DIM end |
#display_size ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/game_2048/terminal.rb', line 127 def display_size move_to(999, 999) @output.write("#{CSI}#{CPR}") data = String.new data << @input.readchar data << @input.readchar return if data != CSI data.clear while (char = @input.readchar) != CPA data << char end data.split(';').map(&:to_i) end |
#erase_display ⇒ Object
72 73 74 |
# File 'lib/game_2048/terminal.rb', line 72 def erase_display @output.write("#{CSI}2#{ED}") end |
#fg_color(color) ⇒ Object
76 77 78 79 |
# File 'lib/game_2048/terminal.rb', line 76 def fg_color(color) color_exist?(color) @sgr << COLORS[color] + COLOR_FG end |
#hide_cursor ⇒ Object
111 112 113 |
# File 'lib/game_2048/terminal.rb', line 111 def hide_cursor @output.write("#{CSI}#{HIDE_CURSOR}") end |
#move_home ⇒ Object
68 69 70 |
# File 'lib/game_2048/terminal.rb', line 68 def move_home move_to(1, 1) end |
#move_to(x, y) ⇒ Object
64 65 66 |
# File 'lib/game_2048/terminal.rb', line 64 def move_to(x, y) @output.write("#{CSI}#{y};#{x}#{CUP}") end |
#raw_mode ⇒ Object
167 168 169 |
# File 'lib/game_2048/terminal.rb', line 167 def raw_mode @input&.raw! end |
#read ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/game_2048/terminal.rb', line 148 def read data = @input.readchar if data == "\e" data << @input.readchar if data == CSI data.clear loop do char = @input.readchar data << char break unless char.ord.between?(0x30, 0x39) || char == ';' end return KEY_MAP.fetch(data, :unknown) end end CHARS_MAP.fetch(data, data) end |
#reset ⇒ Object
106 107 108 109 |
# File 'lib/game_2048/terminal.rb', line 106 def reset @sgr.clear @output.write("#{CSI}#{RESET}#{SGR}") end |
#reverse ⇒ Object
102 103 104 |
# File 'lib/game_2048/terminal.rb', line 102 def reverse @sgr << REVERSE end |
#show_cursor ⇒ Object
115 116 117 |
# File 'lib/game_2048/terminal.rb', line 115 def show_cursor @output.write("#{CSI}#{SHOW_CURSOR}") end |
#underscore ⇒ Object
90 91 92 |
# File 'lib/game_2048/terminal.rb', line 90 def underscore @sgr << UNDERSCORE end |
#write(text = '') ⇒ Object
142 143 144 145 146 |
# File 'lib/game_2048/terminal.rb', line 142 def write(text = '') text = "#{CSI}#{@sgr.join(';')}#{SGR}#{text}" unless @sgr.empty? @sgr.clear @output.write(text) end |