Class: NattyUI::Element

Inherits:
Object
  • Object
show all
Includes:
Features
Defined in:
lib/natty-ui/element.rb

Overview

Base element class supporting all Features.

Direct Known Subclasses

Framed, Section, Temporary

User Interaction collapse

Printing Methods collapse

Sub-Elements collapse

Utilities collapse

Instance Method Details

#await(yes: 'Enter', no: 'Esc') ⇒ true, false #await(yes: 'Enter', no: 'Esc') {|temp| ... } ⇒ true, false Originally defined in module Features

Wait for user input.

Examples:

Wait until user wants to coninue

ui.await { ui.puts '[faint][\\Press ENTER to continue...][/faint]' }

Ask yes/no-question

ui.await(yes: %w[j o t s y d Enter], no: %w[n Esc]) do
  ui.puts 'Do you like NayttUI?'
end
# => true, for user's YES
# => false, for user's NO
# Info:
# The keys will work for Afrikaans, Dutch, English, French, German,
# Italian, Polish, Portuguese, Romanian, Spanish and Swedish.

Overloads:

  • #await(yes: 'Enter', no: 'Esc') {|temp| ... } ⇒ true, false

    Returns wheter the user inputs a positive result.

    Yield Parameters:

    • temp (Temporary)

      temporary displayed section (section will be erased after input)

#choice(*choices, abortable: false) ⇒ Integer? #choice(*choices, abortable: false) {|temp| ... } ⇒ Integer? #choice(**choices, abortable: false) ⇒ Object? #choice(**choices, abortable: false) {|temp| ... } ⇒ Object? Originally defined in module Features

Request a user's chocie.

Overloads:

  • #choice(*choices, abortable: false) {|temp| ... } ⇒ Integer?

    Examples:

    Request a fruit

    ui.choice('Apple', 'Banana', 'Orange') { ui.puts 'What do you prefer?' }
    # => 0, when user likes apples
    # => 1, when bananas are user's favorite
    # => 2, when user is a oranges lover

    Yield Parameters:

    • temp (Temporary)

      temporary displayed section (section will be erased after input)

  • #choice(**choices, abortable: false) {|temp| ... } ⇒ Object?

    Examples:

    Request a preference

    ui.choice(
      k: 'Kitty',
      i: 'iTerm2',
      g: 'Ghostty',
      t: 'Tabby',
      r: 'Rio',
      abortable: true
    ) { ui.puts 'Which terminal emulator do you like?' }
    # => wheter the user selected: :k, :i, :g, :t, :r
    # => nil, when the user aborted

    Yield Parameters:

    • temp (Temporary)

      temporary displayed section (section will be erased after input)

#cols(*columns, **attributes) {|row| ... } ⇒ Features Originally defined in module Features

Print text in columns. This is a shorthand to define a Table with a single row.

Options Hash (**attributes):

Yield Parameters:

  • row (Table::Row)

    helper to define the row layout

#div(*text, **attributes) ⇒ Features Originally defined in module Features

Print a text division with attributes. This is a shorthand to define a Table with a single cell.

Options Hash (**attributes):

#error(title, *text) {|section| ... } ⇒ Object Also known as: err Originally defined in module Features

Create a visually separated section marked as an error with a title for the output of text elements.

Yield Parameters:

See Also:

#failed(title, *text) {|section| ... } ⇒ Object Originally defined in module Features

Create a visually separated section marked as a failure with a title for the output of text elements.

Yield Parameters:

See Also:

#framed(*text, align: :left, border: :default, border_style: nil) {|frame| ... } ⇒ Object Originally defined in module Features

Create a framed section.

Yield Parameters:

#h1(*text) ⇒ Features Originally defined in module Features

Print given text as a H1 #heading.

#h2(*text) ⇒ Features Originally defined in module Features

Print given text as a H2 #heading.

#h3(*text) ⇒ Features Originally defined in module Features

Print given text as a H3 #heading.

#h4(*text) ⇒ Features Originally defined in module Features

Print given text as a H4 #heading.

#h5(*text) ⇒ Features Originally defined in module Features

Print given text as a H5 #heading.

#h6(*text) ⇒ Features Originally defined in module Features

Print given text as a H6 #heading.

#heading(level, *text) ⇒ Features Originally defined in module Features

Print given text as a heading.

There are specific shortcuts for heading levels: #h1, #h2, #h3, #h4, #h5, #h6.

Examples:

Print a level 1 heading

ui.heading(1, 'This is a H1 heading element')
# => ╴╶╴╶─═══ This is a H1 heading element ═══─╴╶╴╶

#hr(type = :default) ⇒ Features Originally defined in module Features

