class Inspec::UI
Provides simple terminal UI
interaction primitives for CLI commands and plugins.
Constants
- ANSI_CODES
- EXIT_FAILED_TESTS
- EXIT_FATAL_DEPRECATION
- EXIT_LICENSE_NOT_ACCEPTED
- EXIT_NORMAL
- EXIT_PLUGIN_ERROR
- EXIT_SKIPPED_TESTS
- EXIT_USAGE_ERROR
- GLYPHS
Attributes
io[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/inspec/ui.rb, line 39 def initialize(opts = {}) @color = opts[:color].nil? ? true : opts[:color] @interactive = opts[:interactive].nil? ? $stdout.isatty : opts[:interactive] @io = opts[:io] || $stdout end
Public Instance Methods
bold(str, opts = { print: true })
click to toggle source
# File lib/inspec/ui.rb, line 66 def bold(str, opts = { print: true }) result = color? ? io.print(ANSI_CODES[:bold] + str.to_s + ANSI_CODES[:reset]) : str.to_s print_or_return(result, opts[:print]) end
color?()
click to toggle source
# File lib/inspec/ui.rb, line 45 def color? @color end
emphasis(str, opts = { print: false })
click to toggle source
error(str, opts = { print: true })
click to toggle source
Issues a one-line message, with 'ERROR: ' prepended in bold red.
# File lib/inspec/ui.rb, line 107 def error(str, opts = { print: true }) str = str.dup.to_s result = "" result += color? ? ANSI_CODES[:bold] + ANSI_CODES[:color][:red] : "" result += "ERROR:" result += color? ? ANSI_CODES[:reset] : "" result += " " result += str result += "\n" print_or_return(result, opts[:print]) end
exit(code_sym = :normal)
click to toggle source
#¶ ↑
Exit Codes
#¶ ↑
# File lib/inspec/ui.rb, line 180 def exit(code_sym = :normal) # If it's a number, give them a pass for now. if code_sym.is_a? Numeric code_int = code_sym else code_const = ("EXIT_" + code_sym.to_s.upcase).to_sym unless self.class.const_defined?(code_const) warning("Unrecognized exit constant #{code_const} - exit with code 1") exit(:usage_error) end code_int = self.class.const_get(code_const) end Kernel.exit(code_int) end
headline(str, opts = { print: true })
click to toggle source
# File lib/inspec/ui.rb, line 86 def headline(str, opts = { print: true }) str = str.dup.to_s if str.length < 76 dash_length = 80 - str.length - 4 # 4 spaces dash_length /= 2 else dash_length = 0 end result = "\n" result += " " + (color? ? GLYPHS[:em_dash] : "-") * dash_length + " " result += color? ? ANSI_CODES[:bold] + ANSI_CODES[:color][:white] : "" result += str result += color? ? ANSI_CODES[:reset] : "" result += " " + (color? ? GLYPHS[:em_dash] : "-") * dash_length + " " result += "\n\n" print_or_return(result, opts[:print]) end
interactive?()
click to toggle source
line(opts = { print: true })
click to toggle source
Draws a horizontal line.
# File lib/inspec/ui.rb, line 133 def line(opts = { print: true }) if color? result = ANSI_CODES[:bold] + GLYPHS[:heavy_dash] * 80 + ANSI_CODES[:reset] + "\n" else result = "-" * 80 + "\n" end print_or_return(result, opts[:print]) end
list_item(str, opts = { print: true })
click to toggle source
Makes a bullet point.
# File lib/inspec/ui.rb, line 143 def list_item(str, opts = { print: true }) bullet = color? ? ANSI_CODES[:bold] + ANSI_CODES[:color][:white] + GLYPHS[:bullet] + ANSI_CODES[:reset] : "*" result = " " + bullet + " " + str.to_s + "\n" print_or_return(result, opts[:print]) end
plain(str, opts = { print: true })
click to toggle source
plain_line(str = "", opts = { print: true })
click to toggle source
# File lib/inspec/ui.rb, line 62 def plain_line(str = "", opts = { print: true }) print_or_return(str.to_s + "\n", opts[:print]) end
print_or_return(str, print_flag)
click to toggle source
# File lib/inspec/ui.rb, line 49 def print_or_return(str, print_flag) io.print(str) if print_flag str end
prompt()
click to toggle source
This simply returns a TTY::Prompt object, gated on interactivity being enabled.
# File lib/inspec/ui.rb, line 203 def prompt unless interactive? raise Inspec::UserInteractionRequired, "Somthing is trying to ask the user a question, but interactivity is disabled." end require "tty-prompt" @prompt ||= TTY::Prompt.new end
table(opts = { print: true }) { |the_table| ... }
click to toggle source
Makes a table. Call with a block; block arg will be a TTY::Table object, with an extension for setting the header. Typical use: ui.table do |t|
t.header = ['Name', 'Rank', 'Cereal Number'] t << ['Crunch', 'Captain', 1] t << ['', '', 1] end
# File lib/inspec/ui.rb, line 157 def table(opts = { print: true }) require "inspec/ui_table_helper" the_table = TableHelper.new yield(the_table) colorizer = proc do |data, row, _col| if color? && row == 0 ANSI_CODES[:bold] + ANSI_CODES[:color][:white] + data.to_s + ANSI_CODES[:reset] else data end end render_mode = color? ? :unicode : :ascii padding = [0, 1, 0, 1] # T R B L result = the_table.render(render_mode, filter: colorizer, padding: padding) + "\n" print_or_return(result, opts[:print]) end
warning(str, opts = { print: true })
click to toggle source
Issues a one-line message, with 'WARNING: ' prepended in bold yellow.
# File lib/inspec/ui.rb, line 120 def warning(str, opts = { print: true }) str = str.dup.to_s result = "" result += color? ? ANSI_CODES[:bold] + ANSI_CODES[:color][:yellow] : "" result += "WARNING:" result += color? ? ANSI_CODES[:reset] : "" result += " " result += str result += "\n" print_or_return(result, opts[:print]) end