Module: Dry::CLI::Help::Terminal

Defined in:
lib/dry/cli/help/terminal.rb,
sig/dry/cli/help.rbs

Overview

The width of the terminal help prints to.

Constant Summary collapse

FALLBACK_WIDTH =

Returns:

  • (Integer)
80

Class Method Summary collapse

Class Method Details

.from_console(console) ⇒ Integer?

Returns:

  • (Integer, nil)


30
31
32
33
34
35
# File 'lib/dry/cli/help/terminal.rb', line 30

def from_console(console)
  columns = console&.winsize&.last
  columns if columns&.positive?
rescue SystemCallError
  nil
end

.from_env(env) ⇒ Integer?

Returns:

  • (Integer, nil)


24
25
26
27
# File 'lib/dry/cli/help/terminal.rb', line 24

def from_env(env)
  columns = Integer(env.fetch("COLUMNS", ""), exception: false)
  columns if columns&.positive?
end

.width(env: ENV, console: IO.console) ⇒ Integer

COLUMNS wins over the console, so a user or a test can pin the width.

Parameters:

  • env (Hash, ENV) (defaults to: ENV)
  • console (IO, nil) (defaults to: IO.console) —

    the controlling terminal, nil without one

  • env: (Hash[String, String]) (defaults to: ENV)
  • console: (Object) (defaults to: IO.console)

Returns:

  • (Integer)


19
20
21
# File 'lib/dry/cli/help/terminal.rb', line 19

def width(env: ENV, console: IO.console)
  from_env(env) || from_console(console) || FALLBACK_WIDTH
end