tty 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -3
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +42 -306
  5. data/lib/tty/{text → shell}/distance.rb +5 -27
  6. data/lib/tty/shell/suggestion.rb +42 -22
  7. data/lib/tty/version.rb +1 -1
  8. data/lib/tty.rb +2 -42
  9. data/spec/tty/{text → shell}/distance/distance_spec.rb +4 -4
  10. data/spec/tty/shell/distance/initialize_spec.rb +14 -0
  11. data/tty.gemspec +4 -3
  12. metadata +37 -269
  13. data/benchmarks/table.rb +0 -40
  14. data/lib/tty/table/border/ascii.rb +0 -32
  15. data/lib/tty/table/border/null.rb +0 -37
  16. data/lib/tty/table/border/row_line.rb +0 -18
  17. data/lib/tty/table/border/unicode.rb +0 -32
  18. data/lib/tty/table/border.rb +0 -222
  19. data/lib/tty/table/border_dsl.rb +0 -251
  20. data/lib/tty/table/border_options.rb +0 -53
  21. data/lib/tty/table/column_set.rb +0 -121
  22. data/lib/tty/table/columns.rb +0 -170
  23. data/lib/tty/table/error.rb +0 -18
  24. data/lib/tty/table/field.rb +0 -139
  25. data/lib/tty/table/header.rb +0 -133
  26. data/lib/tty/table/indentation.rb +0 -52
  27. data/lib/tty/table/operation/alignment.rb +0 -107
  28. data/lib/tty/table/operation/alignment_set.rb +0 -77
  29. data/lib/tty/table/operation/escape.rb +0 -30
  30. data/lib/tty/table/operation/filter.rb +0 -34
  31. data/lib/tty/table/operation/padding.rb +0 -95
  32. data/lib/tty/table/operation/truncation.rb +0 -55
  33. data/lib/tty/table/operation/wrapped.rb +0 -58
  34. data/lib/tty/table/operations.rb +0 -69
  35. data/lib/tty/table/orientation/horizontal.rb +0 -48
  36. data/lib/tty/table/orientation/vertical.rb +0 -38
  37. data/lib/tty/table/orientation.rb +0 -57
  38. data/lib/tty/table/padder.rb +0 -180
  39. data/lib/tty/table/renderer/ascii.rb +0 -16
  40. data/lib/tty/table/renderer/basic.rb +0 -294
  41. data/lib/tty/table/renderer/color.rb +0 -12
  42. data/lib/tty/table/renderer/unicode.rb +0 -21
  43. data/lib/tty/table/renderer.rb +0 -101
  44. data/lib/tty/table/row.rb +0 -217
  45. data/lib/tty/table/transformation.rb +0 -39
  46. data/lib/tty/table/validatable.rb +0 -64
  47. data/lib/tty/table.rb +0 -476
  48. data/lib/tty/text/truncation.rb +0 -99
  49. data/lib/tty/text/wrapping.rb +0 -118
  50. data/lib/tty/text.rb +0 -98
  51. data/spec/tty/table/access_spec.rb +0 -86
  52. data/spec/tty/table/add_row_spec.rb +0 -28
  53. data/spec/tty/table/border/ascii/rendering_spec.rb +0 -90
  54. data/spec/tty/table/border/new_spec.rb +0 -27
  55. data/spec/tty/table/border/null/rendering_spec.rb +0 -130
  56. data/spec/tty/table/border/options/from_spec.rb +0 -38
  57. data/spec/tty/table/border/options/new_spec.rb +0 -14
  58. data/spec/tty/table/border/unicode/rendering_spec.rb +0 -63
  59. data/spec/tty/table/border_options/new_spec.rb +0 -20
  60. data/spec/tty/table/border_options/update_spec.rb +0 -18
  61. data/spec/tty/table/column_set/extract_widths_spec.rb +0 -15
  62. data/spec/tty/table/column_set/total_width_spec.rb +0 -15
  63. data/spec/tty/table/column_set/widths_from_spec.rb +0 -51
  64. data/spec/tty/table/columns/enforce_spec.rb +0 -68
  65. data/spec/tty/table/columns/widths_spec.rb +0 -35
  66. data/spec/tty/table/data_spec.rb +0 -14
  67. data/spec/tty/table/each_spec.rb +0 -41
  68. data/spec/tty/table/each_with_index_spec.rb +0 -57
  69. data/spec/tty/table/empty_spec.rb +0 -23
  70. data/spec/tty/table/eql_spec.rb +0 -34
  71. data/spec/tty/table/field/equality_spec.rb +0 -51
  72. data/spec/tty/table/field/length_spec.rb +0 -21
  73. data/spec/tty/table/field/lines_spec.rb +0 -21
  74. data/spec/tty/table/field/new_spec.rb +0 -29
  75. data/spec/tty/table/field/width_spec.rb +0 -23
  76. data/spec/tty/table/filter_spec.rb +0 -23
  77. data/spec/tty/table/header/call_spec.rb +0 -30
  78. data/spec/tty/table/header/height_spec.rb +0 -27
  79. data/spec/tty/table/header/new_spec.rb +0 -25
  80. data/spec/tty/table/header/set_spec.rb +0 -20
  81. data/spec/tty/table/header/to_ary_spec.rb +0 -14
  82. data/spec/tty/table/header_spec.rb +0 -13
  83. data/spec/tty/table/indentation/insert_indent_spec.rb +0 -27
  84. data/spec/tty/table/initialize_spec.rb +0 -88
  85. data/spec/tty/table/operation/alignment/format_spec.rb +0 -47
  86. data/spec/tty/table/operation/alignment/new_spec.rb +0 -31
  87. data/spec/tty/table/operation/alignment_set/call_spec.rb +0 -39
  88. data/spec/tty/table/operation/alignment_set/each_spec.rb +0 -17
  89. data/spec/tty/table/operation/alignment_set/new_spec.rb +0 -27
  90. data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +0 -14
  91. data/spec/tty/table/operation/escape/call_spec.rb +0 -16
  92. data/spec/tty/table/operation/filter/call_spec.rb +0 -17
  93. data/spec/tty/table/operation/truncation/call_spec.rb +0 -32
  94. data/spec/tty/table/operation/truncation/truncate_spec.rb +0 -22
  95. data/spec/tty/table/operation/wrapped/call_spec.rb +0 -33
  96. data/spec/tty/table/operation/wrapped/wrap_spec.rb +0 -23
  97. data/spec/tty/table/operations/new_spec.rb +0 -30
  98. data/spec/tty/table/options_spec.rb +0 -25
  99. data/spec/tty/table/orientation_spec.rb +0 -145
  100. data/spec/tty/table/padder/parse_spec.rb +0 -45
  101. data/spec/tty/table/padder/to_s_spec.rb +0 -14
  102. data/spec/tty/table/padding_spec.rb +0 -120
  103. data/spec/tty/table/properties_spec.rb +0 -25
  104. data/spec/tty/table/render_spec.rb +0 -63
  105. data/spec/tty/table/render_with_spec.rb +0 -106
  106. data/spec/tty/table/renderer/ascii/indentation_spec.rb +0 -41
  107. data/spec/tty/table/renderer/ascii/padding_spec.rb +0 -61
  108. data/spec/tty/table/renderer/ascii/render_spec.rb +0 -68
  109. data/spec/tty/table/renderer/ascii/resizing_spec.rb +0 -114
  110. data/spec/tty/table/renderer/ascii/separator_spec.rb +0 -28
  111. data/spec/tty/table/renderer/basic/alignment_spec.rb +0 -88
  112. data/spec/tty/table/renderer/basic/coloring_spec.rb +0 -46
  113. data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +0 -28
  114. data/spec/tty/table/renderer/basic/filter_spec.rb +0 -53
  115. data/spec/tty/table/renderer/basic/indentation_spec.rb +0 -48
  116. data/spec/tty/table/renderer/basic/multiline_content_spec.rb +0 -135
  117. data/spec/tty/table/renderer/basic/new_spec.rb +0 -22
  118. data/spec/tty/table/renderer/basic/options_spec.rb +0 -48
  119. data/spec/tty/table/renderer/basic/padding_spec.rb +0 -52
  120. data/spec/tty/table/renderer/basic/render_spec.rb +0 -57
  121. data/spec/tty/table/renderer/basic/resizing_spec.rb +0 -96
  122. data/spec/tty/table/renderer/basic/separator_spec.rb +0 -43
  123. data/spec/tty/table/renderer/basic/truncation_spec.rb +0 -35
  124. data/spec/tty/table/renderer/basic/wrapping_spec.rb +0 -40
  125. data/spec/tty/table/renderer/basic_spec.rb +0 -0
  126. data/spec/tty/table/renderer/border_spec.rb +0 -104
  127. data/spec/tty/table/renderer/render_spec.rb +0 -36
  128. data/spec/tty/table/renderer/select_spec.rb +0 -22
  129. data/spec/tty/table/renderer/style_spec.rb +0 -72
  130. data/spec/tty/table/renderer/unicode/indentation_spec.rb +0 -41
  131. data/spec/tty/table/renderer/unicode/padding_spec.rb +0 -61
  132. data/spec/tty/table/renderer/unicode/render_spec.rb +0 -68
  133. data/spec/tty/table/renderer/unicode/separator_spec.rb +0 -26
  134. data/spec/tty/table/renderer_spec.rb +0 -19
  135. data/spec/tty/table/rotate_spec.rb +0 -86
  136. data/spec/tty/table/row/access_spec.rb +0 -25
  137. data/spec/tty/table/row/call_spec.rb +0 -41
  138. data/spec/tty/table/row/data_spec.rb +0 -26
  139. data/spec/tty/table/row/each_spec.rb +0 -31
  140. data/spec/tty/table/row/equality_spec.rb +0 -73
  141. data/spec/tty/table/row/height_spec.rb +0 -27
  142. data/spec/tty/table/row/new_spec.rb +0 -41
  143. data/spec/tty/table/row/to_ary_spec.rb +0 -14
  144. data/spec/tty/table/to_s_spec.rb +0 -63
  145. data/spec/tty/table/transformation/extract_tuples_spec.rb +0 -35
  146. data/spec/tty/table/validatable/validate_options_spec.rb +0 -33
  147. data/spec/tty/table/validatable_spec.rb +0 -32
  148. data/spec/tty/text/distance/initialize_spec.rb +0 -14
  149. data/spec/tty/text/distance_spec.rb +0 -11
  150. data/spec/tty/text/truncate_spec.rb +0 -12
  151. data/spec/tty/text/truncation/initialize_spec.rb +0 -29
  152. data/spec/tty/text/truncation/truncate_spec.rb +0 -94
  153. data/spec/tty/text/wrap_spec.rb +0 -13
  154. data/spec/tty/text/wrapping/initialize_spec.rb +0 -25
  155. 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: 4e81389b3c20540a552a4677e80c55307fb8964c
