Class: Rich::Console
- Inherits:
-
Object
- Object
- Rich::Console
- Defined in:
- lib/rich/console.rb
Overview
Main console class for terminal output
Constant Summary collapse
- DEFAULT_REFRESH_RATE =
Default refresh rate for progress/live (Hz)
Gem.win_platform? ? 5 : 10
Instance Attribute Summary collapse
-
#color_system ⇒ Symbol
readonly
Color system.
-
#file ⇒ IO
readonly
Output file.
-
#force_terminal ⇒ Boolean
readonly
Force terminal mode.
-
#height_override ⇒ Integer?
readonly
Override height.
-
#highlight ⇒ Boolean
readonly
Enable highlighting.
-
#markup ⇒ Boolean
readonly
Enable markup.
-
#safe_box ⇒ Boolean
readonly
Safe output (escape HTML).
-
#style ⇒ Style?
readonly
Default style.
-
#theme ⇒ TerminalTheme
readonly
Terminal theme.
-
#width_override ⇒ Integer?
readonly
Override width.
Instance Method Summary collapse
-
#clear ⇒ void
Clear the screen.
-
#encoding ⇒ String
Output encoding.
-
#height ⇒ Integer
Console height in characters.
-
#hide_cursor ⇒ void
Hide the cursor.
-
#initialize(file: $stdout, color_system: nil, force_terminal: nil, markup: true, highlight: true, width: nil, height: nil, style: nil, safe_box: true, theme: nil) ⇒ Console
constructor
A new instance of Console.
-
#inspect(obj, title: nil, methods: false, docs: true) ⇒ void
Inspect an object.
-
#is_terminal? ⇒ Boolean
Whether output is a terminal.
-
#legacy_windows? ⇒ Boolean
Is this a legacy Windows console.
-
#no_color? ⇒ Boolean
Whether color output is suppressed (NO_COLOR, a non-TTY target, or TERM=dumb, unless overridden by FORCE_COLOR/force_terminal).
-
#options ⇒ ConsoleOptions
Get console options for rendering.
-
#print(*objects, sep: " ", end_str: "\n", style: nil, highlight: nil) ⇒ void
Print objects to the console.
-
#print_json(json = nil, data: nil, indent: 2) ⇒ void
Print JSON with highlighting.
-
#print_markup(text) ⇒ void
Print with markup parsing.
-
#rule(title = "", style: "rule.line") ⇒ void
Print a horizontal rule.
-
#set_title(title) ⇒ void
Set window title.
-
#show_cursor ⇒ void
Show the cursor.
-
#size ⇒ Array<Integer>
[width, height].
-
#terminal? ⇒ Boolean
Is output a terminal.
-
#width ⇒ Integer
Console width in characters.
-
#write(text) ⇒ void
Write raw text.
-
#write_segments(segments) ⇒ void
Write segments to output.
-
#write_styled(text, style) ⇒ void
Write styled text.
Constructor Details
#initialize(file: $stdout, color_system: nil, force_terminal: nil, markup: true, highlight: true, width: nil, height: nil, style: nil, safe_box: true, theme: nil) ⇒ Console
Returns a new instance of Console.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rich/console.rb', line 125 def initialize( file: $stdout, color_system: nil, force_terminal: nil, markup: true, highlight: true, width: nil, height: nil, style: nil, safe_box: true, theme: nil ) @file = file @force_terminal = force_terminal @markup = markup @highlight = highlight @width_override = width @height_override = height @style = style.is_a?(String) ? Style.parse(style) : style @safe_box = safe_box @theme = theme || DEFAULT_TERMINAL_THEME @color_system = color_system || detect_color_system @legacy_windows = detect_legacy_windows @no_color = compute_no_color # Enable ANSI on Windows if possible if Gem.win_platform? && defined?(Win32Console) Win32Console.enable_ansi! end end |
Instance Attribute Details
#color_system ⇒ Symbol (readonly)
Returns Color system.
96 97 98 |
# File 'lib/rich/console.rb', line 96 def color_system @color_system end |
#file ⇒ IO (readonly)
Returns Output file.
93 94 95 |
# File 'lib/rich/console.rb', line 93 def file @file end |
#force_terminal ⇒ Boolean (readonly)
Returns Force terminal mode.
99 100 101 |
# File 'lib/rich/console.rb', line 99 def force_terminal @force_terminal end |
#height_override ⇒ Integer? (readonly)
Returns Override height.
111 112 113 |
# File 'lib/rich/console.rb', line 111 def height_override @height_override end |
#highlight ⇒ Boolean (readonly)
Returns Enable highlighting.
105 106 107 |
# File 'lib/rich/console.rb', line 105 def highlight @highlight end |
#markup ⇒ Boolean (readonly)
Returns Enable markup.
102 103 104 |
# File 'lib/rich/console.rb', line 102 def markup @markup end |
#safe_box ⇒ Boolean (readonly)
Returns Safe output (escape HTML).
117 118 119 |
# File 'lib/rich/console.rb', line 117 def safe_box @safe_box end |
#style ⇒ Style? (readonly)
Returns Default style.
114 115 116 |
# File 'lib/rich/console.rb', line 114 def style @style end |
#theme ⇒ TerminalTheme (readonly)
Returns Terminal theme.
120 121 122 |
# File 'lib/rich/console.rb', line 120 def theme @theme end |
#width_override ⇒ Integer? (readonly)
Returns Override width.
108 109 110 |
# File 'lib/rich/console.rb', line 108 def width_override @width_override end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Clear the screen
299 300 301 302 303 304 305 |
# File 'lib/rich/console.rb', line 299 def clear if @legacy_windows && defined?(Win32Console) Win32Console.clear_screen else write(Control.clear_screen) end end |
#encoding ⇒ String
Returns Output encoding.
217 218 219 |
# File 'lib/rich/console.rb', line 217 def encoding @file.respond_to?(:encoding) ? @file.encoding.to_s : "utf-8" end |
#height ⇒ Integer
Returns Console height in characters.
190 191 192 193 194 |
# File 'lib/rich/console.rb', line 190 def height return @height_override if @height_override detect_size[1] end |
#hide_cursor ⇒ void
This method returns an undefined value.
Hide the cursor
319 320 321 322 323 324 325 |
# File 'lib/rich/console.rb', line 319 def hide_cursor if @legacy_windows && defined?(Win32Console) Win32Console.hide_cursor else write(Control.hide_cursor) end end |
#inspect(obj, title: nil, methods: false, docs: true) ⇒ void
This method returns an undefined value.
Inspect an object
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/rich/console.rb', line 389 def inspect(obj, title: nil, methods: false, docs: true) title ||= obj.class.name rule(title, style: "bold") print("Class: #{obj.class}") print("Object ID: #{obj.object_id}") if obj.respond_to?(:instance_variables) ivars = obj.instance_variables unless ivars.empty? print("\nInstance Variables:") ivars.each do |ivar| value = obj.instance_variable_get(ivar) print(" #{ivar}: #{value.inspect}") end end end if methods && obj.respond_to?(:methods) obj_methods = (obj.methods - Object.methods).sort unless obj_methods.empty? print("\nMethods:") obj_methods.each do |method| print(" #{method}") end end end rule(style: "bold") end |
#is_terminal? ⇒ Boolean
Returns Whether output is a terminal.
164 165 166 167 |
# File 'lib/rich/console.rb', line 164 def is_terminal? return @force_terminal unless @force_terminal.nil? @file.respond_to?(:tty?) && @file.tty? end |
#legacy_windows? ⇒ Boolean
Returns Is this a legacy Windows console.
178 179 180 |
# File 'lib/rich/console.rb', line 178 def legacy_windows? @legacy_windows end |
#no_color? ⇒ Boolean
Returns Whether color output is suppressed (NO_COLOR, a non-TTY target, or TERM=dumb, unless overridden by FORCE_COLOR/force_terminal).
159 160 161 |
# File 'lib/rich/console.rb', line 159 def no_color? @no_color end |
#options ⇒ ConsoleOptions
Get console options for rendering
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/rich/console.rb', line 203 def ConsoleOptions.new( min_width: 1, max_width: width, height: height, legacy_windows: @legacy_windows, encoding: encoding, is_terminal: terminal?, highlight: @highlight, markup: @markup ) end |
#print(*objects, sep: " ", end_str: "\n", style: nil, highlight: nil) ⇒ void
This method returns an undefined value.
Print objects to the console
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rich/console.rb', line 228 def print(*objects, sep: " ", end_str: "\n", style: nil, highlight: nil) highlight = @highlight if highlight.nil? base_style = style.is_a?(String) ? Style.parse(style) : style segments = [] objects.each_with_index do |obj, index| segments.concat(render_object(obj, highlight: highlight)) segments << Segment.new(sep) if sep && !sep.empty? && index < objects.length - 1 end segments << Segment.new(end_str) if end_str && !end_str.empty? segments = Segment.apply_style(segments, style: base_style) if base_style write_segments(segments) end |
#print_json(json = nil, data: nil, indent: 2) ⇒ void
This method returns an undefined value.
Print JSON with highlighting
256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/rich/console.rb', line 256 def print_json(json = nil, data: nil, indent: 2) require "json" json_str = json || ::JSON.pretty_generate(data, indent: " " * indent) if @highlight # Colorize JSON highlighted = colorize_json(json_str) print(highlighted) else print(json_str) end end |
#print_markup(text) ⇒ void
This method returns an undefined value.
Print with markup parsing
247 248 249 |
# File 'lib/rich/console.rb', line 247 def print_markup(text) write_segments(Markup.parse(text).to_segments) end |
#rule(title = "", style: "rule.line") ⇒ void
This method returns an undefined value.
Print a horizontal rule
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/rich/console.rb', line 274 def rule(title = "", style: "rule.line") console_width = width rule_style = Style.parse(style) if title.empty? line = "─" * console_width write_styled(line + "\n", rule_style) else title_length = Cells.cell_len(title) + 2 remaining = console_width - title_length if remaining > 0 left_width = remaining / 2 right_width = remaining - left_width line = "─" * left_width + " #{title} " + "─" * right_width else line = title end write_styled(line + "\n", rule_style) end end |
#set_title(title) ⇒ void
This method returns an undefined value.
Set window title
330 331 332 333 334 335 336 |
# File 'lib/rich/console.rb', line 330 def set_title(title) if @legacy_windows && defined?(Win32Console) Win32Console.set_title(title) else write(Control.set_title(title)) end end |
#show_cursor ⇒ void
This method returns an undefined value.
Show the cursor
309 310 311 312 313 314 315 |
# File 'lib/rich/console.rb', line 309 def show_cursor if @legacy_windows && defined?(Win32Console) Win32Console.show_cursor else write(Control.show_cursor) end end |
#size ⇒ Array<Integer>
Returns [width, height].
197 198 199 |
# File 'lib/rich/console.rb', line 197 def size [width, height] end |
#terminal? ⇒ Boolean
Returns Is output a terminal.
170 171 172 173 174 175 |
# File 'lib/rich/console.rb', line 170 def terminal? return @force_terminal unless @force_terminal.nil? return false unless @file.respond_to?(:tty?) @file.tty? end |
#width ⇒ Integer
Returns Console width in characters.
183 184 185 186 187 |
# File 'lib/rich/console.rb', line 183 def width return @width_override if @width_override detect_size[0] end |
#write(text) ⇒ void
This method returns an undefined value.
Write raw text
341 342 343 344 |
# File 'lib/rich/console.rb', line 341 def write(text) @file.write(text) @file.flush if @file.respond_to?(:flush) end |
#write_segments(segments) ⇒ void
This method returns an undefined value.
Write segments to output
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/rich/console.rb', line 364 def write_segments(segments) if @no_color segments.each { |segment| write(segment.text) unless segment.control? } elsif @legacy_windows && defined?(Win32Console) # Legacy console cannot interpret ANSI; drive colors via the Console API. segments.each do |segment| if segment.control? segment.control.each { |code| write(Control.generate(*code)) } elsif segment.style write_styled_legacy(segment.text, segment.style) else write(segment.text) end end else write(Segment.render(segments, color_system: @color_system)) end end |
#write_styled(text, style) ⇒ void
This method returns an undefined value.
Write styled text
350 351 352 353 354 355 356 357 358 359 |
# File 'lib/rich/console.rb', line 350 def write_styled(text, style) if @no_color write(text) elsif @legacy_windows && defined?(Win32Console) write_styled_legacy(text, style) else rendered = style.render(color_system: @color_system) write("#{rendered}#{text}\e[0m") end end |