tty 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +45 -115
  4. data/lib/tty.rb +3 -31
  5. data/lib/tty/plugins/plugin.rb +2 -2
  6. data/lib/tty/terminal.rb +2 -58
  7. data/lib/tty/terminal/home.rb +27 -9
  8. data/lib/tty/version.rb +1 -1
  9. data/spec/tty/plugins/plugin/load_spec.rb +10 -18
  10. data/spec/tty/system/editor/open_spec.rb +1 -1
  11. data/spec/tty/terminal/home_spec.rb +18 -26
  12. data/spec/tty/tty_spec.rb +1 -1
  13. data/spec/tty/vector/new_spec.rb +1 -1
  14. metadata +2 -83
  15. data/lib/tty/shell.rb +0 -211
  16. data/lib/tty/shell/distance.rb +0 -49
  17. data/lib/tty/shell/question.rb +0 -335
  18. data/lib/tty/shell/question/modifier.rb +0 -93
  19. data/lib/tty/shell/question/validation.rb +0 -92
  20. data/lib/tty/shell/reader.rb +0 -110
  21. data/lib/tty/shell/response.rb +0 -249
  22. data/lib/tty/shell/response_delegation.rb +0 -55
  23. data/lib/tty/shell/statement.rb +0 -60
  24. data/lib/tty/shell/suggestion.rb +0 -126
  25. data/lib/tty/support/utils.rb +0 -16
  26. data/lib/tty/terminal/echo.rb +0 -38
  27. data/lib/tty/terminal/raw.rb +0 -38
  28. data/spec/tty/shell/ask_spec.rb +0 -77
  29. data/spec/tty/shell/distance/distance_spec.rb +0 -75
  30. data/spec/tty/shell/distance/initialize_spec.rb +0 -14
  31. data/spec/tty/shell/error_spec.rb +0 -30
  32. data/spec/tty/shell/print_table_spec.rb +0 -24
  33. data/spec/tty/shell/question/argument_spec.rb +0 -30
  34. data/spec/tty/shell/question/character_spec.rb +0 -24
  35. data/spec/tty/shell/question/default_spec.rb +0 -25
  36. data/spec/tty/shell/question/in_spec.rb +0 -23
  37. data/spec/tty/shell/question/initialize_spec.rb +0 -24
  38. data/spec/tty/shell/question/modifier/apply_to_spec.rb +0 -34
  39. data/spec/tty/shell/question/modifier/letter_case_spec.rb +0 -27
  40. data/spec/tty/shell/question/modifier/whitespace_spec.rb +0 -33
  41. data/spec/tty/shell/question/modify_spec.rb +0 -44
  42. data/spec/tty/shell/question/valid_spec.rb +0 -46
  43. data/spec/tty/shell/question/validate_spec.rb +0 -30
  44. data/spec/tty/shell/question/validation/coerce_spec.rb +0 -24
  45. data/spec/tty/shell/question/validation/valid_value_spec.rb +0 -28
  46. data/spec/tty/shell/reader/getc_spec.rb +0 -42
  47. data/spec/tty/shell/response/read_bool_spec.rb +0 -40
  48. data/spec/tty/shell/response/read_char_spec.rb +0 -16
  49. data/spec/tty/shell/response/read_date_spec.rb +0 -20
  50. data/spec/tty/shell/response/read_email_spec.rb +0 -42
  51. data/spec/tty/shell/response/read_multiple_spec.rb +0 -23
  52. data/spec/tty/shell/response/read_number_spec.rb +0 -28
  53. data/spec/tty/shell/response/read_range_spec.rb +0 -31
  54. data/spec/tty/shell/response/read_spec.rb +0 -68
  55. data/spec/tty/shell/response/read_string_spec.rb +0 -19
  56. data/spec/tty/shell/say_spec.rb +0 -67
  57. data/spec/tty/shell/statement/initialize_spec.rb +0 -15
  58. data/spec/tty/shell/suggest_spec.rb +0 -50
  59. data/spec/tty/shell/warn_spec.rb +0 -30
  60. data/spec/tty/terminal/color_spec.rb +0 -16
  61. data/spec/tty/terminal/echo_spec.rb +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e168e711989d7450e4ab6ab08d745b056228ef3d
4
- data.tar.gz: 314f1c8e75411f3654306a22a5dde7ab1ab2e444
3
+ metadata.gz: aa562e7fbc18bbf508315687b7ff78d6fcc48065
4
+ data.tar.gz: 010a69a34e11b44425161db72f4faadf64e28a04
5
5
  SHA512:
