Module: NattyUI::Features
Overview
Printing Methods collapse
-
#cols(*columns, **attributes) {|row| ... } ⇒ Features
Print text in columns.
-
#div(*text, **attributes) ⇒ Features
Print a text division with attributes.
-
#h1(*text) ⇒ Features
Print given text as a H1 #heading.
-
#h2(*text) ⇒ Features
Print given text as a H2 #heading.
-
#h3(*text) ⇒ Features
Print given text as a H3 #heading.
-
#h4(*text) ⇒ Features
Print given text as a H4 #heading.
-
#h5(*text) ⇒ Features
Print given text as a H5 #heading.
-
#h6(*text) ⇒ Features
Print given text as a H6 #heading.
-
#hbars(values, min: nil, max: nil, normalize: false, text: true, width: :auto, style: nil, text_style: nil) ⇒ Features
Dump given values as horizontal bars.
-
#heading(level, *text) ⇒ Features
Print given text as a heading.
-
#hr(type = :default) ⇒ Features
Print a horizontal rule.
-
#ls(*items, compact: true, glyph: nil) ⇒ Features
Print given items as list (like 'ls' command).
-
#mark(*text, mark: :default, **options) ⇒ Features
Print given text with a decoration mark.
-
#pin(*text, mark: nil, **options) ⇒ Features
Print given text as lines like #puts.
-
#progress(title, max: nil, pin: false, &block) ⇒ Object
Dynamically display a task progress.
-
#puts(*text, **options) ⇒ Features
Print given text as lines.
-
#quote(*text) ⇒ Features
Print given text as a quotation.
-
#run(*cmd, preserve_spaces: false, max_lines: 10, **options) ⇒ [Process::Status, Array<String>, Array<String>]?
Execute a shell program and return output.
-
#sh(*cmd, preserve_spaces: false, **options) ⇒ Process::Status?
Execute a program.
-
#space(count = 1) ⇒ Features
Print one or more space lines.
-
#table(**attributes) {|table| ... } ⇒ Features
Generate and print a table.
-
#vbars(values, normalize: false, height: 10, bar_width: :auto, style: nil) ⇒ Features
Dump given values as vertical bars.
Sub-Elements collapse
-
#error(title, *text) {|section| ... } ⇒ Object
(also: #err)
Create a visually separated section marked as an error with a title for the output of text elements.
-
#failed(title, *text) {|section| ... } ⇒ Object
Create a visually separated section marked as a failure with a title for the output of text elements.
-
#framed(*text, align: :left, border: :default, border_style: nil) {|frame| ... } ⇒ Object
Create a framed section.
-
#information(title, *text) {|section| ... } ⇒ Object
(also: #info)
Create a visually separated section marked as informational with a title for the output of text elements.
-
#message(title, *text) {|section| ... } ⇒ Object
(also: #msg)
Create a visually separated section with a title for the output of text elements.
-
#section(*text) {|section| ... } ⇒ Object
Create a visually separated section for the output of text elements.
-
#task(title, *text, pin: false) {|task| ... } ⇒ Object
Generate a task section.
-
#warning(title, *text) {|section| ... } ⇒ Object
(also: #warn)
Create a visually separated section marked as a warning with a title for the output of text elements.
User Interaction collapse
-
#await(yes: 'Enter', no: 'Esc') ⇒ true, ...
Wait for user input.
-
#choice(*choices, abortable: false, selected: nil, **kwchoices, &block) ⇒ Object
Allows the user to select an option from a selection.
-
#options(abortable: false, selected: nil, **choices) {|temp| ... } ⇒ {#to_s => [true,false]}?
Allows the user to select from several options.
-
#select(*choices, abortable: false, selected: nil) {|temp| ... } ⇒ Array<#to_s>?
Allows the user to select from several options.
Utilities collapse
-
#temporary {|temp| ... } ⇒ Object
Display some temporary content.
Instance Method Details
#await(yes: 'Enter', no: 'Esc') ⇒ true, ... #await(yes: 'Enter', no: 'Esc') {|temp| ... } ⇒ true, ...
Wait for user input.
802 803 804 805 806 807 808 |
# File 'lib/natty-ui/features.rb', line 802 def await(yes: 'Enter', no: 'Esc') return __await(yes, no) unless block_given? temporary do |temp| yield(temp) __await(yes, no) end end |
#choice(*choices, abortable: false) ⇒ Integer? #choice(*choices, abortable: false) {|temp| ... } ⇒ Integer? #choice(**choices, abortable: false) ⇒ Object? #choice(**choices, abortable: false) {|temp| ... } ⇒ Object?
Allows the user to select an option from a selection. The selected option is returned.
884 885 886 887 888 889 890 891 892 893 |
# File 'lib/natty-ui/features.rb', line 884 def choice(*choices, abortable: false, selected: nil, **kwchoices, &block) return if choices.empty? && kwchoices.empty? choice = if Terminal.ansi? Choice.new(self, choices, kwchoices, abortable, selected) else DumbChoice.new(self, choices, kwchoices, abortable) end __with(choice) { choice.select(&block) } end |
#cols(*columns, **attributes) {|row| ... } ⇒ Features
Print text in columns. This is a shorthand to define a Table with a single row.
401 402 403 404 405 406 407 408 409 |
# File 'lib/natty-ui/features.rb', line 401 def cols(*columns, **attributes) tab_att, att = Utils.split_table_attr(attributes) table(**tab_att) do |table| table.add do |row| columns.each { row.add(_1, **att) } yield(row) if block_given? end end end |
#div(*text, **attributes) ⇒ Features
Print a text division with attributes. This is a shorthand to define a Table with a single cell.
432 433 434 435 436 437 |
# File 'lib/natty-ui/features.rb', line 432 def div(*text, **attributes) return self if text.empty? tab_att, att = Utils.split_table_attr(attributes) tab_att[:border_around] = true table(**tab_att) { |table| table.add { _1.add(*text, **att) } } end |
#error(title, *text) {|section| ... } ⇒ Object Also known as: err
Create a visually separated section marked as an error with a title for the output of text elements.
712 |
# File 'lib/natty-ui/features.rb', line 712 def error(title, *text, &block) = __tsec(:error, title, text, &block) |
#failed(title, *text) {|section| ... } ⇒ Object
Create a visually separated section marked as a failure with a title for the output of text elements.
719 |
# File 'lib/natty-ui/features.rb', line 719 def failed(title, *text, &block) = __tsec(:failed, title, text, &block) |
#framed(*text, align: :left, border: :default, border_style: nil) {|frame| ... } ⇒ Object
Create a framed section.
737 738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/natty-ui/features.rb', line 737 def framed(*text, align: :left, border: :default, border_style: nil, &block) __with( Framed.new( self, Utils.align(align), Theme.current.border(border), Utils.style(border_style), text ), &block ) end |
#h1(*text) ⇒ Features
Print given text as a H1 #heading.
256 |
# File 'lib/natty-ui/features.rb', line 256 def h1(*text) = heading(1, *text) |
#h2(*text) ⇒ Features
Print given text as a H2 #heading.
263 |
# File 'lib/natty-ui/features.rb', line 263 def h2(*text) = heading(2, *text) |
#h3(*text) ⇒ Features
Print given text as a H3 #heading.
270 |
# File 'lib/natty-ui/features.rb', line 270 def h3(*text) = heading(3, *text) |
#h4(*text) ⇒ Features
Print given text as a H4 #heading.
277 |
# File 'lib/natty-ui/features.rb', line 277 def h4(*text) = heading(4, *text) |
#h5(*text) ⇒ Features
Print given text as a H5 #heading.
284 |
# File 'lib/natty-ui/features.rb', line 284 def h5(*text) = heading(5, *text) |
#h6(*text) ⇒ Features
Print given text as a H6 #heading.
291 |
# File 'lib/natty-ui/features.rb', line 291 def h6(*text) = heading(6, *text) |
#hbars(values, min: nil, max: nil, normalize: false, text: true, width: :auto, style: nil, text_style: nil) ⇒ Features
Dump given values as horizontal bars.
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/natty-ui/features.rb', line 499 def ( values, min: nil, max: nil, normalize: false, text: true, width: :auto, style: nil, text_style: nil ) return self if (values = values.to_a).empty? if values.any?(&:negative?) raise(ArgumentError, 'values can not be negative') end style = text_style = nil unless Terminal.ansi? renderer = HBarsRenderer.new(values, min, max) renderer.with_text(text_style) if text puts(*renderer.lines(Utils.as_size(3..columns, width), style, normalize)) end |
#heading(level, *text) ⇒ Features
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/natty-ui/features.rb', line 238 def heading(level, *text) prefix, suffix = Theme.current.heading(level) puts( *text, max_width: columns, prefix: prefix, prefix_width: prefix.width, suffix: suffix, suffix_width: suffix.width, align: :centered ) end |
#hr(type = :default) ⇒ Features
Print a horizontal rule.
302 303 304 305 306 |
# File 'lib/natty-ui/features.rb', line 302 def hr(type = :default) theme = Theme.current bc = theme.border(type)[10] puts("#{theme.heading_sytle}#{bc * columns}") end |
#information(title, *text) {|section| ... } ⇒ Object Also known as: info
Create a visually separated section marked as informational with a title for the output of text elements.
696 697 698 |
# File 'lib/natty-ui/features.rb', line 696 def information(title, *text, &block) __tsec(:information, title, text, &block) end |
#ls(*items, compact: true, glyph: nil) ⇒ Features
Print given items as list (like 'ls' command).
Each list item will optionally be decorated with the given glyph as:
Integeras the start value for a numbered listSymbolas the start symbol:hexto create a hexadecimal numbered list- any text as prefix
341 342 343 344 345 |
# File 'lib/natty-ui/features.rb', line 341 def ls(*items, compact: true, glyph: nil) return self if items.empty? renderer = compact ? CompactLSRenderer : LSRenderer puts(*renderer.lines(items, glyph, columns)) end |
#mark(*text, mark: :default, **options) ⇒ Features
Print given text with a decoration mark.
177 178 179 180 181 182 |
# File 'lib/natty-ui/features.rb', line 177 def mark(*text, mark: :default, **) mark = Theme.current.mark(mark) [:first_line_prefix] = mark [:first_line_prefix_width] = mark.width puts(*text, **) end |
#message(title, *text) {|section| ... } ⇒ Object Also known as: msg
Create a visually separated section with a title for the output of text elements.
689 |
# File 'lib/natty-ui/features.rb', line 689 def (title, *text, &block) = __sec(:message, title, text, &block) |
#options(abortable: false, selected: nil, **choices) {|temp| ... } ⇒ {#to_s => [true,false]}?
Allows the user to select from several options. All options are returned with their selection status.
912 913 914 915 916 917 918 919 920 921 |
# File 'lib/natty-ui/features.rb', line 912 def (abortable: false, selected: nil, **choices, &block) return {} if choices.empty? = if Terminal.ansi? Options.new(self, choices, abortable, selected) else DumbOptions.new(self, choices, abortable, selected) end __with() { .select(&block) } end |
#pin(*text, mark: nil, **options) ⇒ Features
204 205 206 |
# File 'lib/natty-ui/features.rb', line 204 def pin(*text, mark: nil, **) mark(*text, mark: mark, pin: true, **) end |
#progress(title, max: nil, pin: false) ⇒ ProgressHelper #progress(title, max: nil, pin: false) {|progress| ... } ⇒ Object
Dynamically display a task progress.
When a max parameter is given the progress will be displayed as a
progress bar below the title. Otherwise the progress is displayed just
by accumulating dots.
571 572 573 574 575 576 577 578 579 |
# File 'lib/natty-ui/features.rb', line 571 def progress(title, max: nil, pin: false, &block) progress = if Terminal.ansi? Progress.new(self, title, max, pin) else DumbProgress.new(self, title, max) end block ? __with(progress, &block) : progress end |
#puts(*text, **options) ⇒ Features
Print given text as lines.
39 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/natty-ui/features.rb', line 39 def puts(*text, **) if .empty? bbcode = true max_width = Terminal.columns else bbcode = true if (bbcode = [:bbcode]).nil? ignore_newline = [:eol] == false || [:ignore_newline] if (max_width = [:max_width]).nil? return self if (max_width = Terminal.columns).zero? elsif max_width < 1 if max_width > 0 max_width *= Terminal.columns elsif max_width < 0 max_width += Terminal.columns else return self end end prefix_width = if (prefix = [:prefix]) prefix = Ansi.bbcode(prefix) if bbcode [:prefix_width] || Text.width(prefix, bbcode: false) else 0 end if (first_line = [:first_line_prefix]) first_line = Ansi.bbcode(first_line) if bbcode first_line_width = [:first_line_prefix_width] || Text.width(first_line, bbcode: false) if prefix_width < first_line_width prefix_next = "#{prefix}#{' ' * (first_line_width - prefix_width)}" prefix = first_line prefix_width = first_line_width else prefix_next = prefix prefix = if first_line_width < prefix_width first_line + (' ' * (prefix_width - first_line_width)) else first_line end end end max_width -= prefix_width if (suffix = [:suffix]) suffix = Ansi.bbcode(suffix) if bbcode max_width -= [:suffix_width] || Text.width(suffix, bbcode: false) end end return self if max_width <= 0 lines = Text.each_line_with_size( *text, limit: max_width, bbcode: bbcode, ansi: Terminal.ansi?, ignore_newline: ignore_newline ) tail = [:tail] and lines = lines.to_a.last(tail) @__eol ||= Terminal.ansi? ? "\e[m\n" : "\n" if (align = [:align]).nil? lines.each do |line, _| Terminal.print(prefix, line, suffix, @__eol, bbcode: false) @lines_written += 1 prefix, prefix_next = prefix_next, nil if prefix_next end return self end unless [:expand] lines = lines.to_a max_width = lines.max_by(&:last).last end case align when :right lines.each do |line, width| Terminal.print( prefix, ' ' * (max_width - width), line, suffix, @__eol, bbcode: false ) @lines_written += 1 prefix, prefix_next = prefix_next, nil if prefix_next end when :centered lines.each do |line, width| space = max_width - width Terminal.print( prefix, ' ' * (lw = space / 2), line, ' ' * (space - lw), suffix, @__eol, bbcode: false ) @lines_written += 1 prefix, prefix_next = prefix_next, nil if prefix_next end else lines.each do |line, width| Terminal.print( prefix, line, ' ' * (max_width - width), suffix, @__eol, bbcode: false ) @lines_written += 1 prefix, prefix_next = prefix_next, nil if prefix_next end end self end |
#quote(*text) ⇒ Features
Print given text as a quotation.
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/natty-ui/features.rb', line 213 def quote(*text) width = columns * 0.75 quote = Theme.current.mark(:quote) puts( *text, prefix: quote, prefix_width: quote.width, max_width: width < 20 ? nil : width.round ) end |
#run(*cmd, preserve_spaces: false, max_lines: 10, **options) ⇒ [Process::Status, Array<String>, Array<String>]?
Execute a shell program and return output. Limit the lines displayed.
630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/natty-ui/features.rb', line 630 def run(*cmd, preserve_spaces: false, max_lines: 10, **) result = ShellRenderer.run( self, cmd, , preserve_spaces, max_lines.clamp(1, Terminal.rows) ) result if result[0] end |
#section(*text) {|section| ... } ⇒ Object
Create a visually separated section for the output of text elements. Like any other Element sections support all NattyUI::Features.
675 |
# File 'lib/natty-ui/features.rb', line 675 def section(*text, &block) = __sec(:default, nil, text, &block) |
#select(*choices, abortable: false, selected: nil) {|temp| ... } ⇒ Array<#to_s>?
Allows the user to select from several options. The selected options are returned.
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 |
# File 'lib/natty-ui/features.rb', line 944 def select(*choices, abortable: false, selected: nil, &block) return [] if choices.empty? choices = choices[0] if choices.size == 1 && choices[0].is_a?(Enumerable) if selected == :all sel = true elsif selected selected = choices[selected.to_i] end ( abortable: abortable, selected: selected, **choices.to_h { [_1, sel] }, &block ).filter_map { |key, selected| key if selected } end |
#sh(*cmd, preserve_spaces: false, **options) ⇒ Process::Status?
Execute a program.
605 606 607 |
# File 'lib/natty-ui/features.rb', line 605 def sh(*cmd, preserve_spaces: false, **) ShellRenderer.sh(self, cmd, , preserve_spaces) end |
#space(count = 1) ⇒ Features
Print one or more space lines.
314 315 316 |
# File 'lib/natty-ui/features.rb', line 314 def space(count = 1) (count = count.to_i).positive? ? puts("\n" * count) : self end |
#table(**attributes) {|table| ... } ⇒ Features
Generate and print a table. See Table for much more details about table generation.
376 377 378 379 380 381 382 383 384 |
# File 'lib/natty-ui/features.rb', line 376 def table(**attributes) return self unless block_given? yield(table = Table.new(**attributes)) puts( *TableRenderer[table, columns], align: table.attributes.position, expand: true ) end |
#task(title, *text, pin: false) {|task| ... } ⇒ Object
Generate a task section.
760 761 762 |
# File 'lib/natty-ui/features.rb', line 760 def task(title, *text, pin: false, &block) __with(Task.new(self, title, text, pin), &block) end |
#temporary {|temp| ... } ⇒ Object
Display some temporary content. The content displayed in the block will be erased after the block ends.
984 |
# File 'lib/natty-ui/features.rb', line 984 def temporary(&block) = __with(Temporary.new(self), &block) |
#vbars(values, normalize: false, height: 10, bar_width: :auto, style: nil) ⇒ Features
Dump given values as vertical bars.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/natty-ui/features.rb', line 456 def ( values, normalize: false, height: 10, bar_width: :auto, style: nil ) return self if (values = values.to_a).empty? if values.any?(&:negative?) raise(ArgumentError, 'values can not be negative') end puts( *VBarsRenderer.lines( values, columns, height, normalize, , Terminal.ansi? ? style : nil ) ) end |
#warning(title, *text) {|section| ... } ⇒ Object Also known as: warn
Create a visually separated section marked as a warning with a title for the output of text elements.
705 |
# File 'lib/natty-ui/features.rb', line 705 def warning(title, *text, &block) = __tsec(:warning, title, text, &block) |