4
- data.tar.gz: 10b70aa565b3bd550cbb7177826e3075ff47b9f8
3
+ metadata.gz: ad0efdc965bc3c933d7cb71d0bf75a5be2c3bf45
4
+ data.tar.gz: f4e7b12fbe37bce6cc467730a472112ca673ae36
5
5
  SHA512:
6
- metadata.gz: 07691cae6416ce1edee73949c2088ce1be07ab2f764b8ca1c59af6e5d2c98a03efaad09a64171a87eba9152c507277bdc01ba91a024edfd28cecfad92f68aa7d
7
- data.tar.gz: 4be98f8a15d2cf8a363b56c27d101c901a8f4091c24fe470a17350ec952d2b0ce1b2b143d2e887392f83d290a6ddfd6e2b5437e87307012a5f59e951d1a61601
6
+ metadata.gz: 0045cfb5de1675a6485681b047f6cf5e785e1758b6fca908bf84a943d199f8ed2e77ba8cb0239f8dc810f07cea551ae565b485849c395e1e9fa7ed1de62bcb79
7
+ data.tar.gz: 103f2d9010887f3b658f97f901df315deebe80f54248950818322477d0aea36f779a2ab93ce633c504f96491649feec324bce45d946402ebed729c81eabdd561
data/.travis.yml CHANGED
@@ -2,9 +2,10 @@ language: ruby
2
2
  bundler_args: --without yard guard benchmarks