Print a horizontal rule.

Examples:

ui.hr(:heavy)

#information(title, *text) {|section| ... } ⇒ Object Also known as: info Originally defined in module Features

Create a visually separated section marked as informational with a title for the output of text elements.

Yield Parameters:

See Also:

#ls(*items, compact: true, glyph: nil) ⇒ Features Originally defined in module Features

Print given items as list (like 'ls' command).

Each list item will optionally be decorated with the given glyph as:

  • Integer as the start value for a numbered list
  • Symbol as the start symbol
  • :hex to create a hexadecimal numbered list
  • any text as prefix

Examples:

Print all Ruby files as a numbered list

ui.ls(Dir['*/**/*.rb'], glyph: 1)

Print all Ruby files as a bullet point list (with green bullets)

ui.ls(Dir['*/**/*.rb'], glyph: '[green]•[/fg]')

#mark(*text, mark: :default) ⇒ Features Originally defined in module Features

Print given text with a decoration mark.

#message(title, *text) {|section| ... } ⇒ Object Also known as: msg Originally defined in module Features

Create a visually separated section with a title for the output of text elements.

Yield Parameters:

See Also:

#pin(*text, mark: nil, **options) ⇒ Features Originally defined in module Features

Print given text as lines like #puts. Used in elements with temporary output like #task the text will be kept ("pinned").

It can optionally have a decoration marker in first line like #mark.

Examples:

Print two lines decorated as information which are pinned

ui.task 'Do something important' do |task|
  # ...
  task.pin("This is text", "which is pinned", mark: :information)
  # ...
end
# => ✓ Do something important
# =>   𝒊 This is text
# =>     which is pinned.

Options Hash (**options):

  • :align (:left, :right, :centered) — default: :left

    text alignment

  • :ignore_newline (true, false) — default: false

    whether to igniore newline characters

#progress(title, max: nil, pin: false) ⇒ ProgressHelper #progress(title, max: nil, pin: false) {|progress| ... } ⇒ Object Originally defined in module Features

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.

Examples:

Display a progress bar

ui.progress('Download file', max: 1024) do |progress|
  while progress.value < progress.max
    # just to simulate the download
    sleep(0.1)
    bytes_read = rand(10..128)

    # here we actualize the progress
    progress.value += bytes_read
  end
end

Display simple progress

progress = ui.progress('Check some stuff')
10.times do
  # simulate some work
  sleep(0.1)

  # here we actualize the progress
  progress.step
end
progress.ok('Stuff checked ok')

Overloads:

  • #progress(title, max: nil, pin: false) {|progress| ... } ⇒ Object

    Returns the result of the given block.

    Yield Parameters:

#quote(*text) ⇒ Features Originally defined in module Features

Print given text as a quotation.

#section(*text) {|section| ... } ⇒ Object Originally defined in module Features

Create a visually separated section for the output of text elements. Like any other Element sections support all NattyUI::Features.

Examples:

ui.section do |section|
  section.h1('About Sections')
  section.space
  section.puts('Sections are areas of text elements.')
  section.puts('You can use any other feature inside such an area.')
end
# => ╭────╶╶╶
# => │ ╴╶╴╶─═══ About Sections ═══─╴╶╴╶
# => │
# => │ Sections are areas of text elements.
# => │ You can use any other feature inside such an area.
# => ╰──── ─╶╶╶

Yield Parameters:

#space(count = 1) ⇒ Features Originally defined in module Features

Print one or more space lines.

#table(**attributes) {|table| ... } ⇒ Features Originally defined in module Features

Generate and print a table. See Table for much more details about table generation.

Examples:

Draw a very simple 3x4 table with complete borders

ui.table(border: :default, border_around: true, padding: [0, 1]) do |table|
  table.add 1, 2, 3, 4
  table.add 5, 6, 7, 8
  table.add 9, 10, 11, 12
end

Options Hash (**attributes):

Yield Parameters:

  • table (Table)

    helper to define the table layout

#task(title, *text, pin: false) {|task| ... } ⇒ Object Originally defined in module Features

Generate a task section.

Yield Parameters:

  • task (Task)

    itself

#temporary {|temp| ... } ⇒ Object Originally defined in module Features

Display some temporary content. The content displayed in the block will be erased after the block ends.

Examples:

Show tempoary information

ui.temporary do
  ui.info('Information', 'This text will disappear when you pressed ENTER.')
  ui.await
end

Yield Parameters:

#warning(title, *text) {|section| ... } ⇒ Object Also known as: warn Originally defined in module Features

Create a visually separated section marked as a warning with a title for the output of text elements.

Yield Parameters:

See Also: