natty-ui 0.30.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -3
- data/examples/cols.rb +4 -6
- data/examples/hbars.rb +2 -3
- data/examples/illustration.rb +13 -14
- data/examples/info.rb +9 -13
- data/examples/named-colors.rb +3 -5
- data/examples/tables.rb +5 -8
- data/examples/tasks.rb +14 -14
- data/examples/themes.rb +51 -0
- data/lib/natty-ui/attributes.rb +37 -38
- data/lib/natty-ui/features.rb +107 -35
- data/lib/natty-ui/hbars_renderer.rb +51 -43
- data/lib/natty-ui/progress.rb +5 -7
- data/lib/natty-ui/shell_command.rb +132 -0
- data/lib/natty-ui/shell_renderer.rb +92 -0
- data/lib/natty-ui/table.rb +47 -12
- data/lib/natty-ui/table_renderer.rb +3 -2
- data/lib/natty-ui/theme.rb +326 -282
- data/lib/natty-ui/utils.rb +3 -3
- data/lib/natty-ui/vbars_renderer.rb +1 -0
- data/lib/natty-ui/version.rb +1 -1
- data/lib/natty-ui.rb +3 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 92532c7a8557373cb4e26efb2a0bae197e1da2daddac5853ef40800c7cfbf344
|
|
4
|
+
data.tar.gz: 3da178c8e00a752802e8cf1fc4822ce6d187516fc7af5af31e03b85b123fd348
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0e41a869ea4dfde35449314f95662e2fcb837f50a76ab49cdd9e0f7cc462c5c12481e83e2eec66029468b2ef91d9f05e56f3702e0d488329c0fb3e1ee61f4431
|
|
7
|
+
data.tar.gz: da9d3545c79c99d6d09cc924a2d0dcbff6e697c264228a8ec657103c2fda7fc77583e7a5fb60db04f3a3189fd7161059e9841d155038f83cd9de88f88ee6cf65
|
data/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# NattyUI
|
|
1
|
+
# ᓚᕠᗢ NattyUI 
|
|
2
2
|
|
|
3
3
|
This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely, natty user interface you like to have for your command line applications. It contains elegant, simple and beautiful tools that enhance your command line interfaces functionally and aesthetically.
|
|
4
4
|
|
|
5
5
|
- Gem: [rubygems.org](https://rubygems.org/gems/natty-ui)
|
|
6
6
|
- Source: [codeberg.org](https://codeberg.org/mblumtritt/natty-ui)
|
|
7
|
-
- Help: [rubydoc.info](https://rubydoc.info/gems/natty-ui/
|
|
7
|
+
- Help: [rubydoc.info](https://rubydoc.info/gems/natty-ui/index)
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
@@ -18,6 +18,7 @@ This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely, nat
|
|
|
18
18
|
- headings
|
|
19
19
|
- text separators
|
|
20
20
|
- lists
|
|
21
|
+
- bar graphs
|
|
21
22
|
- various types of text blocks with automatic alignment
|
|
22
23
|
- text columns
|
|
23
24
|
- tables
|
|
@@ -27,7 +28,7 @@ This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely, nat
|
|
|
27
28
|
|
|
28
29
|
## Help
|
|
29
30
|
|
|
30
|
-
📕 See the [online help](https://rubydoc.info/gems/natty-ui/
|
|
31
|
+
📕 See the [online help](https://rubydoc.info/gems/natty-ui/index) and have a look at the [examples](./examples/) directory to learn from code.
|
|
31
32
|
|
|
32
33
|
### Run Examples
|
|
33
34
|
|
data/examples/cols.rb
CHANGED
|
@@ -17,12 +17,10 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Print Columns[/]' do
|
|
|
17
17
|
|
|
18
18
|
ui.space
|
|
19
19
|
ui.cols do |cc|
|
|
20
|
-
cc.add(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
align: :left
|
|
25
|
-
)
|
|
20
|
+
cc.add(<<~TEXT, style: 'bright_white on_blue', align: :left)
|
|
21
|
+
This is a left aligned blue column with some text but also with
|
|
22
|
+
a forced line break.
|
|
23
|
+
TEXT
|
|
26
24
|
cc.add(
|
|
27
25
|
'This is the middle red column.',
|
|
28
26
|
style: 'bright_white on_red',
|
data/examples/hbars.rb
CHANGED
|
@@ -10,9 +10,8 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Print Horizontal Bars[/]' do
|
|
|
10
10
|
ui.puts 'NattyUI can quick dump values as horizontal bars.'
|
|
11
11
|
|
|
12
12
|
ui.space
|
|
13
|
-
ui.hbars values, style: :green, normalize: true, width: 0.5
|
|
13
|
+
ui.hbars values, style: :green, normalize: true, width: 0.5, min: 10
|
|
14
14
|
ui.puts(
|
|
15
|
-
'These are the same values but [i]normalized[/i] '
|
|
16
|
-
'and printed in half width.'
|
|
15
|
+
'These are the same values but [i]normalized[/i] and printed in half width.'
|
|
17
16
|
)
|
|
18
17
|
end
|
data/examples/illustration.rb
CHANGED
|
@@ -3,16 +3,17 @@
|
|
|
3
3
|
require_relative '../lib/natty-ui'
|
|
4
4
|
|
|
5
5
|
ui.message '[b]ᓚᕠᗢ NattyUI[/b]' do
|
|
6
|
+
ui.space
|
|
6
7
|
ui.table(border: :defaulth, border_style: 'bright_blue') do |table|
|
|
7
8
|
table.add do |row|
|
|
8
9
|
row.add 'Styles'
|
|
9
|
-
row.add <<~TEXT
|
|
10
|
+
row.add <<~TEXT, eol: false
|
|
10
11
|
Support of all ANSI styles like
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
12
|
+
[b]bold[/b],
|
|
13
|
+
[i]italic[/i],
|
|
14
|
+
[u]underline[/u],
|
|
15
|
+
[inv]invert[/inv],
|
|
16
|
+
[s]strike[/s],
|
|
16
17
|
[faint]faint[/faint],
|
|
17
18
|
[double_underline]double underline[/double_underline],
|
|
18
19
|
[curly_underline]curly underline[/curly_underline],
|
|
@@ -24,10 +25,9 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b]' do
|
|
|
24
25
|
|
|
25
26
|
table.add do |row|
|
|
26
27
|
row.add 'Colors'
|
|
27
|
-
row.add <<~TEXT
|
|
28
|
-
✓ [palegreen]3/4-bit color[/fg]
|
|
29
|
-
✓ [skyblue]8-bit color[/fg]
|
|
30
|
-
✓ [tan1]NO_COLOR convention
|
|
28
|
+
row.add <<~TEXT
|
|
29
|
+
✓ [palegreen]3/4-bit color[/fg] ✓ #{NattyUI::Ansi.rainbow('Truecolor')} ✓ [tan1]NO_COLOR convention[/fg]
|
|
30
|
+
✓ [skyblue]8-bit color[/fg] ✓ [gold]Dumb terminals[/fg]
|
|
31
31
|
TEXT
|
|
32
32
|
end
|
|
33
33
|
|
|
@@ -38,7 +38,7 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b]' do
|
|
|
38
38
|
|
|
39
39
|
table.add do |row|
|
|
40
40
|
row.add 'Layout'
|
|
41
|
-
row.add <<~TEXT
|
|
41
|
+
row.add <<~TEXT
|
|
42
42
|
🎩 heading elements 📝 messages 📊 bar graphs
|
|
43
43
|
📏 horizontal rulers [blue]┼┼[/] tables
|
|
44
44
|
📋 lists [b green]✓✓[/] tasks
|
|
@@ -47,15 +47,14 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b]' do
|
|
|
47
47
|
|
|
48
48
|
table.add do |row|
|
|
49
49
|
row.add 'Asian', 'language', 'support'
|
|
50
|
-
row.add <<~TEXT
|
|
50
|
+
row.add <<~TEXT
|
|
51
51
|
[bright_green]🇨🇳 该库支持中文,日文和韩文文本!
|
|
52
52
|
[bright_green]🇯🇵 ライブラリは中国語、日本語、韓国語のテキストをサポートしています
|
|
53
53
|
[bright_green]🇰🇷 이 라이브러리는 중국어, 일본어 및 한국어 텍스트를 지원합니다
|
|
54
54
|
TEXT
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
table.columns[0].style
|
|
58
|
-
table.columns[0].width = 10
|
|
57
|
+
table.columns[0].assign(style: 'bold red', width: 8)
|
|
59
58
|
table.columns[1].padding_right = 2
|
|
60
59
|
end
|
|
61
60
|
end
|
data/examples/info.rb
CHANGED
|
@@ -3,15 +3,14 @@
|
|
|
3
3
|
require_relative '../lib/natty-ui'
|
|
4
4
|
|
|
5
5
|
def colors
|
|
6
|
-
return Terminal.colors
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
colors.join("\n")
|
|
6
|
+
return Terminal.colors if Terminal.colors < 8
|
|
7
|
+
color_bar = "#{(0..15).map { "[#{_1.to_s(16).rjust(2, '0')}]██" }.join}[/]"
|
|
8
|
+
return "#{Terminal.colors}\n#{color_bar}" unless Terminal.true_color?
|
|
9
|
+
<<~COLORS
|
|
10
|
+
true color
|
|
11
|
+
#{color_bar}
|
|
12
|
+
#{Terminal::Ansi.rainbow('████████████████████████████████')}
|
|
13
|
+
COLORS
|
|
15
14
|
end
|
|
16
15
|
|
|
17
16
|
ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Terminal Information[/]' do
|
|
@@ -22,10 +21,7 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Terminal Information[/]' do
|
|
|
22
21
|
table.add 'Input mode', Terminal.input_mode
|
|
23
22
|
table.add 'Colors', colors
|
|
24
23
|
table.add 'Screen size', Terminal.size.join(' x ')
|
|
25
|
-
|
|
26
|
-
fc.width = 14
|
|
27
|
-
fc.padding_right = 2
|
|
28
|
-
fc.align = :right
|
|
24
|
+
table.columns[0].assign(width: 15, padding_right: 2, align: :right)
|
|
29
25
|
table.columns[1].style = %i[bold bright_yellow]
|
|
30
26
|
end
|
|
31
27
|
end
|
data/examples/named-colors.rb
CHANGED
|
@@ -8,16 +8,14 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Named Colors Support[/]' do
|
|
|
8
8
|
NattyUI supports a series of named color values, such as those supported by Kitty.
|
|
9
9
|
|
|
10
10
|
INFO
|
|
11
|
-
|
|
12
11
|
ui.information('Note') do
|
|
13
12
|
ui.puts('Not all terminal emulators support true-colors.')
|
|
14
13
|
end
|
|
15
14
|
|
|
16
15
|
ui.space
|
|
17
16
|
ui.ls(
|
|
18
|
-
NattyUI::Ansi
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
.map! { "[bg_#{_1}] [/bg] [#{_1}]#{_1}[/fg]" }
|
|
17
|
+
NattyUI::Ansi.named_colors.filter_map do |name|
|
|
18
|
+
"[bg_#{name}] [/bg] [#{name}]#{name}[/fg]" unless /\d/.match?(name)
|
|
19
|
+
end
|
|
22
20
|
)
|
|
23
21
|
end
|
data/examples/tables.rb
CHANGED
|
@@ -20,14 +20,14 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Print Tables[/]' do
|
|
|
20
20
|
row.padding = [0, 2]
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
filler_text = <<~FILLER
|
|
23
|
+
filler_text = <<~FILLER
|
|
24
24
|
This is some filler text to demonstrate word-wise line breaks inside
|
|
25
25
|
a table cell. Please, just ignore this nonsense-text gently.
|
|
26
26
|
FILLER
|
|
27
27
|
table.add do |row|
|
|
28
|
-
row.add '[blue]Row 2 Col 0', filler_text, align: :right
|
|
29
|
-
row.add '[blue]Row 2 Col 1', filler_text, align: :centered
|
|
30
|
-
row.add '[blue]Row 2 Col 2', filler_text, align: :left
|
|
28
|
+
row.add '[blue]Row 2 Col 0', filler_text, eol: false, align: :right
|
|
29
|
+
row.add '[blue]Row 2 Col 1', filler_text, eol: false, align: :centered
|
|
30
|
+
row.add '[blue]Row 2 Col 2', filler_text, eol: false, align: :left
|
|
31
31
|
row.padding = [1, 2]
|
|
32
32
|
end
|
|
33
33
|
end
|
|
@@ -37,10 +37,7 @@ ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Print Tables[/]' do
|
|
|
37
37
|
table.add(*('A'..'Z').each_slice(2).map(&:join))
|
|
38
38
|
table.add(*('😀'..'😌'))
|
|
39
39
|
table.add(*(3..15).map { _1.to_s(3) })
|
|
40
|
-
table.each
|
|
41
|
-
row.align = :centered
|
|
42
|
-
row.style = :bright_yellow
|
|
43
|
-
end
|
|
40
|
+
table.each { |row| row.assign(align: :centered, style: :bright_yellow) }
|
|
44
41
|
end
|
|
45
42
|
|
|
46
43
|
ui.space
|
data/examples/tasks.rb
CHANGED
|
@@ -5,48 +5,48 @@ require_relative '../lib/natty-ui'
|
|
|
5
5
|
ui.message '[b]ᓚᕠᗢ NattyUI[/b] [i green]Tasks[/]' do
|
|
6
6
|
ui.space
|
|
7
7
|
ui.puts <<~TEXT, eol: false
|
|
8
|
-
Tasks are sections that are closed either successfully or with an error
|
|
9
|
-
If successful, their content is only displayed temporarily and can
|
|
10
|
-
other features, in particular further (sub)tasks. As an
|
|
11
|
-
elements can be “pinned” as permanent content.
|
|
8
|
+
Tasks are sections that are closed either successfully or with an error
|
|
9
|
+
message. If successful, their content is only displayed temporarily and can
|
|
10
|
+
consist of all other features, in particular further (sub)tasks. As an
|
|
11
|
+
exception to this, some elements can be “pinned” as permanent content.
|
|
12
12
|
TEXT
|
|
13
13
|
|
|
14
14
|
# to simulate some work:
|
|
15
|
-
def
|
|
16
|
-
def
|
|
15
|
+
def do_something = sleep(0.25)
|
|
16
|
+
def do_something_heavy = sleep(0.75)
|
|
17
17
|
|
|
18
18
|
ui.space
|
|
19
19
|
ui.task 'Actualize Reading List' do
|
|
20
20
|
ui.puts('This is a simple which actualizes the book reading list.')
|
|
21
21
|
|
|
22
22
|
ui.task('Connect to Library') do
|
|
23
|
-
|
|
23
|
+
do_something
|
|
24
24
|
ui.mark 'Server Found', mark: :checkmark
|
|
25
|
-
ui.task('Login...') {
|
|
25
|
+
ui.task('Login...') { do_something_heavy }
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
ui.task('Request New Books') {
|
|
28
|
+
ui.task('Request New Books') { do_something_heavy }
|
|
29
29
|
|
|
30
30
|
bar = ui.progress('Loading Books...', pin: true)
|
|
31
31
|
11.times do
|
|
32
|
-
|
|
32
|
+
do_something
|
|
33
33
|
bar.step
|
|
34
34
|
end
|
|
35
35
|
bar.ok 'Books Loaded'
|
|
36
36
|
|
|
37
|
-
ui.task('Disconnect from Library') {
|
|
37
|
+
ui.task('Disconnect from Library') { do_something_heavy }
|
|
38
38
|
|
|
39
39
|
ui.progress('Read Cover Images', max: 11) do |progress|
|
|
40
40
|
while progress.value < progress.max
|
|
41
|
-
|
|
41
|
+
do_something
|
|
42
42
|
progress.step
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
ui.pin('New Books Marked', mark: :checkmark)
|
|
47
47
|
|
|
48
|
-
ui.task('Optimize Database') {
|
|
48
|
+
ui.task('Optimize Database') { do_something_heavy }
|
|
49
49
|
|
|
50
|
-
ui.task('Remove Dust') {
|
|
50
|
+
ui.task('Remove Dust') { do_something_heavy }
|
|
51
51
|
end
|
|
52
52
|
end
|
data/examples/themes.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../lib/natty-ui'
|
|
4
|
+
|
|
5
|
+
NAMES = NattyUI::Theme.names.freeze
|
|
6
|
+
DESCR = NAMES.map { NattyUI::Theme.description(_1) }.freeze
|
|
7
|
+
ORG = NattyUI::Theme.current_name
|
|
8
|
+
|
|
9
|
+
def show(name)
|
|
10
|
+
NattyUI::Theme.use(name)
|
|
11
|
+
theme = NattyUI::Theme.current
|
|
12
|
+
ui.message(
|
|
13
|
+
"[b]ᓚᕠᗢ NattyUI [/b] [i green]Theme #{NattyUI::Theme.description(name)}[/]"
|
|
14
|
+
) do
|
|
15
|
+
ui.ls(
|
|
16
|
+
(1..6).map do |i|
|
|
17
|
+
left, right = theme.heading(i)
|
|
18
|
+
"#{left} H#{i} #{right}"
|
|
19
|
+
end
|
|
20
|
+
)
|
|
21
|
+
ui.space
|
|
22
|
+
ui.ls(theme.defined_marks.map { "#{theme.mark(_1)} #{_1}" })
|
|
23
|
+
ui.space
|
|
24
|
+
ui.ls(
|
|
25
|
+
%i[
|
|
26
|
+
task_style
|
|
27
|
+
choice_style
|
|
28
|
+
choice_current_style
|
|
29
|
+
sh_out_style
|
|
30
|
+
sh_err_style
|
|
31
|
+
].map { "#{theme.send(_1)}█ #{_1} [/]" }
|
|
32
|
+
)
|
|
33
|
+
ui.space
|
|
34
|
+
%i[section message information warning error].each do |name|
|
|
35
|
+
ui.send(name, name.capitalize)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
while true
|
|
41
|
+
selected =
|
|
42
|
+
ui.choice(*DESCR, abortable: true, selected: selected) do
|
|
43
|
+
ui.puts '[bright_green b]Select a theme:[/] [faint](Abort with [\\ESC])'
|
|
44
|
+
end
|
|
45
|
+
break unless selected
|
|
46
|
+
ui.temporary do
|
|
47
|
+
show(NAMES[selected])
|
|
48
|
+
ui.await { ui.puts '[faint][\\Press ENTER to continue...]' }
|
|
49
|
+
end
|
|
50
|
+
NattyUI::Theme.use(ORG)
|
|
51
|
+
end
|
data/lib/natty-ui/attributes.rb
CHANGED
|
@@ -3,53 +3,52 @@
|
|
|
3
3
|
module NattyUI
|
|
4
4
|
# @todo This chapter needs more documentation.
|
|
5
5
|
#
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
end
|
|
6
|
+
class Attributes
|
|
7
|
+
# @return [Attributes] updated copy of itself
|
|
8
|
+
def merge(**attributes)
|
|
9
|
+
attributes.empty? ? dup : dup._assign(attributes)
|
|
10
|
+
end
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
# @return [Attributes] itself
|
|
13
|
+
def merge!(**attributes) = attributes.empty? ? self : _assign(attributes)
|
|
14
|
+
alias assign merge!
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
# @!visibility private
|
|
17
|
+
def to_hash = _store({})
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
# @!visibility private
|
|
20
|
+
def to_h(&block) = block ? _store({}).to_h(&block) : _store({})
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
private
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
def initialize(**attributes)
|
|
25
|
+
_init
|
|
26
|
+
_assign(attributes) unless attributes.empty?
|
|
27
|
+
end
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
def _init = nil
|
|
30
|
+
def _assign(_opt) = self
|
|
31
|
+
def _store(opt) = opt
|
|
32
|
+
def as_uint(value) = [0, value.to_i].max
|
|
33
|
+
def as_nint(value) = ([0, value.to_i].max if value)
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
def as_wh(value)
|
|
36
|
+
return unless value
|
|
37
|
+
return value > 0 ? value : nil if value.is_a?(Numeric)
|
|
38
|
+
value.is_a?(Range) ? wh_from(value.begin, value.end) : nil
|
|
39
|
+
end
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
def wh_from(min, max)
|
|
42
|
+
min = normalized(min)
|
|
43
|
+
max = normalized(max)
|
|
44
|
+
return max ? (..max) : nil unless min
|
|
45
|
+
return Range.new(min, nil) unless max
|
|
46
|
+
min == max ? min : Range.new(*[min, max].minmax)
|
|
47
|
+
end
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
end
|
|
49
|
+
def normalized(value)
|
|
50
|
+
return value < 0 ? nil : value if value.is_a?(Float) && value < 1
|
|
51
|
+
(value = value.to_i) < 1 ? nil : value
|
|
53
52
|
end
|
|
54
53
|
|
|
55
54
|
module Align
|
data/lib/natty-ui/features.rb
CHANGED
|
@@ -37,12 +37,6 @@ module NattyUI
|
|
|
37
37
|
# @return [Features]
|
|
38
38
|
# itself
|
|
39
39
|
def puts(*text, **options)
|
|
40
|
-
if (ansi = Terminal.ansi?)
|
|
41
|
-
@__eol ||= "\e[m\n"
|
|
42
|
-
else
|
|
43
|
-
@__eol ||= "\n"
|
|
44
|
-
end
|
|
45
|
-
|
|
46
40
|
if options.empty?
|
|
47
41
|
bbcode = true
|
|
48
42
|
max_width = Terminal.columns
|
|
@@ -107,12 +101,14 @@ module NattyUI
|
|
|
107
101
|
*text,
|
|
108
102
|
limit: max_width,
|
|
109
103
|
bbcode: bbcode,
|
|
110
|
-
ansi: ansi
|
|
104
|
+
ansi: Terminal.ansi?,
|
|
111
105
|
ignore_newline: ignore_newline
|
|
112
106
|
)
|
|
107
|
+
tail = options[:tail] and lines = lines.to_a.last(tail)
|
|
108
|
+
@__eol ||= Terminal.ansi? ? "\e[m\n" : "\n"
|
|
113
109
|
|
|
114
110
|
if (align = options[:align]).nil?
|
|
115
|
-
lines.each do |line|
|
|
111
|
+
lines.each do |line, _|
|
|
116
112
|
Terminal.print(prefix, line, suffix, @__eol, bbcode: false)
|
|
117
113
|
@lines_written += 1
|
|
118
114
|
prefix, prefix_next = prefix_next, nil if prefix_next
|
|
@@ -489,10 +485,11 @@ module NattyUI
|
|
|
489
485
|
# ui.hbars 1..10, style: :blue, width: 0.5
|
|
490
486
|
#
|
|
491
487
|
# @param values [#to_a, Array<Numeric>] values to print
|
|
492
|
-
# @param
|
|
488
|
+
# @param min [#to_f] start value
|
|
489
|
+
# @param max [#to_f] end value
|
|
493
490
|
# @param normalize [true, false] whether the values should be normalized
|
|
494
|
-
# @param
|
|
495
|
-
# @param
|
|
491
|
+
# @param text [true, false] whether the values should be printed too
|
|
492
|
+
# @param width [:auto, :min, Integer] with of each bar
|
|
496
493
|
# @param style [Symbol, Array<Symbol>, nil] bar drawing style
|
|
497
494
|
# @param text_style [Symbol, Array<Symbol>, nil] text style
|
|
498
495
|
#
|
|
@@ -501,8 +498,10 @@ module NattyUI
|
|
|
501
498
|
# @return (see puts)
|
|
502
499
|
def hbars(
|
|
503
500
|
values,
|
|
504
|
-
|
|
501
|
+
min: nil,
|
|
502
|
+
max: nil,
|
|
505
503
|
normalize: false,
|
|
504
|
+
text: true,
|
|
506
505
|
width: :auto,
|
|
507
506
|
style: nil,
|
|
508
507
|
text_style: nil
|
|
@@ -512,12 +511,9 @@ module NattyUI
|
|
|
512
511
|
raise(ArgumentError, 'values can not be negative')
|
|
513
512
|
end
|
|
514
513
|
style = text_style = nil unless Terminal.ansi?
|
|
515
|
-
|
|
516
|
-
if
|
|
517
|
-
|
|
518
|
-
else
|
|
519
|
-
puts(*HBarsRenderer.lines_bars_only(values, size, normalize, style))
|
|
520
|
-
end
|
|
514
|
+
renderer = HBarsRenderer.new(values, min, max)
|
|
515
|
+
renderer.with_text(text_style) if text
|
|
516
|
+
puts(*renderer.lines(Utils.as_size(3..columns, width), style, normalize))
|
|
521
517
|
end
|
|
522
518
|
|
|
523
519
|
# Dynamically display a task progress.
|
|
@@ -582,6 +578,76 @@ module NattyUI
|
|
|
582
578
|
block ? __with(progress, &block) : progress
|
|
583
579
|
end
|
|
584
580
|
|
|
581
|
+
# Execute a program.
|
|
582
|
+
#
|
|
583
|
+
# @example Execute a simple command
|
|
584
|
+
# ui.sh 'ls'
|
|
585
|
+
#
|
|
586
|
+
# @example Execute a command wih arguments
|
|
587
|
+
# ret = ui.sh('curl', '--version')
|
|
588
|
+
# raise('Curl not found') unless ret&.success?
|
|
589
|
+
#
|
|
590
|
+
# @example Execute shell commands
|
|
591
|
+
# ui.sh "mkdir 'test' && cd 'test'"
|
|
592
|
+
#
|
|
593
|
+
# @example Execute a command with environment variables
|
|
594
|
+
# ui.sh({'ENV' => 'test'}, 'rails')
|
|
595
|
+
#
|
|
596
|
+
# @see run
|
|
597
|
+
#
|
|
598
|
+
# @param cmd [String]
|
|
599
|
+
# command and optional arguments
|
|
600
|
+
# @param shell [true,false]
|
|
601
|
+
# whether a seperate shell should be created
|
|
602
|
+
# @param input [IO,#to_io,Array,#each,#to_a,#to_s,nil]
|
|
603
|
+
# input piped to the command
|
|
604
|
+
# @param preserve_spaces [true,false]
|
|
605
|
+
# whether the spaces and tabs of the output should be preserve
|
|
606
|
+
# @return [Process::Status]
|
|
607
|
+
# when command was executed
|
|
608
|
+
# @return [nil]
|
|
609
|
+
# in error case (like command not found)
|
|
610
|
+
def sh(*cmd, shell: false, input: nil, preserve_spaces: false)
|
|
611
|
+
ShellRenderer.sh(self, cmd, shell, input, preserve_spaces)
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
# Execute a shell program and return output. Limit the lines displayed.
|
|
615
|
+
#
|
|
616
|
+
# @example Capture output and error
|
|
617
|
+
# status, out, err = ui.run('ls ./ && ls ./this_does_not_exist')
|
|
618
|
+
# # => #<Process::Status: pid 25562 exit 1>
|
|
619
|
+
# # => [...] # the output of first `ls`
|
|
620
|
+
# # => ["ls: ./this_does_not_exist: No such file or directory"]
|
|
621
|
+
#
|
|
622
|
+
# @see sh
|
|
623
|
+
#
|
|
624
|
+
# @param cmd (see sh)
|
|
625
|
+
# @param shell (see sh)
|
|
626
|
+
# @param input (see sh)
|
|
627
|
+
# @param preserve_spaces (see sh)
|
|
628
|
+
# @param max_lines [Integer] limit of displayed lines
|
|
629
|
+
# @return [[Process::Status, Array<String>, Array<String>]]
|
|
630
|
+
# process status, output and error output when command was executed
|
|
631
|
+
# @return [nil] in error case (like command not found)
|
|
632
|
+
def run(
|
|
633
|
+
*cmd,
|
|
634
|
+
shell: false,
|
|
635
|
+
input: nil,
|
|
636
|
+
preserve_spaces: false,
|
|
637
|
+
max_lines: 10
|
|
638
|
+
)
|
|
639
|
+
result =
|
|
640
|
+
ShellRenderer.run(
|
|
641
|
+
self,
|
|
642
|
+
cmd,
|
|
643
|
+
shell,
|
|
644
|
+
input,
|
|
645
|
+
preserve_spaces,
|
|
646
|
+
max_lines.clamp(1, Terminal.rows)
|
|
647
|
+
)
|
|
648
|
+
result if result[0]
|
|
649
|
+
end
|
|
650
|
+
|
|
585
651
|
#
|
|
586
652
|
# @!endgroup
|
|
587
653
|
#
|
|
@@ -734,7 +800,7 @@ module NattyUI
|
|
|
734
800
|
# key code/s a user can input to return negative resault
|
|
735
801
|
#
|
|
736
802
|
# @return [true, false]
|
|
737
|
-
#
|
|
803
|
+
# whether the user inputs a positive result
|
|
738
804
|
#
|
|
739
805
|
# @overload await(yes: 'Enter', no: 'Esc', &block)
|
|
740
806
|
# @param yes [String, Enumerable<String>]
|
|
@@ -746,7 +812,7 @@ module NattyUI
|
|
|
746
812
|
# temporary displayed section (section will be erased after input)
|
|
747
813
|
#
|
|
748
814
|
# @return [true, false]
|
|
749
|
-
#
|
|
815
|
+
# whether the user inputs a positive result
|
|
750
816
|
# @return nil
|
|
751
817
|
# in error case
|
|
752
818
|
#
|
|
@@ -815,7 +881,7 @@ module NattyUI
|
|
|
815
881
|
# r: 'Rio',
|
|
816
882
|
# abortable: true
|
|
817
883
|
# ) { ui.puts 'Which terminal emulator do you like?' }
|
|
818
|
-
# # =>
|
|
884
|
+
# # => whether the user selected: :k, :i, :g, :t, :r
|
|
819
885
|
# # => nil, when the user aborted
|
|
820
886
|
# @param [#to_s] choices
|
|
821
887
|
# one or more alternatives to select from
|
|
@@ -963,36 +1029,42 @@ module NattyUI
|
|
|
963
1029
|
end
|
|
964
1030
|
|
|
965
1031
|
dir = __dir__
|
|
966
|
-
autoload :Choice, "#{dir}/choice.rb"
|
|
967
|
-
autoload :DumbChoice, "#{dir}/dumb_choice.rb"
|
|
968
|
-
autoload :DumbOptions, "#{dir}/dumb_options.rb"
|
|
969
|
-
autoload :CompactLSRenderer, "#{dir}/ls_renderer.rb"
|
|
970
1032
|
autoload :Framed, "#{dir}/framed.rb"
|
|
971
|
-
autoload :HBarsRenderer, "#{dir}/hbars_renderer.rb"
|
|
972
|
-
autoload :LSRenderer, "#{dir}/ls_renderer.rb"
|
|
973
|
-
autoload :Options, "#{dir}/options.rb"
|
|
974
|
-
autoload :Progress, "#{dir}/progress.rb"
|
|
975
|
-
autoload :DumbProgress, "#{dir}/progress.rb"
|
|
976
1033
|
autoload :Section, "#{dir}/section.rb"
|
|
977
1034
|
autoload :Table, "#{dir}/table.rb"
|
|
978
1035
|
autoload :Task, "#{dir}/task.rb"
|
|
979
1036
|
autoload :Temporary, "#{dir}/temporary.rb"
|
|
980
1037
|
autoload :Theme, "#{dir}/theme.rb"
|
|
981
1038
|
autoload :Utils, "#{dir}/utils.rb"
|
|
1039
|
+
|
|
1040
|
+
autoload :Choice, "#{dir}/choice.rb"
|
|
1041
|
+
autoload :DumbChoice, "#{dir}/dumb_choice.rb"
|
|
1042
|
+
autoload :Options, "#{dir}/options.rb"
|
|
1043
|
+
autoload :DumbOptions, "#{dir}/dumb_options.rb"
|
|
1044
|
+
autoload :Progress, "#{dir}/progress.rb"
|
|
1045
|
+
autoload :DumbProgress, "#{dir}/progress.rb"
|
|
1046
|
+
|
|
1047
|
+
autoload :CompactLSRenderer, "#{dir}/ls_renderer.rb"
|
|
1048
|
+
autoload :HBarsRenderer, "#{dir}/hbars_renderer.rb"
|
|
1049
|
+
autoload :LSRenderer, "#{dir}/ls_renderer.rb"
|
|
1050
|
+
autoload :ShellRenderer, "#{dir}/shell_renderer.rb"
|
|
982
1051
|
autoload :VBarsRenderer, "#{dir}/vbars_renderer.rb"
|
|
983
1052
|
|
|
984
1053
|
private_constant(
|
|
1054
|
+
:Framed,
|
|
1055
|
+
:Utils,
|
|
1056
|
+
# -
|
|
985
1057
|
:Choice,
|
|
986
1058
|
:DumbChoice,
|
|
1059
|
+
:Options,
|
|
987
1060
|
:DumbOptions,
|
|
1061
|
+
:Progress,
|
|
1062
|
+
:DumbProgress,
|
|
1063
|
+
# -
|
|
988
1064
|
:CompactLSRenderer,
|
|
989
|
-
:Framed,
|
|
990
1065
|
:HBarsRenderer,
|
|
991
1066
|
:LSRenderer,
|
|
992
|
-
:
|
|
993
|
-
:Progress,
|
|
994
|
-
:DumbProgress,
|
|
995
|
-
:Utils,
|
|
1067
|
+
:ShellRenderer,
|
|
996
1068
|
:VBarsRenderer
|
|
997
1069
|
)
|
|
998
1070
|
end
|