Module: NattyUI::Features
Overview
Output methods collapse
-
#h1(title) ⇒ Features
Prints a level-1 heading.
-
#h2(title) ⇒ Features
Prints a level-2 heading.
-
#h3(title) ⇒ Features
Prints a level-3 heading.
-
#h4(title) ⇒ Features
Prints a level-4 heading.
-
#h5(title) ⇒ Features
Prints a level-5 heading.
-
#h6(title) ⇒ Features
Prints a level-6 heading.
-
#hbars(values, min: nil, max: nil, normalize: false, width: :auto, style: nil, text: true, text_style: nil) ⇒ Features
Prints a horizontal bar chart.
-
#heading(level, title) ⇒ Features
Prints a heading at the given level.
-
#hr(kind = nil) ⇒ Features
Prints a horizontal rule spanning the available width.
-
#ls(*items, compact: true, glyph: nil) ⇒ Features
Prints a multi-column list.
-
#mark(*text, mark: :default, **popts) ⇒ Features
Prints text with a leading mark symbol.
-
#ok(*text, **popts) ⇒ Features
Prints text with a green check-mark prefix.
-
#pin(*text, mark: :default, **popts) ⇒ Features
Prints text with a mark that persists after a Temporary element closes.
-
#puts(*text, **popts) ⇒ Features
Formats and prints text to the terminal.
-
#quote(*text) ⇒ Features
Prints text with a left-side quotation border.
-
#space(count = 1) ⇒ Features
Prints one or more blank lines.
-
#table(**options) {|table| ... } ⇒ Features
Renders a Table to the terminal.
-
#vbars(values, min: nil, max: nil, normalize: false, height: 10, bar_width: 3, style: nil) ⇒ Features
Prints a vertical bar chart.
Section elements collapse
-
#error(title, border: :default) {|section| ... } ⇒ Object, Section
Creates a Section with
:errorstyling. -
#fatal(title, border: :default) {|section| ... } ⇒ Object, Section
Creates a Section with
:fatalstyling. -
#frame(title = nil, border: :default, style: nil) {|frame| ... } ⇒ Object, Frame
Creates a Frame element — a bordered box with an optional title.
-
#information(title, border: :default) {|section| ... } ⇒ Object, Section
(also: #info)
Creates a Section with
:informationstyling. -
#margin {|margin| ... } ⇒ Object, Margin
Creates a Margin element that adds horizontal and vertical whitespace.
-
#message(title, border: :default) {|section| ... } ⇒ Object, Section
(also: #msg)
Creates a Section with
:messagestyling. -
#progress(*title, max: nil, **popts) {|progress| ... } ⇒ Object, Progress
Creates a Progress element for tracking incremental work.
-
#section(title = nil, type: :default, border: :default) {|section| ... } ⇒ Object, Section
(also: #begin)
Creates a Section element — a bordered container with an optional title.
-
#task(title, pin: false) {|task| ... } ⇒ Object, Task
Creates a Task element — a labelled step that shows a spinner while running and a check mark on success.
-
#temporary {|temp| ... } ⇒ Object, Temporary
Creates a Temporary element whose output is erased when it closes.
-
#warning(title, border: :default) {|section| ... } ⇒ Object, Section
(also: #warn)
Creates a Section with
:warningstyling.
User Interaction collapse
-
#await {|temp| ... } ⇒ nil
Waits for the user to press any key.
-
#choice(*items, abortable: false, selected: nil, **pairs) {|temp| ... } ⇒ Object
Presents a list of options and returns the one the user selects.
-
#query(**options) {|temp| ... } ⇒ Object
Waits for a key event and returns information about it.
-
#select(*items, abortable: false, selected: nil, **pairs) {|temp| ... } ⇒ Array
Presents a list of options and returns all items the user selects.
Utilities collapse
-
#run(*cmd, env = {}, shell: false, input: nil, max_lines: 10, **spawn_options) ⇒ Array(Process::Status, Array<String>, Array<String>)?
Executes a shell command, captures its output, and returns it.
-
#sh(*cmd, env = {}, shell: false, input: nil, **spawn_options) ⇒ Object
Executes a shell command and prints its output to the terminal.
Instance Method Details
#await {|temp| ... } ⇒ nil
Waits for the user to press any key.
676 677 678 679 680 681 682 |
# File 'lib/natty-ui/features.rb', line 676 def await yield(temp = temporary) if block_given? Terminal.read_key_event nil ensure temp&.end end |
#choice(*items, abortable: false, selected: nil) ⇒ Object #choice(abortable: false, selected: nil, **pairs) ⇒ Object
Presents a list of options and returns the one the user selects.
In ANSI mode the user navigates with arrow keys and confirms with Enter. In dumb mode items are numbered and the user types the item number.
754 755 756 757 758 759 760 761 762 763 764 |
# File 'lib/natty-ui/features.rb', line 754 def choice(*items, abortable: false, selected: nil, **pairs) return if items.empty? && pairs.empty? yield(temp = temporary) if block_given? (Terminal.ansi? ? Choice : DumbChoice).new( self, items + pairs.values, Array.new(items.size, &:itself) + pairs.keys ).select(abortable, selected) ensure temp&.end end |
#error(title, border: :default) {|section| ... } ⇒ Object, Section
Creates a Section with :error styling.
560 561 562 |
# File 'lib/natty-ui/features.rb', line 560 def error(title, border: :default, &) section(title, border:, type: :error, &) end |
#fatal(title, border: :default) {|section| ... } ⇒ Object, Section
Creates a Section with :fatal styling.
570 571 572 |
# File 'lib/natty-ui/features.rb', line 570 def fatal(title, border: :default, &) section(title, border:, type: :fatal, &) end |
#frame(title = nil, border: :default, style: nil) {|frame| ... } ⇒ Object, Frame
Creates a NattyUI::Frame element — a bordered box with an optional title.
599 600 601 |
# File 'lib/natty-ui/features.rb', line 599 def frame(title = nil, border: :default, style: nil, &) __with(Frame.new(self, title, border, style), &) end |
#h1(title) ⇒ Features
Prints a level-1 heading.
213 |
# File 'lib/natty-ui/features.rb', line 213 def h1(title) = heading(1, title) |
#h2(title) ⇒ Features
Prints a level-2 heading.
219 |
# File 'lib/natty-ui/features.rb', line 219 def h2(title) = heading(2, title) |
#h3(title) ⇒ Features
Prints a level-3 heading.
225 |
# File 'lib/natty-ui/features.rb', line 225 def h3(title) = heading(3, title) |
#h4(title) ⇒ Features
Prints a level-4 heading.
231 |
# File 'lib/natty-ui/features.rb', line 231 def h4(title) = heading(4, title) |
#h5(title) ⇒ Features
Prints a level-5 heading.
237 |
# File 'lib/natty-ui/features.rb', line 237 def h5(title) = heading(5, title) |
#h6(title) ⇒ Features
Prints a level-6 heading.
243 |
# File 'lib/natty-ui/features.rb', line 243 def h6(title) = heading(6, title) |
#hbars(values, min: nil, max: nil, normalize: false, width: :auto, style: nil, text: true, text_style: nil) ⇒ Features
Prints a horizontal bar chart.
All values must be non-negative.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/natty-ui/features.rb', line 354 def ( values, min: nil, max: nil, normalize: false, width: :auto, style: nil, text: true, text_style: nil ) = HBars.new(values, min, max, normalize) return self if .empty? raise(ArgumentError, 'values can not be negative') unless .valid? .with_text(text_style) if text puts(*.lines(columns, width, style), bbcode: false) end |
#heading(level, title) ⇒ Features
207 |
# File 'lib/natty-ui/features.rb', line 207 def heading(level, title) = Heading.render(self, level, title) |
#hr(kind = nil) ⇒ Features
Prints a horizontal rule spanning the available width.
261 |
# File 'lib/natty-ui/features.rb', line 261 def hr(kind = nil) = HorizontalRule.render(self, kind) |
#information(title, border: :default) {|section| ... } ⇒ Object, Section Also known as: info
Creates a Section with :information styling.
538 539 540 |
# File 'lib/natty-ui/features.rb', line 538 def information(title, border: :default, &) section(title, border:, type: :information, &) end |
#ls(*items, compact: true, glyph: nil) ⇒ Features
Prints a multi-column list.
Items are arranged in columns to fit the terminal width.
286 287 288 289 |
# File 'lib/natty-ui/features.rb', line 286 def ls(*items, compact: true, glyph: nil) return self if items.empty? puts(*(compact ? CompactLS : LS).lines(columns, items, glyph)) end |
#margin(value) ⇒ Object, Margin #margin(vertical = 0, horizontal = 1) ⇒ Object, Margin #margin(top, horizontal, bottom) ⇒ Object, Margin #margin(top, right, bottom, left) ⇒ Object, Margin #margin(top: 0, right: 0, bottom: 0, left: 0) ⇒ Object, Margin
Creates a Margin element that adds horizontal and vertical whitespace.
486 |
# File 'lib/natty-ui/features.rb', line 486 def margin(*, &) = __with(Margin.new(self, *), &) |
#mark(*text, mark: :default, **popts) ⇒ Features
Prints text with a leading mark symbol.
151 152 153 |
# File 'lib/natty-ui/features.rb', line 151 def mark(*text, mark: :default, **popts) Mark.render(self, mark, *text, **popts) end |
#message(title, border: :default) {|section| ... } ⇒ Object, Section Also known as: msg
Creates a Section with :message styling.
527 528 529 |
# File 'lib/natty-ui/features.rb', line 527 def (title, border: :default, &) section(title, border:, type: :message, &) end |
#ok(*text, **popts) ⇒ Features
Prints text with a green check-mark prefix.
Shorthand for mark(*text, mark: :checkmark, **options).
165 |
# File 'lib/natty-ui/features.rb', line 165 def ok(*text, **popts) = mark(*text, mark: :checkmark, **popts) |
#pin(*text, mark: :default, **popts) ⇒ Features
180 181 182 |
# File 'lib/natty-ui/features.rb', line 180 def pin(*text, mark: :default, **popts) mark(*text, mark:, pin: true, **popts) end |
#progress(*title, max: nil, **popts) {|progress| ... } ⇒ Object, Progress
Creates a Progress element for tracking incremental work.
When max is given the progress is displayed as a percentage bar.
When max is nil an open-ended dot animation is shown instead.
646 647 648 649 650 651 652 653 654 655 656 |
# File 'lib/natty-ui/features.rb', line 646 def progress(*title, max: nil, **popts, &) __with( (Terminal.ansi? ? Progress : DumbProgress).new( self, max, *title, **popts ), & ) end |
#puts(*text, **popts) ⇒ Features
Formats and prints text to the terminal.
Text is word-wrapped to fit the available column width. BBCode-like
markup is interpreted by default (e.g. [b]bold[/b], [red]text[/fg]).
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 |
# File 'lib/natty-ui/features.rb', line 53 def puts(*text, **popts) return if text.empty? popts.delete(:pin) # ignore! if popts.empty? popts[:bbcode] = true popts[:width] = max_width = columns return self if max_width < 1 else popts[:bbcode] = true unless popts.key?(:bbcode) max_width = __determine_max_width(popts.delete(:max_width)) return self if max_width < 1 unless (prefix = popts.delete(:prefix)).nil? prefix = StrConst[prefix] unless prefix.is_a?(StrConst) return self if (max_width -= prefix.width) < 1 end if (cprefix = popts.delete(:cprefix)).nil? cprefix = prefix elsif cprefix == true prefix, cprefix = StrConst.spacer(prefix.width), prefix if prefix else cprefix = StrConst[cprefix] unless cprefix.is_a?(StrConst) unless prefix return self if (max_width -= cprefix.width) < 1 prefix = StrConst.spacer(cprefix.width) end end unless (suffix = popts.delete(:suffix)).nil? suffix = StrConst[suffix] unless suffix.is_a?(StrConst) return self if (max_width -= suffix.width) < 1 end if (csuffix = popts.delete(:csuffix)).nil? csuffix = suffix elsif csuffix == true suffix, csuffix = StrConst.spacer(suffix.width), suffix if suffix else csuffix = StrConst[csuffix] unless csuffix.is_a?(StrConst) return self if (max_width -= csuffix.width) < 1 suffix = StrConst.spacer(csuffix.width) end popts[:width] = max_width end popts[:ansi] = Terminal.ansi? lines = Text.format(*text, **popts) if cprefix || csuffix lines.map! do |line| line = "#{cprefix}#{line}#{csuffix}" cprefix, prefix = prefix, nil if prefix csuffix, suffix = suffix, nil if suffix line end end Terminal.puts(*lines, bbcode: false) @lines_written += lines.size self end |
#query(**options) {|temp| ... } ⇒ Object
Waits for a key event and returns information about it.
Key names are strings such as "a", "Enter", "Esc", "Back",
"Shift+Alt+F1".
704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/natty-ui/features.rb', line 704 def query(**) yield(temp = temporary) if block_given? return Terminal.read_key_event.name if .empty? Terminal.on_key_event do |event| event = event.name found, = .find do |_, value| (value.is_a?(Enumerable) && value.include?(event)) || value == event end break found if found end ensure temp&.end end |
#quote(*text) ⇒ Features
Prints text with a left-side quotation border.
191 |
# File 'lib/natty-ui/features.rb', line 191 def quote(*text) = Quote.render(self, *text) |
#run(*cmd, env = {}, shell: false, input: nil, max_lines: 10, **spawn_options) ⇒ Array(Process::Status, Array<String>, Array<String>)?
Executes a shell command, captures its output, and returns it.
Stdout and stderr lines are displayed in a scrolling region limited to
max_lines. All other arguments are identical to #sh.
870 871 872 873 874 875 876 877 |
# File 'lib/natty-ui/features.rb', line 870 def run(*, max_lines: 10, **) (Terminal.ansi? ? ShellRunner : DumbShellRunner).render( self, max_lines, *, ** ) end |
#section(title = nil, type: :default, border: :default) {|section| ... } ⇒ Object, Section Also known as: begin
Creates a Section element — a bordered container with an optional title.
515 516 517 |
# File 'lib/natty-ui/features.rb', line 515 def section(title = nil, type: :default, border: :default, &) __with(Section.new(self, title, type, border), &) end |
#select(*items, abortable: false, selected: nil) ⇒ Array #select(abortable: false, selected: nil, **pairs) ⇒ Array
Presents a list of options and returns all items the user selects.
In ANSI mode the user toggles items with Space and confirms with Enter. In dumb mode items are numbered and the user types item numbers.
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 |
# File 'lib/natty-ui/features.rb', line 801 def select(*items, abortable: false, selected: nil, **pairs) return if items.empty? && pairs.empty? yield(temp = temporary) if block_given? items = items.to_h { [it, it] }.merge!(pairs) (Terminal.ansi? ? Select : DumbSelect).new( self, if selected == :all items.map { it << true } elsif selected.is_a?(Enumerable) items.map { |ret, txt| [ret, txt, selected.include?(ret)] } elsif selected items.map { |ret, txt| [ret, txt, selected == ret] } else items.map { it << false } end ).select(abortable) ensure temp&.end end |
#sh(*cmd, env = {}, shell: false, input: nil, **spawn_options) ⇒ Object
Executes a shell command and prints its output to the terminal.
All arguments and options are forwarded to Terminal.sh, which in turn
uses Process.spawn.
846 |
# File 'lib/natty-ui/features.rb', line 846 def sh(...) = Shell.render(self, ...) |
#space(count = 1) ⇒ Features
Prints one or more blank lines.
128 129 130 |
# File 'lib/natty-ui/features.rb', line 128 def space(count = 1) (count = count.to_int) < 1 ? self : puts(" \n" * count) end |
#table(**options) {|table| ... } ⇒ Features
407 408 409 410 411 412 413 414 415 416 |
# File 'lib/natty-ui/features.rb', line 407 def table(**) return self unless block_given? yield(table = Table.new) return self if table.empty? puts( *TableRenderer.lines(columns, table, **), bbcode: false, eol: :no_paragraph ) end |
#task(title, pin: false) {|task| ... } ⇒ Object, Task
Creates a Task element — a labelled step that shows a spinner while running and a check mark on success.
622 |
# File 'lib/natty-ui/features.rb', line 622 def task(title, pin: false, &) = __with(Task.new(self, title, pin), &) |
#temporary {|temp| ... } ⇒ Object, Temporary
Creates a Temporary element whose output is erased when it closes.
444 |
# File 'lib/natty-ui/features.rb', line 444 def temporary(&) = __with(Temporary.new(self), &) |
#vbars(values, min: nil, max: nil, normalize: false, height: 10, bar_width: 3, style: nil) ⇒ Features
Prints a vertical bar chart.
All values must be non-negative.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/natty-ui/features.rb', line 318 def ( values, min: nil, max: nil, normalize: false, height: 10, bar_width: 3, style: nil ) = VBars.new(values, min, max, normalize) return self if .empty? raise(ArgumentError, 'values can not be negative') unless .valid? puts(*.lines(columns, height, , style), bbcode: false) end |