class HighLineExtension
Add specific improved functionality
Attributes
debug[W]
Public Instance Methods
debug(msg)
click to toggle source
# File lib/rhc/highline_extensions.rb, line 29 def debug(msg) $stderr.puts "DEBUG: #{msg}" if debug? end
debug?()
click to toggle source
# File lib/rhc/highline_extensions.rb, line 35 def debug? @debug end
debug_error(e)
click to toggle source
# File lib/rhc/highline_extensions.rb, line 32 def debug_error(e) debug "#{e.message} (#{e.class})\n #{e.backtrace.join("\n ")}" end
default_max_width()
click to toggle source
# File lib/rhc/highline_extensions.rb, line 109 def default_max_width @wrap_at ? @wrap_at - indentation.length : nil end
header(str, opts={}, &block)
click to toggle source
# File lib/rhc/highline_extensions.rb, line 113 def header(str, opts={}, &block) say Header.new(str, default_max_width, ' ', (opts[:color])) if block_given? indent &block end end
indent(increase=1, statement=nil, multiline=nil) { |self| ... }
click to toggle source
Executes block or outputs statement with indentation
# File lib/rhc/highline_extensions.rb, line 135 def indent(increase=1, statement=nil, multiline=nil) @indent_size ||= 2 @indent_level ||= 0 @indent_level += increase multi = @multi_indent @multi_indent = multiline unless multiline.nil? if block_given? yield self else say(statement) end ensure @multi_indent = multi @indent_level -= increase end
indentation()
click to toggle source
Outputs indentation with current settings
# File lib/rhc/highline_extensions.rb, line 126 def indentation @indent_size ||= 2 @indent_level ||= 0 return ' '*@indent_size*@indent_level end
pager()
click to toggle source
# File lib/rhc/highline_extensions.rb, line 207 def pager #:nocov: return if RHC::Helpers.windows? return unless @output.tty? read, write = IO.pipe unless Kernel.fork # Child process STDOUT.reopen(write) STDERR.reopen(write) if STDERR.tty? read.close write.close return end # Parent process, become pager STDIN.reopen(read) read.close write.close ENV['LESS'] = 'FSRX' # Don't page if the input is short enough Kernel.select [STDIN] # Wait until we have input before we start the pager pager = ENV['PAGER'] || 'less' exec pager rescue exec "/bin/sh", "-c", pager #:nocov: end
paragraph(&block)
click to toggle source
paragraph
highline helper which creates a section with margins of 1, 1
# File lib/rhc/highline_extensions.rb, line 203 def paragraph(&block) section(:top => 1, :bottom => 1, &block) end
raw_no_echo_mode()
click to toggle source
# File lib/rhc/highline_extensions.rb, line 19 def raw_no_echo_mode @state = %xstty -g 2>/dev/null` %xstty raw -echo -icanon isig 2>&1` end
restore_mode()
click to toggle source
# File lib/rhc/highline_extensions.rb, line 24 def restore_mode %xstty #{@state} 2>&1` end
say(msg)
click to toggle source
OVERRIDE
# File lib/rhc/highline_extensions.rb, line 40 def say(msg) if msg.respond_to? :to_str separate_blocks statement = msg.to_str return statement unless statement.present? template = ERB.new(statement, nil, "%") statement = template.result(binding) if @wrap_at statement = statement.chomp.textwrap_ansi(@wrap_at, false) if @last_line_open && statement.length > 1 @last_line_open = false @output.puts end statement = statement.join("#{indentation}\n") end statement = send(:page_print, statement) unless @page_at.nil? @output.print(indentation) unless @last_line_open @last_line_open = if statement[-1, 1] == " " or statement[-1, 1] == "\t" @output.print(statement) @output.flush #statement.strip_ansi.length + (@last_line_open || 0) true else @output.puts(statement) false end elsif msg.respond_to? :each separate_blocks @output.print if @last_line_open @last_line_open = false color = msg.color if msg.respond_to? :color @output.print HighLine::Style(color).code if color msg.each do |s| @output.print indentation @output.puts s end @output.print HighLine::CLEAR if color @output.flush end msg end
section(params={}, &block)
click to toggle source
section
highline helper mixin which correctly formats block of say and ask output to have correct margins. section remembers the last margin used and calculates the relitive margin from the previous section. For example:
section(bottom=1) do
say "Hello"
end
section(top=1) do
say "World"
end
Will output:
> Hello > > World
with only one newline between the two. Biggest margin wins.
params:
top - top margin specified in lines bottom - bottom margin specified in line
# File lib/rhc/highline_extensions.rb, line 181 def section(params={}, &block) top = params[:top] || 0 bottom = params[:bottom] || 0 # the first section cannot take a newline top = 0 unless @margin @margin = [top, @margin || 0].max separate_blocks if params[:now] value = block.call say "\n" if @last_line_open @margin = [bottom, @margin].max value end
table(items, opts={}, &block)
click to toggle source
given an array of arrays “items”, construct an array of strings that can be used to print in tabular form.
# File lib/rhc/highline_extensions.rb, line 96 def table(items, opts={}, &block) items = items.map(&block) if block_given? opts[:width] ||= default_max_width Table.new(items, opts) end
table_args(indent=nil, *args)
click to toggle source
# File lib/rhc/highline_extensions.rb, line 102 def table_args(indent=nil, *args) opts = {} opts[:indent] = indent opts[:width] = [default_max_width, *args] opts end
Private Instance Methods
separate_blocks()
click to toggle source
# File lib/rhc/highline_extensions.rb, line 237 def separate_blocks if (@margin ||= 0) > 0 && !@last_line_open @output.print "\n" * @margin @margin = 0 end end