tty 0.1.2 → 0.1.3
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/.travis.yml +4 -3
- data/CHANGELOG.md +5 -0
- data/README.md +42 -306
- data/lib/tty/{text → shell}/distance.rb +5 -27
- data/lib/tty/shell/suggestion.rb +42 -22
- data/lib/tty/version.rb +1 -1
- data/lib/tty.rb +2 -42
- data/spec/tty/{text → shell}/distance/distance_spec.rb +4 -4
- data/spec/tty/shell/distance/initialize_spec.rb +14 -0
- data/tty.gemspec +4 -3
- metadata +37 -269
- data/benchmarks/table.rb +0 -40
- data/lib/tty/table/border/ascii.rb +0 -32
- data/lib/tty/table/border/null.rb +0 -37
- data/lib/tty/table/border/row_line.rb +0 -18
- data/lib/tty/table/border/unicode.rb +0 -32
- data/lib/tty/table/border.rb +0 -222
- data/lib/tty/table/border_dsl.rb +0 -251
- data/lib/tty/table/border_options.rb +0 -53
- data/lib/tty/table/column_set.rb +0 -121
- data/lib/tty/table/columns.rb +0 -170
- data/lib/tty/table/error.rb +0 -18
- data/lib/tty/table/field.rb +0 -139
- data/lib/tty/table/header.rb +0 -133
- data/lib/tty/table/indentation.rb +0 -52
- data/lib/tty/table/operation/alignment.rb +0 -107
- data/lib/tty/table/operation/alignment_set.rb +0 -77
- data/lib/tty/table/operation/escape.rb +0 -30
- data/lib/tty/table/operation/filter.rb +0 -34
- data/lib/tty/table/operation/padding.rb +0 -95
- data/lib/tty/table/operation/truncation.rb +0 -55
- data/lib/tty/table/operation/wrapped.rb +0 -58
- data/lib/tty/table/operations.rb +0 -69
- data/lib/tty/table/orientation/horizontal.rb +0 -48
- data/lib/tty/table/orientation/vertical.rb +0 -38
- data/lib/tty/table/orientation.rb +0 -57
- data/lib/tty/table/padder.rb +0 -180
- data/lib/tty/table/renderer/ascii.rb +0 -16
- data/lib/tty/table/renderer/basic.rb +0 -294
- data/lib/tty/table/renderer/color.rb +0 -12
- data/lib/tty/table/renderer/unicode.rb +0 -21
- data/lib/tty/table/renderer.rb +0 -101
- data/lib/tty/table/row.rb +0 -217
- data/lib/tty/table/transformation.rb +0 -39
- data/lib/tty/table/validatable.rb +0 -64
- data/lib/tty/table.rb +0 -476
- data/lib/tty/text/truncation.rb +0 -99
- data/lib/tty/text/wrapping.rb +0 -118
- data/lib/tty/text.rb +0 -98
- data/spec/tty/table/access_spec.rb +0 -86
- data/spec/tty/table/add_row_spec.rb +0 -28
- data/spec/tty/table/border/ascii/rendering_spec.rb +0 -90
- data/spec/tty/table/border/new_spec.rb +0 -27
- data/spec/tty/table/border/null/rendering_spec.rb +0 -130
- data/spec/tty/table/border/options/from_spec.rb +0 -38
- data/spec/tty/table/border/options/new_spec.rb +0 -14
- data/spec/tty/table/border/unicode/rendering_spec.rb +0 -63
- data/spec/tty/table/border_options/new_spec.rb +0 -20
- data/spec/tty/table/border_options/update_spec.rb +0 -18
- data/spec/tty/table/column_set/extract_widths_spec.rb +0 -15
- data/spec/tty/table/column_set/total_width_spec.rb +0 -15
- data/spec/tty/table/column_set/widths_from_spec.rb +0 -51
- data/spec/tty/table/columns/enforce_spec.rb +0 -68
- data/spec/tty/table/columns/widths_spec.rb +0 -35
- data/spec/tty/table/data_spec.rb +0 -14
- data/spec/tty/table/each_spec.rb +0 -41
- data/spec/tty/table/each_with_index_spec.rb +0 -57
- data/spec/tty/table/empty_spec.rb +0 -23
- data/spec/tty/table/eql_spec.rb +0 -34
- data/spec/tty/table/field/equality_spec.rb +0 -51
- data/spec/tty/table/field/length_spec.rb +0 -21
- data/spec/tty/table/field/lines_spec.rb +0 -21
- data/spec/tty/table/field/new_spec.rb +0 -29
- data/spec/tty/table/field/width_spec.rb +0 -23
- data/spec/tty/table/filter_spec.rb +0 -23
- data/spec/tty/table/header/call_spec.rb +0 -30
- data/spec/tty/table/header/height_spec.rb +0 -27
- data/spec/tty/table/header/new_spec.rb +0 -25
- data/spec/tty/table/header/set_spec.rb +0 -20
- data/spec/tty/table/header/to_ary_spec.rb +0 -14
- data/spec/tty/table/header_spec.rb +0 -13
- data/spec/tty/table/indentation/insert_indent_spec.rb +0 -27
- data/spec/tty/table/initialize_spec.rb +0 -88
- data/spec/tty/table/operation/alignment/format_spec.rb +0 -47
- data/spec/tty/table/operation/alignment/new_spec.rb +0 -31
- data/spec/tty/table/operation/alignment_set/call_spec.rb +0 -39
- data/spec/tty/table/operation/alignment_set/each_spec.rb +0 -17
- data/spec/tty/table/operation/alignment_set/new_spec.rb +0 -27
- data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +0 -14
- data/spec/tty/table/operation/escape/call_spec.rb +0 -16
- data/spec/tty/table/operation/filter/call_spec.rb +0 -17
- data/spec/tty/table/operation/truncation/call_spec.rb +0 -32
- data/spec/tty/table/operation/truncation/truncate_spec.rb +0 -22
- data/spec/tty/table/operation/wrapped/call_spec.rb +0 -33
- data/spec/tty/table/operation/wrapped/wrap_spec.rb +0 -23
- data/spec/tty/table/operations/new_spec.rb +0 -30
- data/spec/tty/table/options_spec.rb +0 -25
- data/spec/tty/table/orientation_spec.rb +0 -145
- data/spec/tty/table/padder/parse_spec.rb +0 -45
- data/spec/tty/table/padder/to_s_spec.rb +0 -14
- data/spec/tty/table/padding_spec.rb +0 -120
- data/spec/tty/table/properties_spec.rb +0 -25
- data/spec/tty/table/render_spec.rb +0 -63
- data/spec/tty/table/render_with_spec.rb +0 -106
- data/spec/tty/table/renderer/ascii/indentation_spec.rb +0 -41
- data/spec/tty/table/renderer/ascii/padding_spec.rb +0 -61
- data/spec/tty/table/renderer/ascii/render_spec.rb +0 -68
- data/spec/tty/table/renderer/ascii/resizing_spec.rb +0 -114
- data/spec/tty/table/renderer/ascii/separator_spec.rb +0 -28
- data/spec/tty/table/renderer/basic/alignment_spec.rb +0 -88
- data/spec/tty/table/renderer/basic/coloring_spec.rb +0 -46
- data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +0 -28
- data/spec/tty/table/renderer/basic/filter_spec.rb +0 -53
- data/spec/tty/table/renderer/basic/indentation_spec.rb +0 -48
- data/spec/tty/table/renderer/basic/multiline_content_spec.rb +0 -135
- data/spec/tty/table/renderer/basic/new_spec.rb +0 -22
- data/spec/tty/table/renderer/basic/options_spec.rb +0 -48
- data/spec/tty/table/renderer/basic/padding_spec.rb +0 -52
- data/spec/tty/table/renderer/basic/render_spec.rb +0 -57
- data/spec/tty/table/renderer/basic/resizing_spec.rb +0 -96
- data/spec/tty/table/renderer/basic/separator_spec.rb +0 -43
- data/spec/tty/table/renderer/basic/truncation_spec.rb +0 -35
- data/spec/tty/table/renderer/basic/wrapping_spec.rb +0 -40
- data/spec/tty/table/renderer/basic_spec.rb +0 -0
- data/spec/tty/table/renderer/border_spec.rb +0 -104
- data/spec/tty/table/renderer/render_spec.rb +0 -36
- data/spec/tty/table/renderer/select_spec.rb +0 -22
- data/spec/tty/table/renderer/style_spec.rb +0 -72
- data/spec/tty/table/renderer/unicode/indentation_spec.rb +0 -41
- data/spec/tty/table/renderer/unicode/padding_spec.rb +0 -61
- data/spec/tty/table/renderer/unicode/render_spec.rb +0 -68
- data/spec/tty/table/renderer/unicode/separator_spec.rb +0 -26
- data/spec/tty/table/renderer_spec.rb +0 -19
- data/spec/tty/table/rotate_spec.rb +0 -86
- data/spec/tty/table/row/access_spec.rb +0 -25
- data/spec/tty/table/row/call_spec.rb +0 -41
- data/spec/tty/table/row/data_spec.rb +0 -26
- data/spec/tty/table/row/each_spec.rb +0 -31
- data/spec/tty/table/row/equality_spec.rb +0 -73
- data/spec/tty/table/row/height_spec.rb +0 -27
- data/spec/tty/table/row/new_spec.rb +0 -41
- data/spec/tty/table/row/to_ary_spec.rb +0 -14
- data/spec/tty/table/to_s_spec.rb +0 -63
- data/spec/tty/table/transformation/extract_tuples_spec.rb +0 -35
- data/spec/tty/table/validatable/validate_options_spec.rb +0 -33
- data/spec/tty/table/validatable_spec.rb +0 -32
- data/spec/tty/text/distance/initialize_spec.rb +0 -14
- data/spec/tty/text/distance_spec.rb +0 -11
- data/spec/tty/text/truncate_spec.rb +0 -12
- data/spec/tty/text/truncation/initialize_spec.rb +0 -29
- data/spec/tty/text/truncation/truncate_spec.rb +0 -94
- data/spec/tty/text/wrap_spec.rb +0 -13
- data/spec/tty/text/wrapping/initialize_spec.rb +0 -25
- data/spec/tty/text/wrapping/wrap_spec.rb +0 -97
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad0efdc965bc3c933d7cb71d0bf75a5be2c3bf45
|
4
|
+
data.tar.gz: f4e7b12fbe37bce6cc467730a472112ca673ae36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0045cfb5de1675a6485681b047f6cf5e785e1758b6fca908bf84a943d199f8ed2e77ba8cb0239f8dc810f07cea551ae565b485849c395e1e9fa7ed1de62bcb79
|
7
|
+
data.tar.gz: 103f2d9010887f3b658f97f901df315deebe80f54248950818322477d0aea36f779a2ab93ce633c504f96491649feec324bce45d946402ebed729c81eabdd561
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,12 +4,15 @@
|
|
4
4
|
[][travis]
|
5
5
|
[][codeclimate]
|
6
6
|
[][coveralls]
|
7
|
-
[]
|
7
|
+
[][inchpages]
|
8
|
+
[][gitter]
|
8
9
|
|
9
10
|
[gem]: http://badge.fury.io/rb/tty
|
10
11
|
[travis]: http://travis-ci.org/peter-murach/tty
|
11
12
|
[codeclimate]: https://codeclimate.com/github/peter-murach/tty
|
12
13
|
[coveralls]: https://coveralls.io/r/peter-murach/tty
|
14
|
+
[inchpages]: http://inch-ci.org/github/peter-murach/tty
|
15
|
+
[gitter]: https://gitter.im/peter-murach/tty?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
|
13
16
|
|
14
17
|
> TTY is a toolbox for developing beautiful command line clients in Ruby. It provides a fluid interface for gathering input from the user, querying system and terminal and displaying information back. It is not another command line options parser, rather a plumbing library that helps in common tasks.
|
15
18
|
|
@@ -21,7 +24,7 @@ Even more so, any command line application needs a clear way of communicating it
|
|
21
24
|
|
22
25
|
## Features
|
23
26
|
|
24
|
-
|
27
|
+
Fully modular, choose out of many components to suite your needs and jump-start development of your command line app:
|
25
28
|
|
26
29
|
* Terminal ASCII and Unicode tables. [status: ✔ ]
|
27
30
|
* Terminal output colorization. [status: ✔ ]
|
@@ -55,13 +58,6 @@ Or install it yourself as:
|
|
55
58
|
## Contents
|
56
59
|
|
57
60
|
* [1. Table](#1-table)
|
58
|
-
* [1.1 Rendering](#11-rendering)
|
59
|
-
* [1.2 Multiline](#12-multiline)
|
60
|
-
* [1.3 Border](#13-border)
|
61
|
-
* [1.4 Alignment](#14-alignment)
|
62
|
-
* [1.5 Padding](#15-padding)
|
63
|
-
* [1.6 Filter](#16-filter)
|
64
|
-
* [1.7 Width](#17-width)
|
65
61
|
* [2. Color](#2-color)
|
66
62
|
* [3. ProgressBar](#3-progressbar)
|
67
63
|
* [4. Spinner](#4-spinner)
|
@@ -79,8 +75,9 @@ To print tabular output use `TTY::Table`:
|
|
79
75
|
|
80
76
|
```ruby
|
81
77
|
table = TTY::Table[['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']]
|
82
|
-
table.to_s
|
83
|
-
|
78
|
+
table.to_s
|
79
|
+
# => a1 a2 a3
|
80
|
+
b1 b2 b3
|
84
81
|
```
|
85
82
|
|
86
83
|
To colorize your strings use `Pastel`:
|
@@ -104,339 +101,79 @@ spinner = TTY::Spinner.new('Loading ... ', format: :spin_2)
|
|
104
101
|
30.times { spinner.spin }
|
105
102
|
```
|
106
103
|
|
107
|
-
|
108
|
-
|
109
|
-
To instantiate table pass 2-dimensional array:
|
110
|
-
|
111
|
-
```ruby
|
112
|
-
table = TTY::Table[['a1', 'a2'], ['b1', 'b2']]
|
113
|
-
table = TTY::Table.new [['a1', 'a2'], ['b1', 'b2']]
|
114
|
-
table = TTY::Table.new rows: [['a1', 'a2'], ['b1', 'b2']]
|
115
|
-
table = TTY::Table.new ['h1', 'h2'], [['a1', 'a2'], ['b1', 'b2']]
|
116
|
-
table = TTY::Table.new header: ['h1', 'h2'], rows: [['a1', 'a2'], ['b1', 'b2']]
|
117
|
-
```
|
118
|
-
|
119
|
-
or cross header with rows inside a hash like so
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
|
123
|
-
```
|
124
|
-
|
125
|
-
Table behaves like an Array so `<<`, `each` and familiar methods can be used
|
126
|
-
|
127
|
-
```ruby
|
128
|
-
table << ['a1', 'a2', 'a3']
|
129
|
-
table << ['b1', 'b2', 'b3']
|
130
|
-
table << ['a1', 'a2'] << ['b1', 'b2'] # chain rows assignment
|
131
|
-
|
132
|
-
table.each { |row| ... } # iterate over rows
|
133
|
-
table.each_with_index # iterate over each element with row and column index
|
134
|
-
table[i, j] # return element at row(i) and column(j)
|
135
|
-
table.row(i) { ... } # return array for row(i)
|
136
|
-
table.column(j) { ... } # return array for column(j)
|
137
|
-
table.column(name) # return array for column(name), name of header
|
138
|
-
table.row_size # return row size
|
139
|
-
table.column_size # return column size
|
140
|
-
table.size # return an array of [row_size, column_size]
|
141
|
-
table.border # specify border properties
|
142
|
-
```
|
143
|
-
|
144
|
-
or pass your rows in a block
|
145
|
-
|
146
|
-
```ruby
|
147
|
-
table = TTY::Table.new do |t|
|
148
|
-
t << ['a1', 'a2', 'a3']
|
149
|
-
t << ['b1', 'b2', 'b3']
|
150
|
-
end
|
151
|
-
```
|
152
|
-
|
153
|
-
#### 1.1 Rendering
|
154
|
-
|
155
|
-
Once you have an instance of `TTY::Table` you can print it out to the stdout like so:
|
156
|
-
|
157
|
-
```ruby
|
158
|
-
table.to_s
|
159
|
-
|
160
|
-
a1 a2 a3
|
161
|
-
b1 b2 b3
|
162
|
-
```
|
163
|
-
|
164
|
-
This will use so called `basic` renderer with default options.
|
165
|
-
|
166
|
-
However, you can include other customization options such as
|
167
|
-
|
168
|
-
```ruby
|
169
|
-
border # hash of border properties out of :characters, :style, :separator keys
|
170
|
-
border_class # a type of border to use
|
171
|
-
column_widths # array of maximum columns widths
|
172
|
-
column_aligns # array of cell alignments out of :left, :center and :right, default :left
|
173
|
-
filter # a proc object that is applied to every field in a row
|
174
|
-
indent # indentation applied to rendered table
|
175
|
-
multiline # if true will wrap text at new line or column width,
|
176
|
-
# when false will escape special characters
|
177
|
-
orientation # either :horizontal or :vertical
|
178
|
-
padding # array of integers to set table fields padding
|
179
|
-
renderer # enforce display type out of :basic, :color, :unicode, :ascii
|
180
|
-
resize # if true will expand/shrink table column sizes to match the width,
|
181
|
-
# otherwise if false rotate table vertically
|
182
|
-
width # constrain the table total width, otherwise dynamically
|
183
|
-
# calculated from content and terminal size
|
184
|
-
```
|
185
|
-
|
186
|
-
#### 1.2 Multiline
|
187
|
-
|
188
|
-
Renderer options may include `multiline` parameter. The `true` value will cause the table fields wrap at their natural line breaks or in case when the column widths are set the content will wrap.
|
189
|
-
|
190
|
-
```ruby
|
191
|
-
table = TTY::Table.new [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
|
192
|
-
table.render :ascii, multiline: true
|
193
|
-
# =>
|
194
|
-
+-------+-+
|
195
|
-
|First |1|
|
196
|
-
|Multi |2|
|
197
|
-
|Line | |
|
198
|
-
|Content| |
|
199
|
-
|Third |3|
|
200
|
-
+-------+-+
|
201
|
-
```
|
202
|
-
|
203
|
-
When the `false` option is specified all the special characters will be escaped and if the column widths are set the content will be truncated like so
|
104
|
+
To measure screen size use `TTY::Screen`:
|
204
105
|
|
205
106
|
```ruby
|
206
|
-
|
207
|
-
|
208
|
-
# =>
|
209
|
-
|
210
|
-
|First |1|
|
211
|
-
|Multiline\nContent|2|
|
212
|
-
|Third |3|
|
213
|
-
+------------------+-+
|
107
|
+
screen = TTY::Screen.new
|
108
|
+
screen.size # => [51, 280]
|
109
|
+
screen.width # => 280
|
110
|
+
screen.height # => 51
|
214
111
|
```
|
215
112
|
|
216
|
-
|
113
|
+
## 1. Table
|
217
114
|
|
218
|
-
|
115
|
+
**TTY** uses the [tty-table](https://github.com/peter-murach/tty-table) component in order to convert data into table and render as string output in tabular form. For example, to render data with ASCII border:
|
219
116
|
|
220
117
|
```ruby
|
221
|
-
table = TTY::Table.new ['header1',
|
222
|
-
table.render
|
223
|
-
# =>
|
224
|
-
┌───────┬───────┐
|
225
|
-
│header1│header2│
|
226
|
-
├───────┼───────┤
|
227
|
-
│a1 │a2 │
|
228
|
-
│b1 │b2 │
|
229
|
-
└───────┴───────┘
|
230
|
-
```
|
231
|
-
|
232
|
-
You can also create your own custom border by subclassing `TTY::Table::Border` and implementing the `def_border` method using internal DSL methods like so:
|
233
|
-
|
234
|
-
```ruby
|
235
|
-
class MyBorder < TTY::Table::Border
|
236
|
-
def_border do
|
237
|
-
left '$'
|
238
|
-
center '$'
|
239
|
-
right '$'
|
240
|
-
bottom ' '
|
241
|
-
bottom_mid '*'
|
242
|
-
bottom_left '*'
|
243
|
-
bottom_right '*'
|
244
|
-
end
|
245
|
-
end
|
246
|
-
```
|
247
|
-
|
248
|
-
Next pass the border class to your table instance `render_with` method
|
249
|
-
|
250
|
-
```ruby
|
251
|
-
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
252
|
-
table.render_with MyBorder
|
253
|
-
# =>
|
254
|
-
$header1$header2$
|
255
|
-
$a1 $a2 $
|
256
|
-
* * *
|
257
|
-
```
|
258
|
-
|
259
|
-
Finally, if you want to introduce slight modifications to the predefined border types, you can use table `border` helper like so
|
260
|
-
|
261
|
-
```ruby
|
262
|
-
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
263
|
-
table.render do |renderer|
|
264
|
-
renderer.border do
|
265
|
-
mid '='
|
266
|
-
mid_mid ' '
|
267
|
-
end
|
268
|
-
end
|
269
|
-
# =>
|
270
|
-
header1 header2
|
271
|
-
======= =======
|
272
|
-
a1 a2
|
273
|
-
b1 b2
|
274
|
-
```
|
275
|
-
|
276
|
-
In addition to specifying border characters you can force table to render separator line on each row like:
|
277
|
-
|
278
|
-
```ruby
|
279
|
-
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']]
|
280
|
-
table.render do |renderer|
|
281
|
-
renderer.border.separator = :each_row
|
282
|
-
end
|
118
|
+
table = TTY::Table.new ['header1','header2'], [['a1','a2'], ['b1','b2']]
|
119
|
+
table.render(:ascii)
|
283
120
|
# =>
|
284
121
|
+-------+-------+
|
285
122
|
|header1|header2|
|
286
123
|
+-------+-------+
|
287
124
|
|a1 |a2 |
|
288
|
-
+-------+-------+
|
289
125
|
|b1 |b2 |
|
290
126
|
+-------+-------+
|
291
127
|
```
|
292
128
|
|
293
|
-
|
129
|
+
Please refer to [documentation](https://github.com/peter-murach/tty-table) for complete API.
|
294
130
|
|
295
|
-
|
296
|
-
table.render do |renderer|
|
297
|
-
renderer.border.style = :red
|
298
|
-
end
|
299
|
-
```
|
300
|
-
|
301
|
-
#### 1.4 Alignment
|
302
|
-
|
303
|
-
All columns are left aligned by default. You can enforce per column alignment by passing `column_aligns` option like so
|
304
|
-
|
305
|
-
```ruby
|
306
|
-
rows = [['a1', 'a2'], ['b1', 'b2']
|
307
|
-
table = TTY::Table.new rows: rows
|
308
|
-
table.render column_aligns: [:center, :right]
|
309
|
-
```
|
131
|
+
## 2. Color
|
310
132
|
|
311
|
-
|
133
|
+
In order to colorize your output **TTY** uses the [pastel](https://github.com/peter-murach/pastel) component like so:
|
312
134
|
|
313
135
|
```ruby
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
If you require a more granular alignment you can align individual fields in a row by passing `align` option
|
318
|
-
|
319
|
-
```ruby
|
320
|
-
table = TTY::Table.new do |t|
|
321
|
-
t << ['a1', 'a2', 'a3']
|
322
|
-
t << ['b1', {:value => 'b2', :align => :right}, 'b3']
|
323
|
-
t << ['c1', 'c2', {:value => 'c3', :align => :center}]
|
324
|
-
end
|
136
|
+
pastel = Pastel.new
|
137
|
+
pastel.red.on_green.bold 'text...' # => red bold text on green background
|
325
138
|
```
|
326
139
|
|
327
|
-
|
328
|
-
|
329
|
-
By default padding is not applied. You can add `padding` to table fields like so
|
140
|
+
Please refer to [documentation](https://github.com/peter-murach/pastel) for complete API.
|
330
141
|
|
331
|
-
|
332
|
-
header = ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra']
|
333
|
-
rows = [['id', 'int(11)', 'YES', 'nil', 'NULL', '']]
|
334
|
-
table = TTY::Table.new(header, rows)
|
335
|
-
table.render { |renderer| renderer.padding= [0,1,0,1] }
|
336
|
-
# =>
|
337
|
-
+-------+---------+------+-----+---------+-------+
|
338
|
-
| Field | Type | Null | Key | Default | Extra |
|
339
|
-
+-------+---------+------+-----+---------+-------+
|
340
|
-
| id | int(11) | YES | nil | NULL | |
|
341
|
-
+-------+---------+------+-----+---------+-------+
|
342
|
-
```
|
142
|
+
## 3. ProgressBar
|
343
143
|
|
344
|
-
|
144
|
+
In order to draw progress bar **TTY** uses the [tty-progressbar](https://github.com/peter-murach/tty-progressbar) component. For example, to render basic download bar do:
|
345
145
|
|
346
146
|
```ruby
|
347
|
-
|
348
|
-
|
349
|
-
renderer.padding.top = 1
|
350
|
-
}
|
351
|
-
# =>
|
352
|
-
+-----+-------+----+---+-------+-----+
|
353
|
-
| | | | | | |
|
354
|
-
|Field|Type |Null|Key|Default|Extra|
|
355
|
-
+-----+-------+----+---+-------+-----+
|
356
|
-
| | | | | | |
|
357
|
-
|id |int(11)|YES |nil|NULL | |
|
358
|
-
+-----+-------+----+---+-------+-----+
|
147
|
+
bar = TTY::ProgressBar.new("downloading [:bar]", total: 30)
|
148
|
+
30.times { bar.advance }
|
359
149
|
```
|
360
150
|
|
361
|
-
|
362
|
-
|
363
|
-
You can define filters that will modify individual table fields value before they are rendered. A filter can be a callable such as proc. Here's an example that formats
|
151
|
+
Please refer to [documentation](https://github.com/peter-murach/tty-progressbar) for complete API.
|
364
152
|
|
365
|
-
|
366
|
-
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
367
|
-
table.render do |renderer|
|
368
|
-
renderer.filter = Proc.new do |val, row_index, col_index|
|
369
|
-
if col_index == 1 and !(row_index == 0)
|
370
|
-
val.capitalize
|
371
|
-
else
|
372
|
-
val
|
373
|
-
end
|
374
|
-
end
|
375
|
-
end
|
376
|
-
# =>
|
377
|
-
+-------+-------+
|
378
|
-
|header1|header2|
|
379
|
-
+-------+-------+
|
380
|
-
|a1 |A2 |
|
381
|
-
+-------+-------+
|
382
|
-
|b1 |B2 |
|
383
|
-
+-------+-------+
|
384
|
-
```
|
153
|
+
## 4. Spinner
|
385
154
|
|
386
|
-
|
155
|
+
**TTY** uses the [tty-spinner](https://github.com/peter-murach/tty-spinner) component to handle terminal spinning animation. For instance, to create a simple spinner do:
|
387
156
|
|
388
157
|
```ruby
|
389
|
-
|
390
|
-
|
391
|
-
col_index % 2 == 1 ? TTY.color.set(val, :red, :on_green) : val
|
392
|
-
end
|
393
|
-
end
|
158
|
+
spinner = TTY::Spinner.new('Loading ... ', format: :spin_2)
|
159
|
+
30.times { spinner.spin }
|
394
160
|
```
|
395
161
|
|
396
|
-
|
397
|
-
|
398
|
-
To control table's column sizes pass `width`, `resize` options. By default table's natural column widths are calculated from the content. If the total table width does not fit in terminal window then the table is rotated vertically to preserve content.
|
399
|
-
|
400
|
-
The `resize` property will force the table to expand/shrink to match the terminal width or custom `width`. On its own the `width` property will not resize table but only enforce table vertical rotation if content overspills.
|
401
|
-
|
402
|
-
```ruby
|
403
|
-
header = ['h1', 'h2', 'h3']
|
404
|
-
rows = [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']]
|
405
|
-
table = TTY::Table.new header, rows
|
406
|
-
table.render width: 80, resize: true
|
407
|
-
# =>
|
408
|
-
+---------+-------+------------+
|
409
|
-
|h1 |h2 |h3 |
|
410
|
-
+---------+-------+------------+
|
411
|
-
|aaa1 |aa2 |aaaaaaa3 |
|
412
|
-
|b1 |b2 |b3 |
|
413
|
-
+---------+-------+------------+
|
414
|
-
```
|
162
|
+
Please refer to [documentation](https://github.com/peter-murach/tty-spinner) for complete API.
|
415
163
|
|
416
|
-
##
|
164
|
+
## 5. Screen
|
417
165
|
|
418
|
-
|
166
|
+
**TTY** uses the [tty-screen](https://github.com/peter-murach/tty-screen) component to measure the screen properties. For example to get screen size do:
|
419
167
|
|
420
168
|
```ruby
|
421
|
-
|
422
|
-
|
169
|
+
screen = TTY::Screen.new
|
170
|
+
screen.size # => [51, 280]
|
171
|
+
screen.width # => 280
|
172
|
+
screen.height # => 51
|
423
173
|
```
|
424
174
|
|
425
|
-
Please refer to [documentation](https://github.com/peter-murach/pastel) for complete API.
|
426
|
-
|
427
|
-
## 3. ProgressBar
|
428
|
-
|
429
|
-
Please refer to [documentation](https://github.com/peter-murach/tty-progressbar) for complete API.
|
430
|
-
|
431
|
-
## 4. Spinner
|
432
|
-
|
433
|
-
Please refer to [documentation](https://github.com/peter-murach/tty-spinner) for complete API.
|
434
|
-
|
435
|
-
## 5. Screen
|
436
|
-
|
437
175
|
Please refer to [documentation](https://github.com/peter-murach/tty-screen) for complete API.
|
438
176
|
|
439
|
-
|
440
177
|
## 6 Terminal
|
441
178
|
|
442
179
|
To read general terminal properties you can use on of the helpers
|
@@ -490,7 +227,7 @@ default # default value used if none is provided
|
|
490
227
|
echo # turn echo on and off (default: true)
|
491
228
|
mask # mask characters i.e '****' (default: false)
|
492
229
|
modify # apply answer modification :upcase, :downcase, :trim, :chomp etc..
|
493
|
-
|
230
|
+
in # specify range '0-9', '0..9', '0...9' or negative '-1..-9'
|
494
231
|
validate # regex against which stdin input is checked
|
495
232
|
valid # a list of expected valid options
|
496
233
|
```
|
@@ -533,8 +270,7 @@ read_keypress # return the key pressed
|
|
533
270
|
For example, if we wanted to ask a user for a single digit in given range
|
534
271
|
|
535
272
|
```ruby
|
536
|
-
ask("Provide number in range: 0-9") do
|
537
|
-
range '0-9'
|
273
|
+
ask("Provide number in range: 0-9").in('0-9') do
|
538
274
|
on_error :retry
|
539
275
|
end.read_int
|
540
276
|
```
|
@@ -587,4 +323,4 @@ TTY::System.editor.open('file path...')
|
|
587
323
|
|
588
324
|
## Copyright
|
589
325
|
|
590
|
-
Copyright (c) 2012-
|
326
|
+
Copyright (c) 2012-2015 Piotr Murach. See LICENSE for further details.
|
@@ -1,38 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
class
|
4
|
+
class Shell
|
5
5
|
# A class responsible for string comparison
|
6
6
|
class Distance
|
7
|
-
include Unicode
|
8
|
-
|
9
|
-
attr_reader :first
|
10
|
-
|
11
|
-
attr_reader :second
|
12
|
-
|
13
|
-
# Initalize a Distance
|
14
|
-
#
|
15
|
-
# @param [String] first
|
16
|
-
# the first string for comparision
|
17
|
-
#
|
18
|
-
# @param [String] second
|
19
|
-
# the second string for comparison
|
20
|
-
#
|
21
|
-
# @api private
|
22
|
-
def initialize(first, second, *args)
|
23
|
-
options = Utils.extract_options!(args)
|
24
|
-
@first = first.to_s
|
25
|
-
@second = second.to_s
|
26
|
-
# TODO: add option to ignore case
|
27
|
-
end
|
28
|
-
|
29
7
|
# Calculate the optimal string alignment distance
|
30
8
|
#
|
31
|
-
# @api
|
32
|
-
def distance
|
9
|
+
# @api public
|
10
|
+
def distance(first, second)
|
33
11
|
distances = []
|
34
|
-
rows = first.length
|
35
|
-
cols = second.length
|
12
|
+
rows = first.to_s.length
|
13
|
+
cols = second.to_s.length
|
36
14
|
|
37
15
|
0.upto(rows) do |index|
|
38
16
|
distances << [index] + [0] * cols
|
data/lib/tty/shell/suggestion.rb
CHANGED
@@ -5,9 +5,11 @@ module TTY
|
|
5
5
|
class Shell
|
6
6
|
# A class representing a suggestion
|
7
7
|
class Suggestion
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
DEFAULT_INDENT = 8
|
9
|
+
|
10
|
+
SINGLE_TEXT = 'Did you mean this?'
|
11
|
+
|
12
|
+
PLURAL_TEXT = 'Did you mean one of these?'
|
11
13
|
|
12
14
|
# Number of spaces
|
13
15
|
#
|
@@ -24,17 +26,6 @@ module TTY
|
|
24
26
|
# @api public
|
25
27
|
attr_reader :plural_text
|
26
28
|
|
27
|
-
# Possible suggestions
|
28
|
-
#
|
29
|
-
# @api public
|
30
|
-
attr_reader :suggestions
|
31
|
-
|
32
|
-
DEFAULT_INDENT = 8
|
33
|
-
|
34
|
-
SINGLE_TEXT = 'Did you mean this?'
|
35
|
-
|
36
|
-
PLURAL_TEXT = 'Did you mean one of these?'
|
37
|
-
|
38
29
|
# Initialize a Suggestion
|
39
30
|
#
|
40
31
|
# @api public
|
@@ -43,6 +34,7 @@ module TTY
|
|
43
34
|
@single_text = options.fetch(:single_text) { SINGLE_TEXT }
|
44
35
|
@plural_text = options.fetch(:plural_text) { PLURAL_TEXT }
|
45
36
|
@suggestions = []
|
37
|
+
@comparator = Distance.new
|
46
38
|
end
|
47
39
|
|
48
40
|
# Suggest matches out of possibile strings
|
@@ -63,6 +55,8 @@ module TTY
|
|
63
55
|
evaluate
|
64
56
|
end
|
65
57
|
|
58
|
+
private
|
59
|
+
|
66
60
|
# Measure distances between messag and possibilities
|
67
61
|
#
|
68
62
|
# @param [String] message
|
@@ -76,7 +70,7 @@ module TTY
|
|
76
70
|
distances = Hash.new { |hash, key| hash[key] = [] }
|
77
71
|
|
78
72
|
possibilities.each do |possibility|
|
79
|
-
distances[
|
73
|
+
distances[comparator.distance(message, possibility)] << possibility
|
80
74
|
end
|
81
75
|
distances
|
82
76
|
end
|
@@ -90,17 +84,43 @@ module TTY
|
|
90
84
|
# @api private
|
91
85
|
def evaluate
|
92
86
|
return suggestions if suggestions.empty?
|
93
|
-
suggestion = ''
|
94
87
|
if suggestions.one?
|
95
|
-
|
96
|
-
suggestion << (' ' * indent + @suggestions.first)
|
88
|
+
build_single_suggestion
|
97
89
|
else
|
98
|
-
|
99
|
-
suggestion << suggestions.map do |sugest|
|
100
|
-
' ' * indent + sugest
|
101
|
-
end.join("\n")
|
90
|
+
build_multiple_suggestions
|
102
91
|
end
|
103
92
|
end
|
93
|
+
|
94
|
+
# @api private
|
95
|
+
def build_single_suggestion
|
96
|
+
suggestion = ''
|
97
|
+
suggestion << single_text + "\n"
|
98
|
+
suggestion << (' ' * indent + suggestions.first)
|
99
|
+
end
|
100
|
+
|
101
|
+
# @api private
|
102
|
+
def build_multiple_suggestions
|
103
|
+
suggestion = ''
|
104
|
+
suggestion << plural_text + "\n"
|
105
|
+
suggestion << suggestions.map do |sugest|
|
106
|
+
' ' * indent + sugest
|
107
|
+
end.join("\n")
|
108
|
+
end
|
109
|
+
|
110
|
+
# Distance comparator
|
111
|
+
#
|
112
|
+
# @api private
|
113
|
+
attr_reader :comparator
|
114
|
+
|
115
|
+
# Reference to the current shell
|
116
|
+
#
|
117
|
+
# @api private
|
118
|
+
attr_reader :shell
|
119
|
+
|
120
|
+
# Possible suggestions
|
121
|
+
#
|
122
|
+
# @api private
|
123
|
+
attr_reader :suggestions
|
104
124
|
end # Suggestion
|
105
125
|
end # Shell
|
106
126
|
end # TTY
|
data/lib/tty/version.rb
CHANGED