cliutils 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +5 -0
- data/README.md +103 -8
- data/lib/cliutils/constants.rb +1 -1
- data/lib/cliutils/prefs/pref.rb +1 -1
- data/lib/cliutils/pretty_io.rb +1 -1
- data/res/readme-images/prefs-ask-options.png +0 -0
- data/res/readme-images/prefs-ask-prereqs.png +0 -0
- data/test/test_files/prefstest.yaml +10 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 069c2e2c3141bb0295825c6a7ddcbaa9e5fce3b5
|
4
|
+
data.tar.gz: bd67db92f1833ecec791f0d58974687d2e5dae5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2a266ed437b59e7a41733ac86659f75def0aebb7dda0566530d2f7debd22ba7573acb6c4376cc7447a8a98cba505d96af9c452bb6cc1da6ebed6cf2e63b9c70
|
7
|
+
data.tar.gz: d5860547ff62f94989e8de94fa9502295b1f4439db0b8e9eac2638fe7d8beb38d9c173eeea7697935c4898bf975c0d261651383d35eeee4384c2f76875472d96
|
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -54,7 +54,7 @@ Note that although this README.md is extensive, it may not cover all methods. Ch
|
|
54
54
|
CLIUtils offers:
|
55
55
|
|
56
56
|
* [PrettyIO](#prettyio): nicer-looking CLI messages
|
57
|
-
* [Messenging](#messenging): a
|
57
|
+
* [Messenging](#messenging): a system to display nicely-formatted messages to one or more tagets
|
58
58
|
* [Configuration](#configuration): a app configuration manager
|
59
59
|
* [Prefs](#prefs): a preferences prompter and manager
|
60
60
|
|
@@ -73,7 +73,7 @@ puts 'A sample string'.red
|
|
73
73
|
```
|
74
74
|
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prettyio-red-text.png "Colored Text via PrettyIO")
|
75
75
|
|
76
|
-
|
76
|
+
You get a stable of utility methods for the common ANSI color codes:
|
77
77
|
|
78
78
|
```ruby
|
79
79
|
String.blue
|
@@ -95,7 +95,7 @@ puts 'A sample string'.colorize('35;42')
|
|
95
95
|
Naturally, memorizing the ANSI color scheme is a pain, so PrettyIO gives you a convenient method to look up these color combinations:
|
96
96
|
|
97
97
|
```ruby
|
98
|
-
|
98
|
+
color_chart
|
99
99
|
```
|
100
100
|
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prettyio-color-chart.png "PrettyIO Color Chart")
|
101
101
|
|
@@ -178,6 +178,7 @@ Often, it's desirable to log messages as they appear to your user. `messenging`
|
|
178
178
|
For instance, let's say you wanted to log a few messages to both your user's STDOUT and to `file.txt`:
|
179
179
|
|
180
180
|
```Ruby
|
181
|
+
# By default, messenger only outputs to STDOUT.
|
181
182
|
messenger.info('This should only appear in STDOUT.')
|
182
183
|
|
183
184
|
# messenger.attach takes a Hash of string/symbol keys
|
@@ -230,6 +231,7 @@ If there's data in there, it will be consumed into the `configurator`'s `data` p
|
|
230
231
|
Sections are top levels of the configuration file and are managed via the `configurator` object:
|
231
232
|
|
232
233
|
```Ruby
|
234
|
+
configuration.add_section(:app_data)
|
233
235
|
configuration.add_section(:user_data)
|
234
236
|
configuration.add_section(:program_data)
|
235
237
|
configuration.delete_section(:program_data)
|
@@ -257,22 +259,42 @@ Note that all your keys are converted to strings before saving (and, likewise, a
|
|
257
259
|
|
258
260
|
```YAML
|
259
261
|
---
|
262
|
+
app_data:
|
260
263
|
user_data:
|
261
264
|
username: bob
|
262
265
|
```
|
263
266
|
|
264
267
|
### Checking Configuration Versions
|
265
268
|
|
266
|
-
Often, you'll want to check the user's current version of your app against the last version that required some sort of configuration change. `configurator` allows for this via its `compare_version` method
|
269
|
+
Often, you'll want to check the user's current version of your app against the last version that required some sort of configuration change. `configurator` allows for this via its `compare_version` method.
|
270
|
+
|
271
|
+
Assume you have a config file that looks like this:
|
272
|
+
|
273
|
+
```YAML
|
274
|
+
---
|
275
|
+
app_data:
|
276
|
+
# The current version of the app
|
277
|
+
APP_VERSION: 1.0.0
|
278
|
+
|
279
|
+
# The last version that required
|
280
|
+
# a configuration change
|
281
|
+
NEWEST_CONFIG_VERSION: 1.8.0
|
282
|
+
|
283
|
+
# ...other keys...
|
284
|
+
```
|
285
|
+
|
286
|
+
...this will initiate a version check (and give you the option to do something with that information):
|
267
287
|
|
268
288
|
```Ruby
|
269
289
|
# Tell your configurator the name of the key that
|
270
290
|
# stores the app's version in its configuration file.
|
291
|
+
# NOTE that you don't have to specify the section.
|
271
292
|
configuration.cur_version_key = :APP_VERSION
|
272
293
|
|
273
294
|
# Tell your configurator the name of the key that
|
274
295
|
# stores the last version that needed a configuration
|
275
296
|
# change.
|
297
|
+
# NOTE that you don't have to specify the section.
|
276
298
|
configuration.last_version_key = :NEWEST_CONFIG_VERSION
|
277
299
|
|
278
300
|
# Run the check and use a block to get
|
@@ -325,6 +347,63 @@ Assuming the above, `Prefs` is instantiated like so:
|
|
325
347
|
prefs = CLIUtils::Prefs.new('path/to/yaml/file')
|
326
348
|
```
|
327
349
|
|
350
|
+
`Prefs` can also be instantiated via a Hash or an array of prompts; the overall schema remains the same:
|
351
|
+
|
352
|
+
```Ruby
|
353
|
+
# Instantiation through a Hash
|
354
|
+
h = {
|
355
|
+
prompts: [
|
356
|
+
{
|
357
|
+
prompt: 'What is your name?',
|
358
|
+
default: 'Bob Cobb',
|
359
|
+
config_key: :name,
|
360
|
+
config_section: :personal_info
|
361
|
+
},
|
362
|
+
{
|
363
|
+
prompt: 'What is your age?',
|
364
|
+
default: '45',
|
365
|
+
config_key: :age,
|
366
|
+
config_section: :personal_info
|
367
|
+
},
|
368
|
+
{
|
369
|
+
prompt: 'Batman or Superman?',
|
370
|
+
default: 'Batman',
|
371
|
+
config_key: :superhero,
|
372
|
+
config_section: :personal_info
|
373
|
+
}
|
374
|
+
]
|
375
|
+
}
|
376
|
+
|
377
|
+
prefs = CLIUtils::Prefs.new(h)
|
378
|
+
|
379
|
+
# Instantiation through an Array
|
380
|
+
|
381
|
+
a = [
|
382
|
+
{
|
383
|
+
prompt: 'What is your name?',
|
384
|
+
default: 'Bob Cobb',
|
385
|
+
config_key: :name,
|
386
|
+
config_section: :personal_info
|
387
|
+
},
|
388
|
+
{
|
389
|
+
prompt: 'What is your age?',
|
390
|
+
default: '45',
|
391
|
+
config_key: :age,
|
392
|
+
config_section: :personal_info
|
393
|
+
},
|
394
|
+
{
|
395
|
+
prompt: 'Batman or Superman?',
|
396
|
+
default: 'Batman',
|
397
|
+
config_key: :superhero,
|
398
|
+
config_section: :personal_info
|
399
|
+
}
|
400
|
+
]
|
401
|
+
|
402
|
+
prefs = CLIUtils::Prefs.new(a)
|
403
|
+
```
|
404
|
+
|
405
|
+
### Prompting the User
|
406
|
+
|
328
407
|
With valid preferences loaded, simply use `ask` to begin prompting your user:
|
329
408
|
|
330
409
|
```Ruby
|
@@ -334,7 +413,7 @@ prefs.ask
|
|
334
413
|
|
335
414
|
### Prerequisites
|
336
415
|
|
337
|
-
Sometimes, you need to answer certain prompts before others become relevant. `Prefs` allows this via a `prereqs` key, which can contain multiple already-answered key/value pairs to check for. For instance, imagine we want to drill into a user's superhero preference a bit further:
|
416
|
+
Sometimes, you need to answer certain prompts before others become relevant. `Prefs` allows this via a `prereqs` key, which can contain multiple already-answered key/value pairs to check for. For instance, imagine we want to drill into a user's superhero preference a bit further; using our previously-used YAML prefs file:
|
338
417
|
|
339
418
|
```YAML
|
340
419
|
prompts:
|
@@ -356,6 +435,14 @@ prompts:
|
|
356
435
|
prereqs:
|
357
436
|
- config_key: superhero
|
358
437
|
config_value: Superman
|
438
|
+
- prompt: Why don't you have a clue?
|
439
|
+
config_key: no_clue
|
440
|
+
config_section: personal_info
|
441
|
+
prereqs:
|
442
|
+
- config_key: superhero
|
443
|
+
config_value: Superman
|
444
|
+
- config_key: superman_answer
|
445
|
+
config_value: No clue
|
359
446
|
```
|
360
447
|
|
361
448
|
`prereqs` checks for already-answered preferences (based on a Configurator key and value); assuming everything checks out, the subsequent preferences are collected:
|
@@ -365,7 +452,7 @@ prefs.ask
|
|
365
452
|
```
|
366
453
|
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-prereqs.png "Prerequisities")
|
367
454
|
|
368
|
-
Be careful
|
455
|
+
Be careful that you don't define any circular prerequisities (e.g., A requires B and B requires A). In that case, both preferences will be ignored by `Prefs.ask`.
|
369
456
|
|
370
457
|
### Options
|
371
458
|
|
@@ -392,6 +479,14 @@ prompts:
|
|
392
479
|
prereqs:
|
393
480
|
- config_key: superhero
|
394
481
|
config_value: Superman
|
482
|
+
- prompt: Why don't you have a clue?
|
483
|
+
config_key: no_clue
|
484
|
+
config_section: personal_info
|
485
|
+
prereqs:
|
486
|
+
- config_key: superhero
|
487
|
+
config_value: Superman
|
488
|
+
- config_key: superman_answer
|
489
|
+
config_value: No clue
|
395
490
|
```
|
396
491
|
|
397
492
|
Once in place:
|
@@ -446,7 +541,7 @@ prompts:
|
|
446
541
|
- local_filepath
|
447
542
|
```
|
448
543
|
|
449
|
-
The `local_filepath` behavior will expand the user's answer as a filepath (e.g., `~/.ssh` will get saved as `/Users/
|
544
|
+
The `local_filepath` behavior will expand the user's answer as a filepath (e.g., `~/.ssh` will get saved as `/Users/bob/.ssh/`).
|
450
545
|
|
451
546
|
`Prefs` currently supports these behaviors:
|
452
547
|
|
@@ -492,7 +587,7 @@ To report bugs with or suggest features/changes for CLIUtils, please use the [Is
|
|
492
587
|
|
493
588
|
Contributions are welcome and encouraged. To contribute:
|
494
589
|
|
495
|
-
1. Fork it (
|
590
|
+
1. Fork it (http://github.com/bachya/cliutils/fork)
|
496
591
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
497
592
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
498
593
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/cliutils/constants.rb
CHANGED
data/lib/cliutils/prefs/pref.rb
CHANGED
@@ -101,7 +101,7 @@ module CLIUtils
|
|
101
101
|
ret = true
|
102
102
|
if @options
|
103
103
|
unless @options.include?(text)
|
104
|
-
@last_error_message = "Invalid option chosen (\"#{ text }\");"\
|
104
|
+
@last_error_message = "Invalid option chosen (\"#{ text }\"); " \
|
105
105
|
"valid options are: #{ options }"
|
106
106
|
ret = false
|
107
107
|
end
|
data/lib/cliutils/pretty_io.rb
CHANGED
@@ -35,7 +35,7 @@ module CLIUtils
|
|
35
35
|
# Displays a chart of all the possible ANSI foreground
|
36
36
|
# and background color combinations.
|
37
37
|
# @return [void]
|
38
|
-
def
|
38
|
+
def color_chart
|
39
39
|
[0, 1, 4, 5, 7].each do |attr|
|
40
40
|
puts '----------------------------------------------------------------'
|
41
41
|
puts "ESC[#{attr};Foreground;Background"
|
Binary file
|
Binary file
|
@@ -3,6 +3,7 @@ prompts:
|
|
3
3
|
default: Batman
|
4
4
|
config_key: superhero
|
5
5
|
config_section: personal_info
|
6
|
+
options: ['Batman', 'Superman']
|
6
7
|
- prompt: Do you feel smart for preferring Batman?
|
7
8
|
default: Y
|
8
9
|
config_key: batman_answer
|
@@ -16,4 +17,12 @@ prompts:
|
|
16
17
|
config_section: personal_info
|
17
18
|
prereqs:
|
18
19
|
- config_key: superhero
|
19
|
-
config_value: Superman
|
20
|
+
config_value: Superman
|
21
|
+
- prompt: Why don't you have a clue?
|
22
|
+
config_key: no_clue
|
23
|
+
config_section: personal_info
|
24
|
+
prereqs:
|
25
|
+
- config_key: superhero
|
26
|
+
config_value: Superman
|
27
|
+
- config_key: superman_answer
|
28
|
+
config_value: No clue
|