natty-ui 0.34.0 → 1.0.2
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/.yardopts +0 -1
- data/README.md +6 -6
- data/examples/24bit-colors.rb +9 -5
- data/examples/3bit-colors.rb +7 -7
- data/examples/8bit-colors.rb +5 -5
- data/examples/attributes.rb +2 -3
- data/examples/elements.rb +9 -6
- data/examples/examples.rb +9 -9
- data/examples/frames.rb +31 -0
- data/examples/hbars.rb +6 -3
- data/examples/info.rb +13 -10
- data/examples/key-codes.rb +8 -9
- data/examples/ls.rb +24 -22
- data/examples/named-colors.rb +4 -3
- data/examples/sections.rb +27 -17
- data/examples/select.rb +28 -0
- data/examples/sh.rb +25 -7
- data/examples/tables.rb +19 -37
- data/examples/tasks.rb +32 -22
- data/examples/vbars.rb +5 -3
- data/lib/natty-ui/dumb_progress.rb +68 -0
- data/lib/natty-ui/element.rb +64 -65
- data/lib/natty-ui/features.rb +773 -872
- data/lib/natty-ui/frame.rb +87 -0
- data/lib/natty-ui/helper/table.rb +1376 -0
- data/lib/natty-ui/margin.rb +83 -0
- data/lib/natty-ui/progress.rb +116 -149
- data/lib/natty-ui/renderer/bars.rb +93 -0
- data/lib/natty-ui/renderer/choice.rb +56 -0
- data/lib/natty-ui/renderer/dumb_choice.rb +34 -0
- data/lib/natty-ui/renderer/dumb_select.rb +60 -0
- data/lib/natty-ui/renderer/dumb_shell_runner.rb +19 -0
- data/lib/natty-ui/renderer/heading.rb +26 -0
- data/lib/natty-ui/renderer/horizontal_rule.rb +32 -0
- data/lib/natty-ui/{ls_renderer.rb → renderer/ls.rb} +15 -27
- data/lib/natty-ui/renderer/mark.rb +13 -0
- data/lib/natty-ui/renderer/quote.rb +13 -0
- data/lib/natty-ui/renderer/select.rb +63 -0
- data/lib/natty-ui/renderer/shell.rb +15 -0
- data/lib/natty-ui/renderer/shell_runner.rb +29 -0
- data/lib/natty-ui/renderer/table_renderer.rb +429 -0
- data/lib/natty-ui/section.rb +142 -41
- data/lib/natty-ui/task.rb +39 -27
- data/lib/natty-ui/temporary.rb +27 -14
- data/lib/natty-ui/utils/border.rb +139 -0
- data/lib/natty-ui/utils/str_const.rb +62 -0
- data/lib/natty-ui/utils/utils.rb +47 -0
- data/lib/natty-ui/version.rb +1 -1
- data/lib/natty-ui.rb +87 -30
- metadata +31 -28
- data/examples/cols.rb +0 -38
- data/examples/illustration.rb +0 -60
- data/examples/options.rb +0 -28
- data/examples/themes.rb +0 -51
- data/lib/natty-ui/attributes.rb +0 -593
- data/lib/natty-ui/choice.rb +0 -67
- data/lib/natty-ui/dumb_choice.rb +0 -47
- data/lib/natty-ui/dumb_options.rb +0 -64
- data/lib/natty-ui/framed.rb +0 -51
- data/lib/natty-ui/hbars_renderer.rb +0 -66
- data/lib/natty-ui/options.rb +0 -78
- data/lib/natty-ui/shell_renderer.rb +0 -91
- data/lib/natty-ui/table.rb +0 -325
- data/lib/natty-ui/table_renderer.rb +0 -165
- data/lib/natty-ui/theme.rb +0 -403
- data/lib/natty-ui/utils.rb +0 -111
- data/lib/natty-ui/vbars_renderer.rb +0 -49
- data/lib/natty-ui/width_finder.rb +0 -137
- data/natty-ui.gemspec +0 -34
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module NattyUI
|
|
4
|
-
# TODO: see HBarsRenderer - support min, max
|
|
5
|
-
module VBarsRenderer
|
|
6
|
-
class << self
|
|
7
|
-
def lines(vals, width, height, normalize, bar_width, style)
|
|
8
|
-
if style
|
|
9
|
-
bos = Ansi[*style]
|
|
10
|
-
eos = Ansi::RESET
|
|
11
|
-
end
|
|
12
|
-
if ((bar_width != :auto) && bar_width <= 1) || (vals.size * 2 > width)
|
|
13
|
-
db = '▉'
|
|
14
|
-
ds = ' '
|
|
15
|
-
el = "#{bos}#{'▔' * vals.size}#{eos}"
|
|
16
|
-
vals = vals.take(width) if vals.size > width
|
|
17
|
-
else
|
|
18
|
-
bw = (width - vals.size) / vals.size
|
|
19
|
-
bw = [bw, bar_width].min if bar_width != :auto
|
|
20
|
-
db = "#{'█' * bw} "
|
|
21
|
-
bw += 1
|
|
22
|
-
ds = ' ' * bw
|
|
23
|
-
el = "#{bos}#{'▔' * ((bw * vals.size) - 1)}#{eos}"
|
|
24
|
-
end
|
|
25
|
-
height -= 1
|
|
26
|
-
vals = normalize ? normalize!(vals, height) : adjust!(vals, height)
|
|
27
|
-
height
|
|
28
|
-
.downto(1)
|
|
29
|
-
.map { |i| "#{bos}#{vals.map { _1 < i ? ds : db }.join}#{eos}" } << el
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
def adjust!(vals, height)
|
|
35
|
-
max = vals.max.to_f
|
|
36
|
-
vals.map { ((_1 / max) * height).round }
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def normalize!(vals, height)
|
|
40
|
-
min, max = vals.minmax
|
|
41
|
-
return Array.new(vals.size, height) if min == max
|
|
42
|
-
max = (max - min).to_f
|
|
43
|
-
vals.map { (((_1 - min) / max) * height).round }
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
private_constant :VBarsRenderer
|
|
49
|
-
end
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module NattyUI
|
|
4
|
-
class WidthFinder
|
|
5
|
-
# sum, columns =
|
|
6
|
-
def self.find(columns, max_width, saving_by_column = 1)
|
|
7
|
-
new(columns, max_width, saving_by_column).find
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
# width, padding_left, padding_right =
|
|
11
|
-
def self.adjust(width, padding_left, padding_right)
|
|
12
|
-
target = (width / 2) + 1
|
|
13
|
-
padding = padding_left + padding_right
|
|
14
|
-
return width - padding, padding_left, padding_right if padding < target
|
|
15
|
-
ep = ([padding_left, padding_right].max * 100.0) / padding
|
|
16
|
-
padding = width - target
|
|
17
|
-
[
|
|
18
|
-
target,
|
|
19
|
-
padding_left = ((padding * ep) / 100).to_i,
|
|
20
|
-
padding - padding_left
|
|
21
|
-
]
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def initialize(columns, max_width, saving_by_column)
|
|
25
|
-
@max_width = max_width
|
|
26
|
-
@saving_by_column = saving_by_column
|
|
27
|
-
columns, max = fit_max_width(columns, max_width, saving_by_column)
|
|
28
|
-
default = max_width / columns.size
|
|
29
|
-
@columns = columns.map { Column.create(_1, default, max) }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def find
|
|
33
|
-
sum = @columns.sum(&:value)
|
|
34
|
-
if sum < @max_width
|
|
35
|
-
sum = expand(sum)
|
|
36
|
-
elsif @max_width < sum
|
|
37
|
-
sum = shrink(sum)
|
|
38
|
-
end
|
|
39
|
-
[sum, @columns.map(&:value)]
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
private
|
|
43
|
-
|
|
44
|
-
def fit_max_width(columns, max_width, saving_by_column)
|
|
45
|
-
ret = max_width - columns.size + 1
|
|
46
|
-
columns = columns.map { normalized(_1, max_width) }
|
|
47
|
-
return columns, ret if ret >= 1
|
|
48
|
-
saving_by_column += 1
|
|
49
|
-
until ret >= 1
|
|
50
|
-
columns.pop
|
|
51
|
-
ret += saving_by_column
|
|
52
|
-
end
|
|
53
|
-
[columns, ret]
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def normalized(value, max_width)
|
|
57
|
-
return (max_width * value).to_i if value.is_a?(Float)
|
|
58
|
-
return value unless value.is_a?(Range)
|
|
59
|
-
min = value.begin
|
|
60
|
-
max = value.end
|
|
61
|
-
min = (max_width * min).to_i if min.is_a?(Float)
|
|
62
|
-
max = (max_width * max).to_i if max.is_a?(Float)
|
|
63
|
-
(min..max)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def expand(sum)
|
|
67
|
-
max_width = [@columns.sum(&:max_val), @max_width].min
|
|
68
|
-
expandables = @columns.find_all(&:expandable?)
|
|
69
|
-
while !expandables.empty? && sum < max_width
|
|
70
|
-
curr = expandables.min_by(&:value)
|
|
71
|
-
curr.value += 1
|
|
72
|
-
sum += 1
|
|
73
|
-
expandables.delete(curr) unless curr.expandable?
|
|
74
|
-
end
|
|
75
|
-
sum
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def shrink(sum)
|
|
79
|
-
sum = shrink_elastic(sum)
|
|
80
|
-
return sum if sum <= @max_width
|
|
81
|
-
sum = shrink_harder(sum, @columns.find_all { _1.value > 1 && _1.fix? })
|
|
82
|
-
return sum if sum <= @max_width
|
|
83
|
-
sum = shrink_harder(sum, @columns.find_all { _1.value > 1 })
|
|
84
|
-
until sum <= @max_width
|
|
85
|
-
sum -= @columns.pop.value
|
|
86
|
-
sum -= @saving_by_column
|
|
87
|
-
end
|
|
88
|
-
sum
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def shrink_elastic(sum)
|
|
92
|
-
shrinkables = @columns.find_all(&:shrinkable?)
|
|
93
|
-
while !shrinkables.empty? && @max_width < sum
|
|
94
|
-
curr = shrinkables.max_by(&:min_dist)
|
|
95
|
-
curr.value -= 1
|
|
96
|
-
sum -= 1
|
|
97
|
-
shrinkables.delete(curr) unless curr.shrinkable?
|
|
98
|
-
end
|
|
99
|
-
sum
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
def shrink_harder(sum, shrinkables)
|
|
103
|
-
while !shrinkables.empty? && @max_width < sum
|
|
104
|
-
curr = shrinkables.max_by(&:value)
|
|
105
|
-
curr.value -= 1
|
|
106
|
-
sum -= 1
|
|
107
|
-
shrinkables.delete(curr) if curr.value == 1
|
|
108
|
-
end
|
|
109
|
-
sum
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
Column =
|
|
113
|
-
Struct.new(:value, :min_val, :max_val) do
|
|
114
|
-
def self.create(value, default, max)
|
|
115
|
-
case value
|
|
116
|
-
when nil
|
|
117
|
-
new(default, 1, max)
|
|
118
|
-
when Range
|
|
119
|
-
min = (value.begin || 1).clamp(1, max)
|
|
120
|
-
max = (value.end || max).clamp(1, max)
|
|
121
|
-
new(min + ((max - min) / 2), min, max)
|
|
122
|
-
else
|
|
123
|
-
new(value = value.clamp(1, max), value, value)
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
def shrinkable? = min_val < value
|
|
128
|
-
def expandable? = value < max_val
|
|
129
|
-
def fix? = min_val == max_val
|
|
130
|
-
def min_dist = value - min_val
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
private_constant :Column
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
private_constant :WidthFinder
|
|
137
|
-
end
|
data/natty-ui.gemspec
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'lib/natty-ui/version'
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = 'natty-ui'
|
|
7
|
-
spec.version = NattyUI::VERSION
|
|
8
|
-
spec.summary = <<~SUMMARY.tr("\n", ' ')
|
|
9
|
-
This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich,
|
|
10
|
-
lovely, natty user interface you like to have for your CLI.
|
|
11
|
-
SUMMARY
|
|
12
|
-
spec.description = <<~DESCRIPTION.tr("\n", ' ')
|
|
13
|
-
This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely,
|
|
14
|
-
natty user interface tool you like to have for your command line
|
|
15
|
-
applications. It contains elegant, simple and beautiful features that
|
|
16
|
-
enhance your command line interfaces functionally and aesthetically.
|
|
17
|
-
DESCRIPTION
|
|
18
|
-
|
|
19
|
-
spec.author = 'Mike Blumtritt'
|
|
20
|
-
spec.licenses = %w[MIT Ruby]
|
|
21
|
-
spec.homepage = 'https://codeberg.org/mblumtritt/natty-ui'
|
|
22
|
-
spec.metadata['source_code_uri'] = spec.homepage
|
|
23
|
-
spec.metadata['bug_tracker_uri'] = "#{spec.homepage}/issues"
|
|
24
|
-
spec.metadata['documentation_uri'] = 'https://rubydoc.info/gems/natty-ui'
|
|
25
|
-
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
26
|
-
spec.metadata['yard.run'] = 'yard'
|
|
27
|
-
|
|
28
|
-
spec.required_ruby_version = '>= 3.0'
|
|
29
|
-
spec.add_dependency 'terminal_rb', '>= 0.17.0'
|
|
30
|
-
|
|
31
|
-
spec.files = Dir['lib/**/*.rb'] + Dir['examples/*.rb']
|
|
32
|
-
spec.files << 'natty-ui.gemspec' << '.yardopts'
|
|
33
|
-
spec.extra_rdoc_files = %w[README.md]
|
|
34
|
-
end
|