Class: Rich::Console

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_systemSymbol (readonly)

Returns Color system.

Returns:

  • (Symbol)

    Color system



96
97
98
# File 'lib/rich/console.rb', line 96

def color_system
  @color_system
end

#fileIO (readonly)

Returns Output file.

Returns:

  • (IO)

    Output file



93
94
95
# File 'lib/rich/console.rb', line 93

def file
  @file
end

#force_terminalBoolean (readonly)

Returns Force terminal mode.

Returns:

  • (Boolean)

    Force terminal mode



99
100
101
# File 'lib/rich/console.rb', line 99

def force_terminal
  @force_terminal
end

#height_overrideInteger? (readonly)

Returns Override height.

Returns:

  • (Integer, nil)

    Override height



111
112
113
# File 'lib/rich/console.rb', line 111

def height_override
  @height_override
end

#highlightBoolean (readonly)

Returns Enable highlighting.

Returns:

  • (Boolean)

    Enable highlighting



105
106
107
# File 'lib/rich/console.rb', line 105

def highlight
  @highlight
end

#markupBoolean (readonly)

Returns Enable markup.

Returns:

  • (Boolean)

    Enable markup



102
103
104
# File 'lib/rich/console.rb', line 102

def markup
  @markup
end

#safe_boxBoolean (readonly)

Returns Safe output (escape HTML).

Returns:

  • (Boolean)

    Safe output (escape HTML)



117
118
119
# File 'lib/rich/console.rb', line 117

def safe_box
  @safe_box
end

#styleStyle? (readonly)

Returns Default style.

Returns:

  • (Style, nil)

    Default style



114
115
116
# File 'lib/rich/console.rb', line 114

def style
  @style
end

#themeTerminalTheme (readonly)

Returns Terminal theme.

Returns:



120
121
122
# File 'lib/rich/console.rb', line 120

def theme
  @theme
end

#width_overrideInteger? (readonly)

Returns Override width.

Returns:

  • (Integer, nil)

    Override width



108
109
110
# File 'lib/rich/console.rb', line 108

def width_override
  @width_override
end

Instance Method Details

#clearvoid

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

#encodingString

Returns Output encoding.

Returns:

  • (String)

    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

#heightInteger

Returns Console height in characters.

Returns:

  • (Integer)

    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_cursorvoid

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

Parameters:

  • obj (Object)

    Object to inspect

  • title (String, nil) (defaults to: nil)

    Title

  • methods (Boolean) (defaults to: false)

    Show methods

  • docs (Boolean) (defaults to: true)

    Show docs



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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).

Returns:

  • (Boolean)

    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

#optionsConsoleOptions

Get console options for rendering

Returns:



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rich/console.rb', line 203

def options
  ConsoleOptions.new(
    min_width: 1,
    max_width: width,
    height: height,
    legacy_windows: @legacy_windows,
    encoding: encoding,
    is_terminal: terminal?,
    highlight: @highlight,
    markup: @markup
  )
end

This method returns an undefined value.

Print objects to the console

Parameters:

  • objects (Array)

    Objects to print

  • sep (String) (defaults to: " ")

    Separator

  • end_str (String) (defaults to: "\n")

    End string

  • style (String, Style, nil) (defaults to: nil)

    Style

  • highlight (Boolean) (defaults to: nil)

    Enable highlighting



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

This method returns an undefined value.

Print JSON with highlighting

Parameters:

  • json (String, nil) (defaults to: nil)

    JSON string

  • data (Object) (defaults to: nil)

    Data to convert

  • indent (Integer) (defaults to: 2)

    Indentation



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

This method returns an undefined value.

Print with markup parsing

Parameters:

  • text (String)

    Text with markup



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

Parameters:

  • title (String) (defaults to: "")

    Title

  • style (String) (defaults to: "rule.line")

    Style



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

Parameters:

  • title (String)

    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_cursorvoid

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

#sizeArray<Integer>

Returns [width, height].

Returns:

  • (Array<Integer>)
    width, height


197
198
199
# File 'lib/rich/console.rb', line 197

def size
  [width, height]
end

#terminal?Boolean

Returns Is output a terminal.

Returns:

  • (Boolean)

    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

#widthInteger

Returns Console width in characters.

Returns:

  • (Integer)

    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

Parameters:

  • text (String)

    Text to write



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

Parameters:

  • segments (Array<Segment>)

    Segments to write



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

Parameters:

  • text (String)

    Text to write

  • style (Style)

    Style to apply



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