Class: NattyUI::Wrapper
- Inherits:
-
Object
- Object
- NattyUI::Wrapper
- Includes:
- Features
- Defined in:
- lib/natty-ui/wrapper.rb,
lib/natty-ui/wrapper/ask.rb,
lib/natty-ui/wrapper/task.rb,
lib/natty-ui/wrapper/query.rb,
lib/natty-ui/wrapper/quote.rb,
lib/natty-ui/wrapper/table.rb,
lib/natty-ui/wrapper/framed.rb,
lib/natty-ui/wrapper/element.rb,
lib/natty-ui/wrapper/heading.rb,
lib/natty-ui/wrapper/message.rb,
lib/natty-ui/wrapper/request.rb,
lib/natty-ui/wrapper/section.rb,
lib/natty-ui/wrapper/progress.rb,
lib/natty-ui/wrapper/horizontal_rule.rb,
lib/natty-ui/wrapper/list_in_columns.rb
Overview
Helper class to wrap an output stream and implement all Features.
Defined Under Namespace
Classes: Ask, Element, Framed, Heading, HorizontalRule, ListInColumns, Message, Pairs, Progress, Query, Quote, Request, Section, Table, Task
Instance Attribute Summary collapse
-
#ansi? ⇒ Boolean
readonly
Whether ANSI is supported.
-
#screen_columns ⇒ Integer
(also: #available_width, #rcol)
readonly
Number of screen columns.
-
#screen_rows ⇒ Integer
readonly
Number of screen rows.
-
#screen_size ⇒ [Integer, Integer]
readonly
Screen size as rows and columns.
-
#stream ⇒ IO
readonly
IO stream used for output.
Tool functions collapse
-
#cls ⇒ Wrapper
Clear Screen.
-
#page {|Wrapper| ... } ⇒ Object
Saves current screen, deletes all screen content and moves the cursor to the top left screen corner.
-
#print(*args, **options) ⇒ Wrapper
Print given arguments to the output stream.
-
#puts(*args, **options) ⇒ Wrapper
Print given arguments line-wise to the output stream.
-
#space(lines = 1) ⇒ Wrapper
Add at least one empty line.
-
#temporary ⇒ Object
Resets the part of the screen written below the current output line when the given block ended.
Instance Method Summary collapse
-
#animate(..., animation: :default) ⇒ Wrapper::Section, Wrapper
included
from Features
Print given arguments line-wise with animation.
-
#ask(question, yes: "jotsyd\r\n", no: 'n') ⇒ Boolean?
included
from Features
Ask a yes/no question from user.
-
#completed(title, *args) {|message| ... } ⇒ Object, Wrapper::Message
(also: #done, #ok)
included
from Features
Creates a completion message section with a highlighted
title
and prints given additional arguments as lines into the section. -
#error(title, *args) {|message| ... } ⇒ Object, Wrapper::Message
(also: #err)
included
from Features
Creates a error message section with a highlighted
title
and prints given additional arguments as lines into the section. -
#failed(title, *args) {|message| ... } ⇒ Object, Wrapper::Message
included
from Features
Creates a failure message section with a highlighted
title
and prints given additional arguments as lines into the section. -
#framed(*args, type: :rounded) {|framed| ... } ⇒ Object, Wrapper::Framed
included
from Features
Creates frame-enclosed section with a highlighted
title
and prints given additional arguments as lines into the section. -
#h1(title) ⇒ Wrapper::Section, Wrapper
included
from Features
Prints a H1 title.
-
#h2(title) ⇒ Wrapper::Section, Wrapper
included
from Features
Prints a H2 title.
-
#h3(title) ⇒ Wrapper::Section, Wrapper
included
from Features
Prints a H3 title.
-
#h4(title) ⇒ Wrapper::Section, Wrapper
included
from Features
Prints a H4 title.
-
#h5(title) ⇒ Wrapper::Section, Wrapper
included
from Features
Prints a H5 title.
-
#hr(symbol = '─') ⇒ Wrapper::Section, Wrapper
included
from Features
Print a horizontal rule.
-
#information(title, *args) {|message| ... } ⇒ Object, Wrapper::Message
(also: #info)
included
from Features
Creates a informational message section with a highlighted
title
and prints given additional arguments as lines into the section. -
#ls(*args, compact: true, glyph: nil) ⇒ Wrapper, Wrapper::Element
included
from Features
Print items of a given list as columns.
-
#message(title, *args, glyph: :default) {|message| ... } ⇒ Object, Wrapper::Message
(also: #msg)
included
from Features
Creates a section with a highlighted
title
and prints given additional arguments as lines into the section. -
#pairs(seperator = ': ', **kwargs) ⇒ Wrapper::Section, Wrapper
included
from Features
Table-like display of key/value pairs.
-
#progress(title, max_value: nil, spinner: :pulse) ⇒ Wrapper::Progress
included
from Features
Creates progress element implementing additional ProgressAttributes.
-
#query(question, *choices, result: :char, display: :list, **kw_choices) ⇒ Char, ...
included
from Features
Request a choice from user.
-
#quote(*args) {|section| ... } ⇒ Object, Wrapper::Quote
included
from Features
Creates a quotation section and prints given arguments as lines into the section.
-
#request(question, password: false) ⇒ String?
included
from Features
Request user input.
-
#section(*args, prefix: ' ', suffix: ' ') {|section| ... } ⇒ Object, Wrapper::Section
(also: #sec)
included
from Features
Creates a default section and prints given arguments as lines into the section.
-
#table(*table, type: :simple, expand: false) {|Table| ... } ⇒ Wrapper::Section, Wrapper
included
from Features
Table view of data.
-
#task(title, *args) {|task| ... } ⇒ Object, Wrapper::Task
included
from Features
Creates task section implementing additional ProgressAttributes.
-
#warning(title, *args) {|message| ... } ⇒ Object, Wrapper::Message
(also: #warn)
included
from Features
Creates a warning message section with a highlighted
title
and prints given additional arguments as lines into the section.
Instance Attribute Details
#ansi? ⇒ Boolean (readonly)
Returns whether ANSI is supported.
32 |
# File 'lib/natty-ui/wrapper.rb', line 32 def ansi? = false |
#screen_columns ⇒ Integer (readonly) Also known as: available_width, rcol
Returns number of screen columns.
44 |
# File 'lib/natty-ui/wrapper.rb', line 44 def screen_columns = screen_size[1] |
#screen_rows ⇒ Integer (readonly)
Returns number of screen rows.
40 |
# File 'lib/natty-ui/wrapper.rb', line 40 def screen_rows = screen_size[0] |
#screen_size ⇒ [Integer, Integer] (readonly)
Returns screen size as rows and columns.
36 |
# File 'lib/natty-ui/wrapper.rb', line 36 def screen_size = (@screen_size ||= determine_screen_size) |
#stream ⇒ IO (readonly)
Returns IO stream used for output.
28 29 30 |
# File 'lib/natty-ui/wrapper.rb', line 28 def stream @stream end |
Instance Method Details
#animate(..., animation: :default) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Print given arguments line-wise with animation.
#ask(question, yes: "jotsyd\r\n", no: 'n') ⇒ Boolean? Originally defined in module Features
Ask a yes/no question from user.
The defaults for yes
and no
will work for
Afrikaans, Dutch, English, French, German, Italian, Polish, Portuguese,
Romanian, Spanish and Swedish.
The default for yes
includes ENTER
and RETURN
key
#completed(title, *args) {|message| ... } ⇒ Object, Wrapper::Message Also known as: done, ok Originally defined in module Features
Creates a completion message section with a highlighted title
and
prints given additional arguments as lines into the section.
When used for a #task section it closes this section with status :ok
.
#error(title, *args) {|message| ... } ⇒ Object, Wrapper::Message Also known as: err Originally defined in module Features
Creates a error message section with a highlighted title
and
prints given additional arguments as lines into the section.
#failed(title, *args) {|message| ... } ⇒ Object, Wrapper::Message Originally defined in module Features
Creates a failure message section with a highlighted title
and
prints given additional arguments as lines into the section.
When used for a #task section it closes this section with status
:failed
.
#framed(*args, type: :rounded) {|framed| ... } ⇒ Object, Wrapper::Framed Originally defined in module Features
Creates frame-enclosed section with a highlighted title
and
prints given additional arguments as lines into the section.
When no block is given, the section must be closed, see Wrapper::Element#close.
#h1(title) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Prints a H1 title.
#h2(title) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Prints a H2 title.
#h3(title) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Prints a H3 title.
#h4(title) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Prints a H4 title.
#h5(title) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Prints a H5 title.
#hr(symbol = '─') ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Print a horizontal rule
#information(title, *args) {|message| ... } ⇒ Object, Wrapper::Message Also known as: info Originally defined in module Features
Creates a informational message section with a highlighted title
and
prints given additional arguments as lines into the section.
#ls(*args, compact: true, glyph: nil) ⇒ Wrapper, Wrapper::Element Originally defined in module Features
Print items of a given list as columns. In the default compact format columns may have diffrent widths and the list items are ordered column-wise. The non-compact format prints all columns in same width and order the list items row-wise.
#message(title, *args, glyph: :default) {|message| ... } ⇒ Object, Wrapper::Message Also known as: msg Originally defined in module Features
Creates a section with a highlighted title
and prints given additional
arguments as lines into the section.
#page {|Wrapper| ... } ⇒ Object
The screen manipulation is only available in ANSI mode see #ansi?
Saves current screen, deletes all screen content and moves the cursor to the top left screen corner. It restores the screen after the block.
106 107 108 109 110 |
# File 'lib/natty-ui/wrapper.rb', line 106 def page block_given? ? yield(self) : self ensure @stream.flush end |
#pairs(seperator = ': ', **kwargs) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Table-like display of key/value pairs.
#print(*args, **options) ⇒ Wrapper
Print given arguments to the output stream.
67 68 69 70 71 72 73 74 75 |
# File 'lib/natty-ui/wrapper.rb', line 67 def print(*args, **) pprint(args, ) do |line| @stream.print(line) @lines_written += 1 end @lines_written -= 1 @stream.flush self end |
#progress(title, max_value: nil, spinner: :pulse) ⇒ Wrapper::Progress Originally defined in module Features
Creates progress element implementing additional ProgressAttributes.
When a max_value
is given, the progress will by displayed as a bar.
Otherwise the spinner
is used for a little animation.
When no pre-defined spinner is specified then spinner will be used char-wise as a string for the progress animation.
A progress element has additional states and can be closed with #completed or #failed.
#puts(*args, **options) ⇒ Wrapper
Print given arguments line-wise to the output stream.
53 54 55 56 57 58 59 60 |
# File 'lib/natty-ui/wrapper.rb', line 53 def puts(*args, **) pprint(args, ) do |line| @stream.puts(line) @lines_written += 1 end @stream.flush self end |
#query(question, *choices, result: :char, display: :list, **kw_choices) ⇒ Char, ... Originally defined in module Features
Request a choice from user.
#quote(*args) {|section| ... } ⇒ Object, Wrapper::Quote Originally defined in module Features
Creates a quotation section and prints given arguments as lines into the section.
#request(question, password: false) ⇒ String? Originally defined in module Features
Request user input.
#section(*args, prefix: ' ', suffix: ' ') {|section| ... } ⇒ Object, Wrapper::Section Also known as: sec Originally defined in module Features
Creates a default section and prints given arguments as lines into the section.
#space(lines = 1) ⇒ Wrapper
Add at least one empty line
81 82 83 84 85 86 |
# File 'lib/natty-ui/wrapper.rb', line 81 def space(lines = 1) lines = [1, lines.to_i].max (@stream << ("\n" * lines)).flush @lines_written += lines self end |
#table(*args, type: simple, expand: false) ⇒ Wrapper::Section, Wrapper #table(type: simple, expand: false) ⇒ Wrapper::Section, Wrapper Originally defined in module Features
Table view of data.
Defined values for type
are
:double, :heavy, :semi, :simple
#task(title, *args) {|task| ... } ⇒ Object, Wrapper::Task Originally defined in module Features
Creates task section implementing additional ProgressAttributes.
A task section has additional states and can be closed with #completed or #failed.
#temporary ⇒ Proc #temporary {|Wrapper| ... } ⇒ Object
The screen manipulation is only available in ANSI mode see #ansi?
Resets the part of the screen written below the current output line when the given block ended.
129 130 131 132 133 134 135 136 137 |
# File 'lib/natty-ui/wrapper.rb', line 129 def temporary func = temp_func return func unless block_given? begin yield(self) ensure func.call end end |
#warning(title, *args) {|message| ... } ⇒ Object, Wrapper::Message Also known as: warn Originally defined in module Features
Creates a warning message section with a highlighted title
and
prints given additional arguments as lines into the section.