class Game2048::Terminal
VT100 terminal
Constants
- BLINK
- BOLD
- COLORS
- COLOR_BG
- COLOR_FG
- CPA
- CPR
- CSI
- CUB
- CUD
- CUF
- CUP
- CUU
- DIM
- ED
- HIDE_CURSOR
- KEY_MAP
- RESET
- REVERSE
- SGR
- SHOW_CURSOR
- UNDERSCORE
Public Class Methods
new(input: $stdin, output: $stdout)
click to toggle source
# File lib/game_2048/terminal.rb, line 50 def initialize(input: $stdin, output: $stdout) @input = input @output = output @sgr = [] end
Public Instance Methods
bg_color(color)
click to toggle source
# File lib/game_2048/terminal.rb, line 73 def bg_color(color) color_exist?(color) @sgr << COLORS[color] + COLOR_BG end
blink()
click to toggle source
# File lib/game_2048/terminal.rb, line 90 def blink @sgr << BLINK end
bold()
click to toggle source
# File lib/game_2048/terminal.rb, line 78 def bold @sgr << BOLD end
cooked_mode()
click to toggle source
# File lib/game_2048/terminal.rb, line 155 def cooked_mode @input&.cooked! end
dim()
click to toggle source
# File lib/game_2048/terminal.rb, line 86 def dim @sgr << DIM end
display_size()
click to toggle source
# File lib/game_2048/terminal.rb, line 111 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()
click to toggle source
# File lib/game_2048/terminal.rb, line 64 def erase_display @output.write("#{CSI}2#{ED}") end
fg_color(color)
click to toggle source
# File lib/game_2048/terminal.rb, line 68 def fg_color(color) color_exist?(color) @sgr << COLORS[color] + COLOR_FG end
hide_cursor()
click to toggle source
# File lib/game_2048/terminal.rb, line 103 def hide_cursor @output.write("#{CSI}#{HIDE_CURSOR}") end
move_home()
click to toggle source
# File lib/game_2048/terminal.rb, line 60 def move_home move_to(1, 1) end
move_to(x, y)
click to toggle source
# File lib/game_2048/terminal.rb, line 56 def move_to(x, y) @output.write("#{CSI}#{y};#{x}#{CUP}") end
raw_mode()
click to toggle source
# File lib/game_2048/terminal.rb, line 151 def raw_mode @input&.raw! end
read()
click to toggle source
# File lib/game_2048/terminal.rb, line 132 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 data end
reset()
click to toggle source
# File lib/game_2048/terminal.rb, line 98 def reset @sgr.clear @output.write("#{CSI}#{RESET}#{SGR}") end
reverse()
click to toggle source
# File lib/game_2048/terminal.rb, line 94 def reverse @sgr << REVERSE end
show_cursor()
click to toggle source
# File lib/game_2048/terminal.rb, line 107 def show_cursor @output.write("#{CSI}#{SHOW_CURSOR}") end
underscore()
click to toggle source
# File lib/game_2048/terminal.rb, line 82 def underscore @sgr << UNDERSCORE end
write(text = '')
click to toggle source
# File lib/game_2048/terminal.rb, line 126 def write(text = '') text = "#{CSI}#{@sgr.join(';')}#{SGR}#{text}" unless @sgr.empty? @sgr.clear @output.write(text) end
Private Instance Methods
color_exist?(color)
click to toggle source
# File lib/game_2048/terminal.rb, line 161 def color_exist?(color) raise TerminalError, "Unknown color #{color}" unless COLORS.key?(color) true end