Class: HighLineExtension
- Defined in:
- lib/rhc/highline_extensions.rb
Overview
Add specific improved functionality
Instance Attribute Summary collapse
Instance Method Summary collapse
- #debug? ⇒ Boolean
- #debug_error(e) ⇒ Object
- #default_max_width ⇒ Object
- #header(str, opts = {}, &block) ⇒ Object
-
#indent(increase = 1, statement = nil, multiline = nil) ⇒ Object
Executes block or outputs statement with indentation.
-
#indentation ⇒ Object
Outputs indentation with current settings.
- #pager ⇒ Object
-
#paragraph(&block) ⇒ Object
paragraph.
- #raw_no_echo_mode ⇒ Object
- #restore_mode ⇒ Object
-
#say(msg) ⇒ Object
OVERRIDE.
-
#section(params = {}, &block) ⇒ Object
section.
-
#table(items, opts = {}, &block) ⇒ Object
given an array of arrays “items”, construct an array of strings that can be used to print in tabular form.
- #table_args(indent = nil, *args) ⇒ Object
Instance Attribute Details
#debug(msg) ⇒ Object
29 30 31 |
# File 'lib/rhc/highline_extensions.rb', line 29 def debug(msg) $stderr.puts "DEBUG: #{msg}" if debug? end |
Instance Method Details
#debug? ⇒ Boolean
35 36 37 |
# File 'lib/rhc/highline_extensions.rb', line 35 def debug? @debug end |
#debug_error(e) ⇒ Object
32 33 34 |
# File 'lib/rhc/highline_extensions.rb', line 32 def debug_error(e) debug "#{e.} (#{e.class})\n #{e.backtrace.join("\n ")}" end |
#default_max_width ⇒ Object
109 110 111 |
# File 'lib/rhc/highline_extensions.rb', line 109 def default_max_width @wrap_at ? @wrap_at - indentation.length : nil end |
#header(str, opts = {}, &block) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/rhc/highline_extensions.rb', line 113 def header(str,opts = {}, &block) say Header.new(str, default_max_width, ' ') if block_given? indent &block end end |
#indent(increase = 1, statement = nil, multiline = nil) ⇒ Object
Executes block or outputs statement with indentation
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# 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 ⇒ Object
Outputs indentation with current settings
126 127 128 129 130 |
# File 'lib/rhc/highline_extensions.rb', line 126 def indentation @indent_size ||= 2 @indent_level ||= 0 return ' '*@indent_size*@indent_level end |
#pager ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# 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) ⇒ Object
paragraph
highline helper which creates a section with margins of 1, 1
203 204 205 |
# File 'lib/rhc/highline_extensions.rb', line 203 def paragraph(&block) section(:top => 1, :bottom => 1, &block) end |
#raw_no_echo_mode ⇒ Object
19 20 21 22 |
# File 'lib/rhc/highline_extensions.rb', line 19 def raw_no_echo_mode @state = `stty -g 2>/dev/null` `stty raw -echo -icanon isig 2>&1` end |
#restore_mode ⇒ Object
24 25 26 |
# File 'lib/rhc/highline_extensions.rb', line 24 def restore_mode `stty #{@state} 2>&1` end |
#say(msg) ⇒ Object
OVERRIDE
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# 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) ⇒ Object
given an array of arrays “items”, construct an array of strings that can be used to print in tabular form.
96 97 98 99 100 |
# 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) ⇒ Object
102 103 104 105 106 107 |
# 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 |