6
- metadata.gz: 914a13c08433506b4d726a844cf12187e4553c348ada2383b7aaf90deadad41c4f2aba071a60a17678a649634f65a27f2c5fba9ec2dc9e2529c3b112f898a196
7
- data.tar.gz: f0fd1747bff7318467d15e423e21549715c51258192b7e0259e1c23cbdf8f2e745f38c77810ae3b8b9ae77029a65e7db6966a7baaf96e84909f2c96dfa3f873c
6
+ metadata.gz: d01e6e1e38b28bab95f362e574d9727c78d371385eb6ce9b828fee37f23219da45ec15dc765f6a6556a54a02d7d58588c7f3ed9da277bc46a7b0277e74944ecf
7
+ data.tar.gz: 5ba3bcaac12cd90c4a505452298e3c2d17a791581d6aa8eca8826437cfd1c838dfbb13ba11587a8532bcf5a9fad1a35c0a6dc0425a661097d34ae04752818854
@@ -1,3 +1,15 @@
1
+ 0.3.0 (Nov 24, 2015)
2
+
3
+ * Extract prompting for user input as dependency on tty-prompt
4
+ * Extract terminal mode
5
+ * Remove support utils
6
+ * Update dependencies to latest
7
+ * Change home directory implementation
8
+
9
+ 0.2.1 (Sept 20, 2015)
10
+
11
+ * Update dependencies to latest versions
12
+
1
13
  0.2.0 (July 6, 2015)
2
14
 