3
3
  script: "bundle exec rake ci"
4
4
  rvm:
5
- - 1.9.3
6
- - 2.0.0
7
- - 2.1.0
5
+ - 1.9
6
+ - 2.0
7
+ - 2.1
8
+ - 2.2
8
9
  - ruby-head
9
10
  - rbx-2
10
11
  - jruby-19mode
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 0.1.3 (April 3, 2015)
2
+
3
+ * Add table rendering as dependency on tty-table
4
+ * Update tty-progressbar dependency
5
+
1
6
  0.1.2 (December 14, 2014)
2
7
 
3
8
  * Add Necromancer for type conversions
data/README.md CHANGED
@@ -4,12 +4,15 @@
4
4
  [![Build Status](https://secure.travis-ci.org/peter-murach/tty.png?branch=master)][travis]
5
5
  [![Code Climate](https://codeclimate.com/github/peter-murach/tty.png)][codeclimate]
6
6
  [![Coverage Status](https://coveralls.io/repos/peter-murach/tty/badge.png?branch=master)][coveralls]
7
- [![Inline docs](http://inch-ci.org/github/peter-murach/tty.png?branch=master)](http://inch-ci.org/github/peter-murach/tty)
7
+ [![Inline docs](http://inch-ci.org/github/peter-murach/tty.png?branch=master)][inchpages]
8
+ [![Gitter](https://badges.gitter.im/Join Chat.svg)][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
- Jump-start development of your command line app:
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 # => a1 a2 a3
83
- b1 b2 b3
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
- ## 1 Table
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
- table = TTY::Table.new [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
207
- table.render :ascii, multiline: false
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
- #### 1.3 Border
113
+ ## 1. Table
217
114
 
218
- To print border around data table you need to specify `renderer` type out of `basic`, `ascii`, `unicode`. By default `basic` is used. For instance, to output unicode border:
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', 'header2'], [['a1', 'a2'], ['b1', 'b2']
222
- table.render :unicode
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
- Also to change the display color of your border do:
129
+ Please refer to [documentation](https://github.com/peter-murach/tty-table) for complete API.
294
130
 
295
- ```ruby
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
- To align a single column do
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
- table.align_column(1, :right)
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
- #### 1.5 Padding
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
- ```ruby
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
- or you can set specific padding using `right`, `left`, `top`, `bottom` helpers. However, when adding top or bottom padding a `multiline` option needs to be set to `true` to allow for rows to span multiple lines. For example
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
- table.render { |renderer|
348
- renderer.multiline = true
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
- #### 1.6 Filter
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
- ```ruby
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
- To color even fields red on green background add filter like so
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
- table.render do |renderer|
390
- renderer.filter = proc do |val, row_index, col_index|
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
- #### 1.7 Width
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
- ## 2 Color
164
+ ## 5. Screen
417
165
 
418
- To colorize your output you can use **Pastel** like so:
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
- pastel = Pastel.new
422
- pastel.red.on_green.bold 'text...' # => red bold text on green background
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
- range # specify range '0-9', '0..9', '0...9' or negative '-1..-9'
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-2014 Piotr Murach. See LICENSE for further details.
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 Text
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 private
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
@@ -5,9 +5,11 @@ module TTY
5
5
  class Shell
6
6
  # A class representing a suggestion
7
7
  class Suggestion
8
- # @api private
9
- attr_reader :shell
10
- private :shell
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[Text.distance(message, possibility)] << possibility
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
- suggestion << single_text + "\n"
96
- suggestion << (' ' * indent + @suggestions.first)
88
+ build_single_suggestion
97
89
  else
98
- suggestion << plural_text + "\n"
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
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module TTY
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end # TTY