Module: Ragnetto::Console
- Defined in:
- lib/ragnetto/console.rb,
lib/ragnetto/console/version.rb
Constant Summary collapse
- COLORS =
CORE CONSTANTS
{ "BLACK" => 0, "RED" => 1, "GREEN" => 2, "YELLOW" => 3, "BLUE" => 4, "MAGENTA" => 5, "CYAN" => 6, "WHITE" => 7, "GRAY" => 8, "LIGHT_RED" => 9, "LIGHT_GREEN" => 10, "LIGHT_YELLOW" => 11, "LIGHT_BLUE" => 12, "LIGHT_MAGENTA" => 13, "LIGHT_CYAN" => 14, "LIGHT_WHITE" => 15 }.freeze
- STATE =
{ "OFF" => 0, "ON" => 1 }.freeze
- SHAPES =
{ "BLOCK_BLINK" => 1, "BLOCK_STEADY" => 2, "UNDERLINE_BLINK" => 3, "UNDERLINE_STEADY" => 4, "BAR_BLINK" => 5, "BAR_STEADY" => 6 }.freeze
- VERSION =
"0.1.1"
Class Method Summary collapse
-
.backcolor(color) ⇒ Object
Set the background color (0-15).
-
.caret(shape) ⇒ Object
Change the shape of the cursor (Caret).
-
.clear ⇒ Object
Clears the screen completely and resets the cursor.
-
.cursor(state) ⇒ Object
Manages the visibility of the cursor.
-
.forecolor(color) ⇒ Object
Set the text color (0-15).
-
.getkey ⇒ Object
Reads a single character without echoing.
-
.height ⇒ Object
Gets the current height of the console (rows).
-
.position(x, y) ⇒ Object
Move the cursor to a specific position.
-
.putkey ⇒ Object
Reads a single character and prints it to the screen.
-
.reset ⇒ Object
Reset all styles to default values.
-
.title(title_text) ⇒ Object
Set the terminal window title.
-
.width ⇒ Object
Gets the current width of the console (columns).
-
.write(text, fore = nil, back = nil, x = nil, y = nil) ⇒ Object
Writes text with attributes and positioning.
Class Method Details
.backcolor(color) ⇒ Object
Set the background color (0-15)
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ragnetto/console.rb', line 59 def self.backcolor(color) c = color.is_a?(String) ? COLORS[color.upcase] : color return unless c if c < 8 print "\e[#{40 + c}m" else print "\e[#{100 + (c - 8)}m" end end |
.caret(shape) ⇒ Object
Change the shape of the cursor (Caret)
94 95 96 97 98 |
# File 'lib/ragnetto/console.rb', line 94 def self.caret(shape) s = shape.is_a?(String) ? SHAPES[shape.upcase] : shape print "\e[#{s} q" if s && s >= 1 && s <= 6 end |
.clear ⇒ Object
Clears the screen completely and resets the cursor
54 55 56 |
# File 'lib/ragnetto/console.rb', line 54 def self.clear print "\e[2J\e[H" end |
.cursor(state) ⇒ Object
Manages the visibility of the cursor
83 84 85 86 87 88 89 90 91 |
# File 'lib/ragnetto/console.rb', line 83 def self.cursor(state) s = state.is_a?(String) ? STATE[state.upcase] : state if s == 1 || s == true || s.to_s.upcase == "ON" print "\e[?25h" else print "\e[?25l" end end |
.forecolor(color) ⇒ Object
Set the text color (0-15)
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ragnetto/console.rb', line 71 def self.forecolor(color) c = color.is_a?(String) ? COLORS[color.upcase] : color return unless c if c < 8 print "\e[#{30 + c}m" else print "\e[#{90 + (c - 8)}m" end end |
.getkey ⇒ Object
Reads a single character without echoing
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ragnetto/console.rb', line 128 def self.getkey if Gem.win_platform? cmd = '[console]::ReadKey($true).KeyChar' pipe = IO.popen(['powershell', '-Command', cmd], 'r') char = pipe.read pipe.close char ? char.strip : "" else system("stty -icanon -echo") char = $stdin.read(1) system("stty icanon echo") char end end |
.height ⇒ Object
Gets the current height of the console (rows)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/ragnetto/console.rb', line 205 def self.height if Gem.win_platform? pipe = IO.popen('powershell -command "$host.ui.rawui.WindowSize.Height"') h = pipe.read.to_i pipe.close h.zero? ? 24 : h else pipe = IO.popen("stty size 2>/dev/null") res = pipe.read pipe.close res.empty? ? 24 : res.split[0].to_i end rescue 24 end |
.position(x, y) ⇒ Object
Move the cursor to a specific position
101 102 103 |
# File 'lib/ragnetto/console.rb', line 101 def self.position(x, y) print "\e[#{y};#{x}H" end |
.putkey ⇒ Object
Reads a single character and prints it to the screen
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/ragnetto/console.rb', line 147 def self.putkey char = "" if Gem.win_platform? cmd = '[console]::ReadKey($true).KeyChar' begin pipe = IO.popen(['powershell', '-Command', cmd], 'r') raw_out = pipe.read pipe.close char = raw_out ? raw_out.gsub(/[\r\n]/, '') : "" unless char.empty? print char $stdout.flush end rescue end else system("stty -icanon -echo") char = $stdin.read(1) system("stty icanon echo") if char && !char.empty? print char $stdout.flush end end char end |
.reset ⇒ Object
Reset all styles to default values
222 223 224 225 226 |
# File 'lib/ragnetto/console.rb', line 222 def self.reset print "\e[0m" print "\e[?25h" print "\e[0 q" end |
.title(title_text) ⇒ Object
Set the terminal window title
181 182 183 184 185 |
# File 'lib/ragnetto/console.rb', line 181 def self.title(title_text) return unless title_text.is_a?(String) print "\e]0;#{title_text}\a" end |
.width ⇒ Object
Gets the current width of the console (columns)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/ragnetto/console.rb', line 188 def self.width if Gem.win_platform? pipe = IO.popen('powershell -command "$host.ui.rawui.WindowSize.Width"') w = pipe.read.to_i pipe.close w.zero? ? 80 : w else pipe = IO.popen("stty size 2>/dev/null") res = pipe.read pipe.close res.empty? ? 80 : res.split[1].to_i end rescue 80 end |
.write(text, fore = nil, back = nil, x = nil, y = nil) ⇒ Object
Writes text with attributes and positioning
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ragnetto/console.rb', line 106 def self.write(text, fore = nil, back = nil, x = nil, y = nil) out = "" out << "\e[#{y};#{x}H" if x && y if back c_back = back.is_a?(String) ? COLORS[back.upcase] : back out << "\e[#{c_back < 8 ? 40 + c_back : 100 + (c_back - 8)}m" if c_back end if fore c_fore = fore.is_a?(String) ? COLORS[fore.upcase] : fore out << "\e[#{c_fore < 8 ? 30 + c_fore : 90 + (c_fore - 8)}m" if c_fore end out << text.to_s out << "\e[0m" print out end |