3
15
  * Extract platform detection as dependency on tty-platform
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
- # [![Application icon](https://github.com/peter-murach/tty/raw/master/images/tty.png)][icon]
2
- [icon]: http://peter-murach.github.io/tty/
1
+ <div align="center">
2
+ <a href="http://peter-murach.github.io/tty"><img width="130" src="https://cdn.rawgit.com/peter-murach/tty/master/images/tty.png" alt="tty logo" /></a>
3
+ </div>
4
+ <br/>
3
5
  [![Gem Version](https://badge.fury.io/rb/tty.svg)][gem]
4
6
  [![Build Status](https://secure.travis-ci.org/peter-murach/tty.svg?branch=master)][travis]
5
7
  [![Code Climate](https://codeclimate.com/github/peter-murach/tty/badges/gpa.svg)][codeclimate]
@@ -34,7 +36,7 @@ Fully modular, choose out of many components to suite your needs and jump-start
34
36
  * Text manipulation(wrapping/truncation) [status: ✔ ]
35
37
  * Terminal progress bars drawing. [status: ✔ ]
36
38
  * Terminal spinners. [status: ✔ ]
37
- * Prompt user interface. [status: In Progress]
39
+ * Interactive prompt for user input. [status: ]
38
40
  * File diffs. [status: TODO]
39
41
  * Configuration file management. [status: TODO]
40
42
  * Logging [status: In Progress]
@@ -61,12 +63,13 @@ Or install it yourself as:
61
63
  * [2. Drawing tables](#2-drawing-tables)
62
64
  * [3. Drawing progress bars](#3-drawing-progress-bars)
63
65
  * [4. Drawing spinners](#4-drawing-spinners)
64
- * [5. Output coloring](#5-output-coloring)
65
- * [6. Output paging](#6-output-paging)
66
- * [7. Detecting screen properties](#7-detecting-screen-properties)
67
- * [8. Detecting platform](#8-detecting-platform)
68
- * [9. Searching executables](#9-searching-executables)
69
- * [10. Prompting for input](#10-prompting-for-input)
66
+ * [5. Prompting for input](#5-prompting-for-input)
67
+ * [6. Output coloring](#6-output-coloring)
68
+ * [7. Output paging](#7-output-paging)
69
+ * [8. Detecting screen properties](#8-detecting-screen-properties)
70
+ * [9. Detecting platform](#9-detecting-platform)
71
+ * [10. Searching executables](#10-searching-executables)
72
+ * [11. Setting editor](#11-setting-editor)
70
73
 
71
74
  ## 1. Overview
72
75
 
@@ -111,6 +114,22 @@ screen.width # => 280
111
114
  screen.height # => 51
112
115
  ```
113
116
 
117
+ To ask for user input use `TTY::Prompt`:
118
+
119
+ ```ruby
120
+ prompt = TTY::Prompt.new
121
+ prompt.ask('Do you like Ruby?', type: :bool) # => true
122
+
123
+ # or ask to select from list
124
+
125
+ prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
126
+ # =>
127
+ # Choose your destiny? (Use arrow keys, press Enter to select)
128
+ # ‣ Scorpion
129
+ # Kano
130
+ # Jax
131
+ ```
132
+
114
133
  ## 2. Drawing tables
115
134
 
116
135
  **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:
@@ -153,7 +172,18 @@ spinner = TTY::Spinner.new('Loading ... ', format: :spin_2)
153
172
 
154
173
  Please refer to [documentation](https://github.com/peter-murach/tty-spinner) for complete API.
155
174
 
156
- ## 5. Output coloring
175
+ ## 5. Prompting for input
176
+
177
+ **TTY** relies on [tty-prompt](https://github.com/peter-murach/tty-prompt#ttyprompt) component for processing user input.
178
+
179
+ ```ruby
180
+ prompt = TTY::Prompt.new
181
+ prompt.ask('Do you like Ruby?').read_bool # => true
182
+ ```
183
+
184
+ Please refer to [documentation](https://github.com/peter-murach/tty-prompt#ttyprompt) for complete API.
185
+
186
+ ## 6. Output coloring
157
187
 
158
188
  In order to colorize your output **TTY** uses the [pastel](https://github.com/peter-murach/pastel) component like so:
159
189
 
@@ -164,7 +194,7 @@ pastel.red.on_green.bold 'text...' # => red bold text on green background
164
194
 
165
195
  Please refer to [documentation](https://github.com/peter-murach/pastel) for complete API.
166
196
 
167
- ## 6. Output paging
197
+ ## 7. Output paging
168
198
 
169
199
  To page terminal output **TTY** relies on [tty-pager](https://github.com/peter-murach/tty-pager) component.
170
200
 
@@ -177,7 +207,7 @@ pager.page('Very long text...')
177
207
 
178
208
  Please refer to [documentation](https://github.com/peter-murach/tty-pager) for complete API.
179
209
 
180
- ## 7. Detecting screen properties
210
+ ## 8. Detecting screen properties
181
211
 
182
212
  **TTY** uses the [tty-screen](https://github.com/peter-murach/tty-screen) component to measure the screen properties.
183
213
 
@@ -192,7 +222,7 @@ screen.height # => 51
192
222
 
193
223
  Please refer to [documentation](https://github.com/peter-murach/tty-screen) for complete API.
194
224
 
195
- ## 8. Detecting platform
225
+ ## 9. Detecting platform
196
226
 
197
227
  To check for platform properties **TTY** uses [tty-platform](https://github.com/peter-murach/tty-platform) component.
198
228
 
@@ -212,7 +242,7 @@ TTY::Platform.windows? # => false
212
242
 
213
243
  Please refer to [documentation](https://github.com/peter-murach/tty-platform) for complete API.
214
244
 
215
- ## 9. Searching executables
245
+ ## 10. Searching executables
216
246
 
217
247
  To find executable path **TTY** uses [tty-which](https://github.com/peter-murach/tty-which#ttywhich) component.
218
248
 
@@ -224,108 +254,8 @@ TTY::Which.which('less') # => '/usr/bin/less'
224
254
 
225
255
  Please refer to [documentation](https://github.com/peter-murach/tty-which) for complete API.
226
256
 
227
- ## 10. Prompting for input
228
-
229
- Main responsibility is to interact with the prompt and provide convenience methods.
230
-
231
- Available methods are
232
-
233
- ```ruby
234
- shell = TTY::Shell.new
235
- shell.ask # print question
236
- shell.read # read from stdin
237
- shell.say # print message to stdout
238
- shell.confirm # print message(s) in green
239
- shell.warn # print message(s) in yellow
240
- shell.error # print message(s) in red
241
- shell.suggest # print suggestion message based on possible matches
242
- shell.print_table # print table to stdout
243
- ```
244
-
245
- In order to ask question and parse answers:
246
-
247
- ```ruby
248
- shell = TTY::Shell.new
249
- answer = shell.ask("What is your name?").read_string
250
- ```
251
-
252
- The library provides small DSL to help with parsing and asking precise questions
253
-
254
- ```ruby
255
- argument # :required or :optional
256
- char # turn character based input, otherwise line (default: false)
257
- clean # reset question
258
- default # default value used if none is provided
259
- echo # turn echo on and off (default: true)
260
- mask # mask characters i.e '****' (default: false)
261
- modify # apply answer modification :upcase, :downcase, :trim, :chomp etc..
262
- in # specify range '0-9', '0..9', '0...9' or negative '-1..-9'
263
- validate # regex against which stdin input is checked
264
- valid # a list of expected valid options
265
- ```
266
-
267
- You can chain question methods or configure them inside a block
268
-
269
- ```ruby
270
- shell.ask("What is your name?").argument(:required).default('Piotr').validate(/\w+\s\w+/).read_string
271
-
272
- shell.ask "What is your name?" do
273
- argument :required
274
- default 'Piotr'
275
- validate /\w+\s\w+/
276
- valid ['Piotr', 'Piotrek']
277
- modify :capitalize
278
- end.read_string
279
- ```
280
-
281
- Reading answers and converting them into required types can be done with custom readers
282
-
283
- ```ruby
284
- read_bool # return true or false for strings such as "Yes", "No"
285
- read_char # return first character
286
- read_date # return date type
287
- read_datetime # return datetime type
288
- read_email # validate answer against email regex
289
- read_file # return a File object
290
- read_float # return decimal or error if cannot convert
291
- read_int # return integer or error if cannot convert
292
- read_multiple # return multiple line string
293
- read_password # return string with echo turned off
294
- read_range # return range type
295
- read_regex # return regex expression
296
- read_string # return string
297
- read_symbol # return symbol
298
- read_text # return multiline string
299
- read_keypress # return the key pressed
300
- ```
301
-
302
- For example, if we wanted to ask a user for a single digit in given range
303
-
304
- ```ruby
305
- ask("Provide number in range: 0-9").in('0-9') do
306
- on_error :retry
307
- end.read_int
308
- ```
309
-
310
- on the other hand, if we are interested in range answer then
311
-
312
- ```ruby
313
- ask("Provide range of numbers?").read_range
314
- ```
315
-
316
- To suggest possible matches for the user input use `suggest` method like so
317
-
318
- ```ruby
319
- shell.suggest('sta', ['stage', 'stash', 'commit', 'branch'])
320
- # =>
321
- Did you mean one of these?
322
- stage
323
- stash
324
- ```
257
+ ## 11. Setting editor
325
258
 
326
- TTY::System.which(cmd) # full path to executable if found, nil otherwise
327
- TTY::System.exists?(cmd) # check if command is available
328
- TTY::System.editor # provides access to system editor
329
259
  To set preferred editor you can either use shell environment variables such as `EDITOR` and `VISUAL` or set the command(s) manually like so
330
260
 
331
261
  ```ruby
data/lib/tty.rb CHANGED
@@ -12,35 +12,16 @@ require 'tty-table'
12
12
  require 'tty-which'
13
13
 
14
14
  require 'tty/version'
15
-
16
- require 'tty/support/utils'
17
15
  require 'tty/support/delegatable'
18
16
  require 'tty/support/coercion'
19
17
  require 'tty/support/unicode'
20
-
21
18
  require 'tty/terminal'
22
19
  require 'tty/system'
23
20
  require 'tty/vector'
24
- require 'tty/shell'
25
21
  require 'tty/logger'
26
22
  require 'tty/plugins'
27
-
28
23
  require 'tty/plugins/plugin'
29
-
30
- require 'tty/shell/distance'
31
- require 'tty/shell/response_delegation'
32
- require 'tty/shell/question'
33
- require 'tty/shell/question/validation'
34
- require 'tty/shell/question/modifier'
35
- require 'tty/shell/statement'
36
- require 'tty/shell/suggestion'
37
- require 'tty/shell/reader'
38
- require 'tty/shell/response'
39
-
40
- require 'tty/terminal/echo'
41
- require 'tty/terminal/raw'
42
24
  require 'tty/terminal/home'
43
-
44
25
  require 'tty/system/editor'
45
26
 
46
27
  module TTY
@@ -51,15 +32,6 @@ module TTY
51
32
  # Raised when the operation is not implemented
52
33
  class NoImplementationError < NotImplementedError; end
53
34
 
54
- # Raised when the required argument is not supplied
55
- class ArgumentRequired < ArgumentError; end
56
-
57
- # Raised when the argument validation fails
58
- class ArgumentValidation < ArgumentError; end
59
-
60
- # Raised when the argument is not expected
61
- class InvalidArgument < ArgumentError; end
62
-
63
35
  # Raised when the passed in validation argument is of wrong type
64
36
  class ValidationCoercion < TypeError; end
65
37
 
@@ -93,9 +65,9 @@ module TTY
93
65
  # @return [TTY::Shell]
94
66
  #
95
67
  # @api public
96
- def shell(input=$stdin, output=$stdout)
97
- @shell ||= Shell.new(input, output)
98
- end
68
+ # def shell(input=$stdin, output=$stdout)
69
+ # @shell ||= Shell.new(input, output)
70
+ # end
99
71
 
100
72
  # Return shared system object
101
73
  #
@@ -43,9 +43,9 @@ module TTY
43
43
  begin
44
44
  require gem_name unless enabled?
45
45
  rescue LoadError => error
46
- TTY.shell.error("Unable to load plugin #{gem_name} due to #{error}.")
46
+ puts("Unable to load plugin #{gem_name} due to #{error}.")
47
47
  rescue => error
48
- TTY.shell.error("require '#{gem_name}' failed with #{error}")
48
+ puts("require '#{gem_name}' failed with #{error}")
49
49
  end
50
50
  @enabled = true
51
51
  end
@@ -2,67 +2,11 @@
2
2
 
3
3
  module TTY
4
4
  class Terminal
5
- # Return access to color terminal
6
- #
7
- # @return [TTY::Terminal::Color]
8
- #
9
- # @api public
10
- attr_reader :color
11
-
12
5
  # Initialize a Terminal
13
6
  #
14
7
  # @api public
15
8
  def initialize(options = {})
16
- @color = Pastel.new
17
- @echo = TTY::Terminal::Echo.new
18
- @raw = TTY::Terminal::Raw.new
19
- @home = Home.new
20
- end
21
-
22
- # Switch echo on
23
- #
24
- # @api public
25
- def echo_on
26
- @echo.on
27
- end
28
-
29
- # Switch echo off
30
- #
31
- # @api public
32
- def echo_off
33
- @echo.off
34
- end
35
-
36
- # Echo given block
37
- #
38
- # @param [Boolean] is_on
39
- #
40
- # @api public
41
- def echo(is_on = true, &block)
42
- @echo.echo(is_on, &block)
43
- end
44
-
45
- # Switch raw mode on
46
- #
47
- # @api public
48
- def raw_on
49
- @raw.on
50
- end
51
-
52
- # Switch raw mode off
53
- #
54
- # @api public
55
- def raw_off
56
- @raw.off
57
- end
58
-
59
- # Use raw mode in the given block
60
- #
61
- # @param [Boolean] is_on
62
- #
63
- # @api public
64
- def raw(is_on = true, &block)
65
- @raw.raw(is_on, &block)
9
+ @home = Home.new
66
10
  end
67
11
 
68
12
  # Find user home directory
@@ -71,7 +15,7 @@ module TTY
71
15
  #
72
16
  # @api public
73
17
  def home
74
- @home.home
18
+ @home.find_home
75
19
  end
76
20
  end # Terminal
77
21
  end # TTY
@@ -4,19 +4,37 @@ module TTY
4
4
  class Terminal
5
5
  # A class responsible for locating user home
6
6
  class Home
7
+ # @api public
8
+ def initialize(platform = nil)
9
+ @platform = platform || TTY::Platform
10
+ end
11
+
7
12
  # Find user home
8
13
  #
9
14
  # @api public
10
- def home
11
- if (env_home = ENV['HOME'])
12
- env_home
15
+ def find_home
16
+ path = @platform.windows? ? windows_home : unix_home
17
+ File.expand_path(path)
18
+ end
19
+
20
+ def unix_home
21
+ require 'etc'
22
+ "~#{Etc.getlogin}"
23
+ rescue
24
+ ENV['HOME']
25
+ end
26
+
27
+ def windows_home
28
+ if (home = ENV['HOME'])
29
+ home.tr('\\', '/')
30
+ elsif ENV['HOMEDRIVE'] && ENV['HOMEPATH']
31
+ File.join(ENV['HOMEDRIVE'], ENV['HOMEPATH'])
32
+ elsif ENV['USERPROFILE']
33
+ ENV['USERPROFILE']
34
+ elsif ENV['HOMEDRIVE'] || ENV['SystemDrive']
35
+ File.join(ENV['HOMEDRIVE'] || ENV['SystemDrive'], '/')
13
36
  else
14
- begin
15
- require 'etc'
16
- File.expand_path("~#{Etc.getlogin}")
17
- rescue
18
- TTY::System.windows? ? 'C:/' : '/'
19
- end
37
+ 'C:/'
20
38
  end
21
39
  end
22
40
  end # Home