Class: HighLine
- Includes:
- SystemExtensions
- Defined in:
- lib/skylight/vendor/cli/highline.rb,
lib/skylight/vendor/cli/highline/menu.rb,
lib/skylight/vendor/cli/highline/style.rb,
lib/skylight/vendor/cli/highline/question.rb,
lib/skylight/vendor/cli/highline/simulate.rb,
lib/skylight/vendor/cli/highline/color_scheme.rb,
lib/skylight/vendor/cli/highline/string_extensions.rb,
lib/skylight/vendor/cli/highline/system_extensions.rb
Overview
Extensions for class String
HighLine::String is a subclass of String with convenience methods added for colorization.
Available convenience methods include:
* 'color' method e.g. highline_string.color(:bright_blue, :underline)
* colors e.g. highline_string.magenta
* RGB colors e.g. highline_string.rgb_ff6000
or highline_string.rgb(255,96,0)
* background colors e.g. highline_string.on_magenta
* RGB background colors e.g. highline_string.on_rgb_ff6000
or highline_string.on_rgb(255,96,0)
* styles e.g. highline_string.underline
Additionally, convenience methods can be chained, for instance the following are equivalent:
highline_string.bright_blue.blink.underline
highline_string.color(:bright_blue, :blink, :underline)
HighLine.color(highline_string, :bright_blue, :blink, :underline)
For those less squeamish about possible conflicts, the same convenience methods can be added to the built-in String class, as follows:
require 'highline'
Highline.colorize_strings
Defined Under Namespace
Modules: StringExtensions, SystemExtensions Classes: ColorScheme, Menu, Question, QuestionError, SampleColorScheme, Simulate, String, Style
Constant Summary collapse
- VERSION =
The version of the installed library.
"1.6.21".freeze
- ERASE_LINE_STYLE =
Embed in a String to clear all previous ANSI sequences. This MUST be done before the program exits!
Style.new(:name=>:erase_line, :builtin=>true, :code=>"\e[K")
- ERASE_CHAR_STYLE =
Erase the character under the cursor.
Style.new(:name=>:erase_char, :builtin=>true, :code=>"\e[P")
- CLEAR_STYLE =
Clear color settings
Style.new(:name=>:clear, :builtin=>true, :code=>"\e[0m")
- RESET_STYLE =
Alias for CLEAR.
Style.new(:name=>:reset, :builtin=>true, :code=>"\e[0m")
- BOLD_STYLE =
Bold; Note: bold + a color works as you’d expect,
Style.new(:name=>:bold, :builtin=>true, :code=>"\e[1m")
- DARK_STYLE =
for example bold black. Bold without a color displays the system-defined bold color (e.g. red on Mac iTerm)
Style.new(:name=>:dark, :builtin=>true, :code=>"\e[2m")
- UNDERLINE_STYLE =
Underline
Style.new(:name=>:underline, :builtin=>true, :code=>"\e[4m")
- UNDERSCORE_STYLE =
Alias for UNDERLINE
Style.new(:name=>:underscore, :builtin=>true, :code=>"\e[4m")
- BLINK_STYLE =
Blink; support uncommon
Style.new(:name=>:blink, :builtin=>true, :code=>"\e[5m")
- REVERSE_STYLE =
Reverse foreground and background
Style.new(:name=>:reverse, :builtin=>true, :code=>"\e[7m")
- CONCEALED_STYLE =
Concealed; support uncommon
Style.new(:name=>:concealed, :builtin=>true, :code=>"\e[8m")
- STYLES =
%w{CLEAR RESET BOLD DARK UNDERLINE UNDERSCORE BLINK REVERSE CONCEALED}
- BLACK_STYLE =
These RGB colors are approximate; see en.wikipedia.org/wiki/ANSI_escape_code
Style.new(:name=>:black, :builtin=>true, :code=>"\e[30m", :rgb=>[ 0, 0, 0])
- RED_STYLE =
Style.new(:name=>:red, :builtin=>true, :code=>"\e[31m", :rgb=>[128, 0, 0])
- GREEN_STYLE =
Style.new(:name=>:green, :builtin=>true, :code=>"\e[32m", :rgb=>[ 0,128, 0])
- BLUE_STYLE =
Style.new(:name=>:blue, :builtin=>true, :code=>"\e[34m", :rgb=>[ 0, 0,128])
- YELLOW_STYLE =
Style.new(:name=>:yellow, :builtin=>true, :code=>"\e[33m", :rgb=>[128,128, 0])
- MAGENTA_STYLE =
Style.new(:name=>:magenta, :builtin=>true, :code=>"\e[35m", :rgb=>[128, 0,128])
- CYAN_STYLE =
Style.new(:name=>:cyan, :builtin=>true, :code=>"\e[36m", :rgb=>[ 0,128,128])
- WHITE_STYLE =
On Mac OSX Terminal, white is actually gray
Style.new(:name=>:white, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192])
- GRAY_STYLE =
Alias for WHITE, since WHITE is actually a light gray on Macs
Style.new(:name=>:gray, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192])
- NONE_STYLE =
On Mac OSX Terminal, this is black foreground, or bright white background. Also used as base for RGB colors, if available
Style.new(:name=>:none, :builtin=>true, :code=>"\e[38m", :rgb=>[ 0, 0, 0])
- BASIC_COLORS =
%w{BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE GRAY NONE}
- COLORS =
colors
- @@use_color =
The setting used to disable color output.
true
- @@track_eof =
The setting used to disable EOF tracking.
true
- @@color_scheme =
The setting used to control color schemes.
nil
Constants included from SystemExtensions
SystemExtensions::CHARACTER_MODE, SystemExtensions::JRUBY
Instance Attribute Summary collapse
-
#indent_level ⇒ Object
The indentation level.
-
#indent_size ⇒ Object
The indentation size.
-
#multi_indent ⇒ Object
Indentation over multiple lines.
-
#page_at ⇒ Object
The current row setting for paging output.
-
#wrap_at ⇒ Object
The current column setting for wrapping output.
Class Method Summary collapse
-
.color(string, *colors) ⇒ Object
This method provides easy access to ANSI color sequences, without the user needing to remember to CLEAR at the end of each sequence.
-
.color_code(*colors) ⇒ Object
In case you just want the color code, without the embedding and the CLEAR.
-
.color_scheme ⇒ Object
Returns the current color scheme.
-
.color_scheme=(setting) ⇒ Object
Pass ColorScheme to setting to set a HighLine color scheme.
- .colorize_strings ⇒ Object
-
.const_missing(name) ⇒ Object
For RGB colors:.
- .String(s) ⇒ Object
- .Style(*args) ⇒ Object
-
.supports_rgb_color? ⇒ Boolean
For checking if the current version of HighLine supports RGB colors Usage: HighLine.supports_rgb_color? rescue false # rescue for compatibility with older versions Note: color usage also depends on HighLine.use_color being set.
-
.track_eof=(setting) ⇒ Object
Pass
false
to setting to turn off HighLine’s EOF tracking. -
.track_eof? ⇒ Boolean
Returns true if HighLine is currently tracking EOF for input.
-
.uncolor(string) ⇒ Object
Remove color codes from a string.
-
.use_color=(setting) ⇒ Object
Pass
false
to setting to turn off HighLine’s color escapes. -
.use_color? ⇒ Boolean
Returns true if HighLine is currently using color escapes.
-
.using_color_scheme? ⇒ Boolean
Returns
true
if HighLine is currently using a color scheme.
Instance Method Summary collapse
-
#agree(yes_or_no_question, character = nil) ⇒ Object
A shortcut to HighLine.ask() a question that only accepts “yes” or “no” answers (“y” and “n” are allowed) and returns
true
orfalse
(true
for “yes”). -
#ask(question, answer_type = String, &details) ⇒ Object
This method is the primary interface for user input.
-
#choose(*items, &details) ⇒ Object
This method is HighLine’s menu handler.
-
#color(*args) ⇒ Object
Works as an instance method, same as the class method.
-
#color_code(*colors) ⇒ Object
Works as an instance method, same as the class method.
-
#indent(increase = 1, statement = nil, multiline = nil) ⇒ Object
Executes block or outputs statement with indentation.
-
#indentation ⇒ Object
Outputs indentation with current settings.
-
#initialize(input = $stdin, output = $stdout, wrap_at = nil, page_at = nil, indent_size = 3, indent_level = 0) ⇒ HighLine
constructor
Create an instance of HighLine, connected to the streams input and output.
-
#list(items, mode = :rows, option = nil) ⇒ Object
This method is a utility for quickly and easily laying out lists.
-
#newline ⇒ Object
Outputs newline.
-
#output_cols ⇒ Object
Returns the number of columns for the console, or a default it they cannot be determined.
-
#output_rows ⇒ Object
Returns the number of rows for the console, or a default if they cannot be determined.
-
#say(statement) ⇒ Object
The basic output method for HighLine objects.
-
#uncolor(string) ⇒ Object
Works as an instance method, same as the class method.
Methods included from SystemExtensions
#get_character, #initialize_system_extensions, #raw_no_echo_mode, #restore_mode, #terminal_size
Constructor Details
#initialize(input = $stdin, output = $stdout, wrap_at = nil, page_at = nil, indent_size = 3, indent_level = 0) ⇒ HighLine
Create an instance of HighLine, connected to the streams input and output.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/skylight/vendor/cli/highline.rb', line 176 def initialize( input = $stdin, output = $stdout, wrap_at = nil, page_at = nil, indent_size=3, indent_level=0 ) @input = input @output = output @multi_indent = true @indent_size = indent_size @indent_level = indent_level self.wrap_at = wrap_at self.page_at = page_at @question = nil @answer = nil @menu = nil @header = nil @prompt = nil @gather = nil @answers = nil @key = nil initialize_system_extensions if respond_to?(:initialize_system_extensions) end |
Instance Attribute Details
#indent_level ⇒ Object
The indentation level
211 212 213 |
# File 'lib/skylight/vendor/cli/highline.rb', line 211 def indent_level @indent_level end |
#indent_size ⇒ Object
The indentation size
209 210 211 |
# File 'lib/skylight/vendor/cli/highline.rb', line 209 def indent_size @indent_size end |
#multi_indent ⇒ Object
Indentation over multiple lines
207 208 209 |
# File 'lib/skylight/vendor/cli/highline.rb', line 207 def multi_indent @multi_indent end |
#page_at ⇒ Object
The current row setting for paging output.
205 206 207 |
# File 'lib/skylight/vendor/cli/highline.rb', line 205 def page_at @page_at end |
#wrap_at ⇒ Object
The current column setting for wrapping output.
203 204 205 |
# File 'lib/skylight/vendor/cli/highline.rb', line 203 def wrap_at @wrap_at end |
Class Method Details
.color(string, *colors) ⇒ Object
This method provides easy access to ANSI color sequences, without the user needing to remember to CLEAR at the end of each sequence. Just pass the string to color, followed by a list of colors you would like it to be affected by. The colors can be HighLine class constants, or symbols (:blue for BLUE, for example). A CLEAR will automatically be embedded to the end of the returned String.
This method returns the original string unchanged if HighLine::use_color? is false
.
377 378 379 380 |
# File 'lib/skylight/vendor/cli/highline.rb', line 377 def self.color( string, *colors ) return string unless self.use_color? Style(*colors).color(string) end |
.color_code(*colors) ⇒ Object
In case you just want the color code, without the embedding and the CLEAR
383 384 385 |
# File 'lib/skylight/vendor/cli/highline.rb', line 383 def self.color_code(*colors) Style(*colors).code end |
.color_scheme ⇒ Object
Returns the current color scheme.
80 81 82 |
# File 'lib/skylight/vendor/cli/highline.rb', line 80 def self.color_scheme @@color_scheme end |
.color_scheme=(setting) ⇒ Object
Pass ColorScheme to setting to set a HighLine color scheme.
75 76 77 |
# File 'lib/skylight/vendor/cli/highline.rb', line 75 def self.color_scheme=( setting ) @@color_scheme = setting end |
.colorize_strings ⇒ Object
108 109 110 |
# File 'lib/skylight/vendor/cli/highline/string_extensions.rb', line 108 def self.colorize_strings ::String.send(:include, StringExtensions) end |
.const_missing(name) ⇒ Object
For RGB colors:
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/skylight/vendor/cli/highline.rb', line 148 def self.const_missing(name) if name.to_s =~ /^(ON_)?(RGB_)([A-F0-9]{6})(_STYLE)?$/ # RGB color on = $1 suffix = $4 if suffix code_name = $1.to_s + $2 + $3 else code_name = name.to_s end style_name = code_name + '_STYLE' style = Style.rgb($3) style = style.on if on const_set(style_name, style) const_set(code_name, style.code) if suffix style else style.code end else raise NameError, "Bad color or uninitialized constant #{name}" end end |
.String(s) ⇒ Object
27 28 29 |
# File 'lib/skylight/vendor/cli/highline/string_extensions.rb', line 27 def self.String(s) HighLine::String.new(s) end |
.Style(*args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/skylight/vendor/cli/highline/style.rb', line 10 def self.Style(*args) args = args.compact.flatten if args.size==1 arg = args.first if arg.is_a?(Style) Style.list[arg.name] || Style.index(arg) elsif arg.is_a?(::String) && arg =~ /^\e\[/ # arg is a code if styles = Style.code_index[arg] styles.first else Style.new(:code=>arg) end elsif style = Style.list[arg] style elsif HighLine.color_scheme && HighLine.color_scheme[arg] HighLine.color_scheme[arg] elsif arg.is_a?(Hash) Style.new(arg) elsif arg.to_s.downcase =~ /^rgb_([a-f0-9]{6})$/ Style.rgb($1) elsif arg.to_s.downcase =~ /^on_rgb_([a-f0-9]{6})$/ Style.rgb($1).on else raise NameError, "#{arg.inspect} is not a defined Style" end else name = args Style.list[name] || Style.new(:list=>args) end end |
.supports_rgb_color? ⇒ Boolean
For checking if the current version of HighLine supports RGB colors Usage: HighLine.supports_rgb_color? rescue false # rescue for compatibility with older versions Note: color usage also depends on HighLine.use_color being set
54 55 56 |
# File 'lib/skylight/vendor/cli/highline.rb', line 54 def self.supports_rgb_color? true end |
.track_eof=(setting) ⇒ Object
Pass false
to setting to turn off HighLine’s EOF tracking.
62 63 64 |
# File 'lib/skylight/vendor/cli/highline.rb', line 62 def self.track_eof=( setting ) @@track_eof = setting end |
.track_eof? ⇒ Boolean
Returns true if HighLine is currently tracking EOF for input.
67 68 69 |
# File 'lib/skylight/vendor/cli/highline.rb', line 67 def self.track_eof? @@track_eof end |
.uncolor(string) ⇒ Object
Remove color codes from a string
398 399 400 |
# File 'lib/skylight/vendor/cli/highline.rb', line 398 def self.uncolor(string) Style.uncolor(string) end |
.use_color=(setting) ⇒ Object
Pass false
to setting to turn off HighLine’s color escapes.
42 43 44 |
# File 'lib/skylight/vendor/cli/highline.rb', line 42 def self.use_color=( setting ) @@use_color = setting end |
.use_color? ⇒ Boolean
Returns true if HighLine is currently using color escapes.
47 48 49 |
# File 'lib/skylight/vendor/cli/highline.rb', line 47 def self.use_color? @@use_color end |
.using_color_scheme? ⇒ Boolean
Returns true
if HighLine is currently using a color scheme.
85 86 87 |
# File 'lib/skylight/vendor/cli/highline.rb', line 85 def self.using_color_scheme? not @@color_scheme.nil? end |
Instance Method Details
#agree(yes_or_no_question, character = nil) ⇒ Object
A shortcut to HighLine.ask() a question that only accepts “yes” or “no” answers (“y” and “n” are allowed) and returns true
or false
(true
for “yes”). If provided a true
value, character will cause HighLine to fetch a single character response. A block can be provided to further configure the question as in HighLine.ask()
Raises EOFError if input is exhausted.
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/skylight/vendor/cli/highline.rb', line 222 def agree( yes_or_no_question, character = nil ) ask(yes_or_no_question, lambda { |yn| yn.downcase[0] == ?y}) do |q| q.validate = /\Ay(?:es)?|no?\Z/i q.responses[:not_valid] = 'Please enter "yes" or "no".' q.responses[:ask_on_error] = :question q.character = character yield q if block_given? end end |
#ask(question, answer_type = String, &details) ⇒ Object
This method is the primary interface for user input. Just provide a question to ask the user, the answer_type you want returned, and optionally a code block setting up details of how you want the question handled. See HighLine.say() for details on the format of question, and HighLine::Question for more information about answer_type and what’s valid in the code block.
If @question
is set before ask() is called, parameters are ignored and that object (must be a HighLine::Question) is used to drive the process instead.
Raises EOFError if input is exhausted.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/skylight/vendor/cli/highline.rb', line 247 def ask( question, answer_type = String, &details ) # :yields: question @question ||= Question.new(question, answer_type, &details) return gather if @question.gather # readline() needs to handle its own output, but readline only supports # full line reading. Therefore if @question.echo is anything but true, # the prompt will not be issued. And we have to account for that now. # Also, JRuby-1.7's ConsoleReader.readLine() needs to be passed the prompt # to handle line editing properly. say(@question) unless ((JRUBY or @question.readline) and @question.echo == true) begin @answer = @question.answer_or_default(get_response) unless @question.valid_answer?(@answer) explain_error(:not_valid) raise QuestionError end @answer = @question.convert(@answer) if @question.in_range?(@answer) if @question.confirm # need to add a layer of scope to ask a question inside a # question, without destroying instance data context_change = self.class.new(@input, @output, @wrap_at, @page_at, @indent_size, @indent_level) if @question.confirm == true confirm_question = "Are you sure? " else # evaluate ERb under initial scope, so it will have # access to @question and @answer template = ERB.new(@question.confirm, nil, "%") confirm_question = template.result(binding) end unless context_change.agree(confirm_question) explain_error(nil) raise QuestionError end end @answer else explain_error(:not_in_range) raise QuestionError end rescue QuestionError retry rescue ArgumentError, NameError => error raise if error.is_a?(NoMethodError) if error. =~ /ambiguous/ # the assumption here is that OptionParser::Completion#complete # (used for ambiguity resolution) throws exceptions containing # the word 'ambiguous' whenever resolution fails explain_error(:ambiguous_completion) else explain_error(:invalid_type) end retry rescue Question::NoAutoCompleteMatch explain_error(:no_completion) retry ensure @question = nil # Reset Question object. end end |
#choose(*items, &details) ⇒ Object
This method is HighLine’s menu handler. For simple usage, you can just pass all the menu items you wish to display. At that point, choose() will build and display a menu, walk the user through selection, and return their choice among the provided items. You might use this in a case statement for quick and dirty menus.
However, choose() is capable of much more. If provided, a block will be passed a HighLine::Menu object to configure. Using this method, you can customize all the details of menu handling from index display, to building a complete shell-like menuing system. See HighLine::Menu for all the methods it responds to.
Raises EOFError if input is exhausted.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/skylight/vendor/cli/highline.rb', line 328 def choose( *items, &details ) @menu = @question = Menu.new(&details) @menu.choices(*items) unless items.empty? # Set auto-completion @menu.completion = @menu. # Set _answer_type_ so we can double as the Question for ask(). @menu.answer_type = if @menu.shell lambda do |command| # shell-style selection first_word = command.to_s.split.first || "" = @menu. .extend(OptionParser::Completion) answer = .complete(first_word) if answer.nil? raise Question::NoAutoCompleteMatch end [answer.last, command.sub(/^\s*#{first_word}\s*/, "")] end else @menu. # normal menu selection, by index or name end # Provide hooks for ERb layouts. @header = @menu.header @prompt = @menu.prompt if @menu.shell selected = ask("Ignored", @menu.answer_type) @menu.select(self, *selected) else selected = ask("Ignored", @menu.answer_type) @menu.select(self, selected) end end |
#color(*args) ⇒ Object
Works as an instance method, same as the class method
393 394 395 |
# File 'lib/skylight/vendor/cli/highline.rb', line 393 def color(*args) self.class.color(*args) end |
#color_code(*colors) ⇒ Object
Works as an instance method, same as the class method
388 389 390 |
# File 'lib/skylight/vendor/cli/highline.rb', line 388 def color_code(*colors) self.class.color_code(*colors) end |
#indent(increase = 1, statement = nil, multiline = nil) ⇒ Object
Executes block or outputs statement with indentation
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/skylight/vendor/cli/highline.rb', line 660 def indent(increase=1, statement=nil, multiline=nil) @indent_level += increase multi = @multi_indent @multi_indent = multiline unless multiline.nil? begin if block_given? yield self else say(statement) end rescue @multi_indent = multi @indent_level -= increase raise end @multi_indent = multi @indent_level -= increase end |
#indentation ⇒ Object
Outputs indentation with current settings
653 654 655 |
# File 'lib/skylight/vendor/cli/highline.rb', line 653 def indentation return ' '*@indent_size*@indent_level end |
#list(items, mode = :rows, option = nil) ⇒ Object
This method is a utility for quickly and easily laying out lists. It can be accessed within ERb replacements of any text that will be sent to the user.
The only required parameter is items, which should be the Array of items to list. A specified mode controls how that list is formed and option has different effects, depending on the mode. Recognized modes are:
:columns_across
-
items will be placed in columns, flowing from left to right. If given, option is the number of columns to be used. When absent, columns will be determined based on wrap_at or a default of 80 characters.
:columns_down
-
Identical to
:columns_across
, save flow goes down. :uneven_columns_across
-
Like
:columns_across
but each column is sized independently. :uneven_columns_down
-
Like
:columns_down
but each column is sized independently. :inline
-
All items are placed on a single line. The last two items are separated by option or a default of “ or ”. All other items are separated by “, ”.
:rows
-
The default mode. Each of the items is placed on its own line. The option parameter is ignored in this mode.
Each member of the items Array is passed through ERb and thus can contain their own expansions. Color escape expansions do not contribute to the final field width.
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/skylight/vendor/cli/highline.rb', line 440 def list( items, mode = :rows, option = nil ) items = items.to_ary.map do |item| if item.nil? "" else ERB.new(item, nil, "%").result(binding) end end if items.empty? "" else case mode when :inline option = " or " if option.nil? if items.size == 1 items.first else items[0..-2].join(", ") + "#{option}#{items.last}" end when :columns_across, :columns_down max_length = actual_length( items.max { |a, b| actual_length(a) <=> actual_length(b) } ) if option.nil? limit = @wrap_at || 80 option = (limit + 2) / (max_length + 2) end items = items.map do |item| pad = max_length + (item.to_s.length - actual_length(item)) "%-#{pad}s" % item end row_count = (items.size / option.to_f).ceil if mode == :columns_across rows = Array.new(row_count) { Array.new } items.each_with_index do |item, index| rows[index / option] << item end rows.map { |row| row.join(" ") + "\n" }.join else columns = Array.new(option) { Array.new } items.each_with_index do |item, index| columns[index / row_count] << item end list = "" columns.first.size.times do |index| list << columns.map { |column| column[index] }. compact.join(" ") + "\n" end list end when :uneven_columns_across if option.nil? limit = @wrap_at || 80 items.size.downto(1) do |column_count| row_count = (items.size / column_count.to_f).ceil rows = Array.new(row_count) { Array.new } items.each_with_index do |item, index| rows[index / column_count] << item end widths = Array.new(column_count, 0) rows.each do |row| row.each_with_index do |field, column| size = actual_length(field) widths[column] = size if size > widths[column] end end if column_count == 1 or widths.inject(0) { |sum, n| sum + n + 2 } <= limit + 2 return rows.map { |row| row.zip(widths).map { |field, i| "%-#{i + (field.to_s.length - actual_length(field))}s" % field }.join(" ") + "\n" }.join end end else row_count = (items.size / option.to_f).ceil rows = Array.new(row_count) { Array.new } items.each_with_index do |item, index| rows[index / option] << item end widths = Array.new(option, 0) rows.each do |row| row.each_with_index do |field, column| size = actual_length(field) widths[column] = size if size > widths[column] end end return rows.map { |row| row.zip(widths).map { |field, i| "%-#{i + (field.to_s.length - actual_length(field))}s" % field }.join(" ") + "\n" }.join end when :uneven_columns_down if option.nil? limit = @wrap_at || 80 items.size.downto(1) do |column_count| row_count = (items.size / column_count.to_f).ceil columns = Array.new(column_count) { Array.new } items.each_with_index do |item, index| columns[index / row_count] << item end widths = Array.new(column_count, 0) columns.each_with_index do |column, i| column.each do |field| size = actual_length(field) widths[i] = size if size > widths[i] end end if column_count == 1 or widths.inject(0) { |sum, n| sum + n + 2 } <= limit + 2 list = "" columns.first.size.times do |index| list << columns.zip(widths).map { |column, width| field = column[index] "%-#{width + (field.to_s.length - actual_length(field))}s" % field }.compact.join(" ").strip + "\n" end return list end end else row_count = (items.size / option.to_f).ceil columns = Array.new(option) { Array.new } items.each_with_index do |item, index| columns[index / row_count] << item end widths = Array.new(option, 0) columns.each_with_index do |column, i| column.each do |field| size = actual_length(field) widths[i] = size if size > widths[i] end end list = "" columns.first.size.times do |index| list << columns.zip(widths).map { |column, width| field = column[index] "%-#{width + (field.to_s.length - actual_length(field))}s" % field }.compact.join(" ").strip + "\n" end return list end else items.map { |i| "#{i}\n" }.join end end end |
#newline ⇒ Object
Outputs newline
682 683 684 |
# File 'lib/skylight/vendor/cli/highline.rb', line 682 def newline @output.puts end |
#output_cols ⇒ Object
Returns the number of columns for the console, or a default it they cannot be determined.
690 691 692 693 694 695 |
# File 'lib/skylight/vendor/cli/highline.rb', line 690 def output_cols return 80 unless @output.tty? terminal_size.first rescue return 80 end |
#output_rows ⇒ Object
Returns the number of rows for the console, or a default if they cannot be determined.
701 702 703 704 705 706 |
# File 'lib/skylight/vendor/cli/highline.rb', line 701 def output_rows return 24 unless @output.tty? terminal_size.last rescue return 24 end |
#say(statement) ⇒ Object
The basic output method for HighLine objects. If the provided statement ends with a space or tab character, a newline will not be appended (output will be flush()ed). All other cases are passed straight to Kernel.puts().
The statement parameter is processed as an ERb template, supporting embedded Ruby code. The template is evaluated with a binding inside the HighLine instance, providing easy access to the ANSI color constants and the HighLine.color() method.
616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/skylight/vendor/cli/highline.rb', line 616 def say( statement ) statement = format_statement(statement) return unless statement.length > 0 # Don't add a newline if statement ends with whitespace, OR # if statement ends with whitespace before a color escape code. if /[ \t](\e\[\d+(;\d+)*m)?\Z/ =~ statement @output.print(indentation+statement) @output.flush else @output.puts(indentation+statement) end end |
#uncolor(string) ⇒ Object
Works as an instance method, same as the class method
403 404 405 |
# File 'lib/skylight/vendor/cli/highline.rb', line 403 def uncolor(string) self.class.uncolor(string) end |