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.
671 672 673 674 675 676 677 |
# File 'lib/natty-ui/features.rb', line 671 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.
749 750 751 752 753 754 755 756 757 758 759 |
# File 'lib/natty-ui/features.rb', line 749 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.
555 556 557 |
# File 'lib/natty-ui/features.rb', line 555 def error(title, border: :default, &) section(title, border:, type: :error, &) end |
#fatal(title, border: :default) {|section| ... } ⇒ Object, Section
Creates a Section with :fatal styling.
565 566 567 |
# File 'lib/natty-ui/features.rb', line 565 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.
594 595 596 |
# File 'lib/natty-ui/features.rb', line 594 def frame(title = nil, border: :default, style: nil, &) __with(Frame.new(self, title, border, style), &) end |
#h1(title) ⇒ Features
Prints a level-1 heading.
212 |
# File 'lib/natty-ui/features.rb', line 212 def h1(title) = heading(1, title) |
#h2(title) ⇒ Features
Prints a level-2 heading.
218 |
# File 'lib/natty-ui/features.rb', line 218 def h2(title) = heading(2, title) |
#h3(title) ⇒ Features
Prints a level-3 heading.
224 |
# File 'lib/natty-ui/features.rb', line 224 def h3(title) = heading(3, title) |
#h4(title) ⇒ Features
Prints a level-4 heading.
230 |
# File 'lib/natty-ui/features.rb', line 230 def h4(title) = heading(4, title) |
#h5(title) ⇒ Features
Prints a level-5 heading.
236 |
# File 'lib/natty-ui/features.rb', line 236 def h5(title) = heading(5, title) |
#h6(title) ⇒ Features
Prints a level-6 heading.
242 |
# File 'lib/natty-ui/features.rb', line 242 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.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/natty-ui/features.rb', line 353 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
206 |
# File 'lib/natty-ui/features.rb', line 206 def heading(level, title) = Heading.render(self, level, title) |
#hr(kind = nil) ⇒ Features
Prints a horizontal rule spanning the available width.
260 |
# File 'lib/natty-ui/features.rb', line 260 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.
533 534 535 |
# File 'lib/natty-ui/features.rb', line 533 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.
285 286 287 288 |
# File 'lib/natty-ui/features.rb', line 285 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.
481 |
# File 'lib/natty-ui/features.rb', line 481 def margin(*, &) = __with(Margin.new(self, *), &) |
#mark(*text, mark: :default, **popts) ⇒ Features
Prints text with a leading mark symbol.
150 151 152 |
# File 'lib/natty-ui/features.rb', line 150 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.
522 523 524 |
# File 'lib/natty-ui/features.rb', line 522 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).
164 |
# File 'lib/natty-ui/features.rb', line 164 def ok(*text, **popts) = mark(*text, mark: :checkmark, **popts) |
#pin(*text, mark: :default, **popts) ⇒ Features
179 180 181 |
# File 'lib/natty-ui/features.rb', line 179 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.
641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/natty-ui/features.rb', line 641 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 |
# 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 StrConst === prefix 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 StrConst === cprefix 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 StrConst === suffix 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 StrConst === csuffix 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".
699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/natty-ui/features.rb', line 699 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.
190 |
# File 'lib/natty-ui/features.rb', line 190 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.
865 866 867 868 869 870 871 872 |
# File 'lib/natty-ui/features.rb', line 865 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.
510 511 512 |
# File 'lib/natty-ui/features.rb', line 510 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.
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/natty-ui/features.rb', line 796 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.
841 |
# File 'lib/natty-ui/features.rb', line 841 def sh(...) = Shell.render(self, ...) |
#space(count = 1) ⇒ Features
Prints one or more blank lines.
127 128 129 |
# File 'lib/natty-ui/features.rb', line 127 def space(count = 1) (count = count.to_int) < 1 ? self : puts(" \n" * count) end |
#table(**options) {|table| ... } ⇒ Features
406 407 408 409 410 411 |
# File 'lib/natty-ui/features.rb', line 406 def table(**) return self unless block_given? yield(table = Table.new) return self if table.empty? puts(*TableRenderer.lines(columns, table, **), bbcode: false) 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.
617 |
# File 'lib/natty-ui/features.rb', line 617 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.
439 |
# File 'lib/natty-ui/features.rb', line 439 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.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/natty-ui/features.rb', line 317 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 |