cli_tools 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97ca4006c9d4aad3f468fee6c21a6e7f7ed92939
4
- data.tar.gz: bd7c470b4d297335eb7bc463dd87448f4f2ac34e
3
+ metadata.gz: 4bcdce3da1ebb50323e0a4d23d8215aab5cf1036
4
+ data.tar.gz: 3967d3fb79f35e92060e6ae0b3f2beddf18d092f
5
5
  SHA512:
6
- metadata.gz: ed93a838a7f58d8313baa67d84bb430c0202f23b353b2fbce7238e5475cdd0bb326b1fcffe069a38dd57a6c33794dc4059564ea88d3da15a0c523e30566a2a43
7
- data.tar.gz: 058bc0e697e24561a7d9228021e4dd564e0768d88f628ce2a7777e9d371a6d07e7c8d8b0c7ed4b592713c8431b92b398461859a7a7674cf5883259f53af9775e
6
+ metadata.gz: ec2399a559e4486599225e35e998ef22363389a44970c316d920eef1dbc0c638aa5f1deb8a6b20d1d43580f249117b4f2351ff768e0ecc35a67020bdf3f79aa3
7
+ data.tar.gz: a55b1636c974b0f289af86e20be1bdf3ba164f602a2b4548eebd30333156ee938f30403a042fb325754a507d004e9c94bb4102e8d7d48fcf0a20aa11e887bc95
data/README.md CHANGED
@@ -1,6 +1,16 @@
1
- # Tools for CLI application
1
+ # Tools for a CLI application
2
2
 
3
- A collection of helper methods for ruby CLI application.
3
+ A collection of helper methods for ruby CLI applications.
4
+
5
+ - `sh` — shell command execution, with output capturing
6
+ - `putr` — like `puts`, but erases current line and does not advance to a new line, invaluable for displaying things like progress bar
7
+ - `esc_...` — ANSI colors for your output
8
+ - `kb_getkey` — get the f.. key pressed in a non-blocking way
9
+ - and other little friends
10
+
11
+ ## Requirements
12
+
13
+ You probably need a Unix based system to find this gem useful. The gem methods are tested and working on Mac OS X and Linux systems, and it is not guaranteed to work on Windows (especially because the console input/output is done the unix way).
4
14
 
5
15
  ## Installation
6
16
 
@@ -18,21 +28,35 @@ Or install it yourself as:
18
28
 
19
29
  ## Usage
20
30
 
31
+ Keep all helper methods in CliTools namespace:
21
32
  ```ruby
22
33
  require 'cli_tools'
23
- include CliTools
24
34
 
25
- puts esc_green("Hello")+' world!' # outputs 'Hello world!' where 'Hello' is painted green
35
+ puts CliTools.esc_green("Hello")+' world!' # outputs 'Hello world!' where 'Hello' is painted green
26
36
  ```
27
37
 
28
- OR: all the methods automatically included into Kernel:
38
+ OR: have all the methods automatically included:
29
39
 
30
40
  ```ruby
31
- require 'cli_tools/include'
41
+ require 'cli_tools/include' # equivalent to:
42
+ # require 'cli_tools'
43
+ # include CliTools
32
44
 
33
45
  puts esc_green("Hello")+' world!'
34
46
  ```
35
47
 
48
+ ### Try it out
49
+ ```
50
+ cd examples/
51
+ ruby ex-ansi.rb
52
+ ```
53
+
54
+
55
+ ## TODO
56
+ - add more specs
57
+ - write comprehensive docs
58
+
59
+
36
60
  ## Contributing
37
61
 
38
62
  1. Fork it
@@ -0,0 +1,10 @@
1
+ require 'cli_tools/include'
2
+
3
+ puts "this is ANSI output:"
4
+ putr "one"
5
+ sleep 1
6
+ putr "two"
7
+ sleep 1
8
+ putr "three"
9
+ sleep 1
10
+ puts " and done"
@@ -0,0 +1,27 @@
1
+ require 'cli_tools/include'
2
+
3
+ out = ''
4
+ out_nest = ''
5
+ sh_capture_output( out ) do
6
+ sh "ls -la"
7
+ puts ""
8
+ puts "captured inside:"
9
+ out1 = ''
10
+ sh "ruby ex-ok.rb", true, out1
11
+ puts "out1: #{out1}"
12
+
13
+ puts "nested block:"
14
+ sh_capture_output( out_nest ) do
15
+ sh "ruby ex-err.rb" rescue puts esc_red "sh raised an exception, recovered"
16
+ end
17
+ end
18
+
19
+
20
+ puts
21
+ puts "Globally captured output:"
22
+ puts out
23
+
24
+ puts
25
+ puts "Nested captured output:"
26
+ puts out_nest
27
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require 'cli_tools/include'
3
+
4
+ 1.upto(10) do |i|
5
+ puts "this is stdout output #{i}"
6
+ STDOUT.flush
7
+ STDERR.puts esc_red "this is stderr output #{i}"
8
+ STDERR.flush
9
+ sleep 1
10
+ end
11
+ exit -1
@@ -0,0 +1,11 @@
1
+ require 'cli_tools/include'
2
+
3
+ count = 0
4
+ puts "Press any key when you've had enough"
5
+ while (key = kb_getkey).nil?
6
+ count += 1
7
+ putr "#{count} #{esc_green 'bottles'} hanging on the wall... "
8
+ sleep 1
9
+ end
10
+ puts # advances to a new line
11
+ puts "pressed key code is: #{key[0].ord}"
data/examples/ex-ok.rb ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts "this is stdout output"
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cli_tools/include'
4
+
5
+ COUNT = 10
6
+ puts "hello, putr test"
7
+ 1.upto(COUNT) do |i|
8
+ putr "#{i} of #{COUNT}"
9
+ STDOUT.flush
10
+ sleep 1
11
+ end
12
+ puts " done"
@@ -0,0 +1,7 @@
1
+ require 'cli_tools/include'
2
+
3
+ puts "hello from puts"
4
+
5
+ putr "hello from putr"
6
+
7
+ puts "..and the end"
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts "hello! 1"
4
+ STDOUT.flush
5
+ sleep 2
6
+ puts "hello! 2"
7
+ STDOUT.flush
8
+ sleep 2
9
+ puts "hello! 3"
10
+ STDOUT.flush
11
+ sleep 2
12
+ raise "This is sample exception"
data/examples/ex-sh.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'cli_tools/include'
2
+
3
+ if ARGV.size == 0
4
+ puts "Usage: #{__FILE__} <command> [arg1] [arg2] ..."
5
+ puts "Examples:"
6
+ puts " #{__FILE__} ruby test-err.rb"
7
+ puts " #{__FILE__} ruby test-putr.rb"
8
+ exit -1
9
+ end
10
+
11
+ out = ''
12
+ captured = ''
13
+ begin
14
+ sh ARGV.join(' '), true, captured do |char|
15
+ putc char
16
+ end
17
+ puts esc_green "OK"
18
+ puts "captured output (captured):"
19
+ puts captured
20
+ puts
21
+ rescue ShellExecutionError => e
22
+ puts esc_red "ERROR: #{e}, status:#{e.status}, output:"
23
+ puts e.output
24
+ puts
25
+ end
@@ -1,3 +1,3 @@
1
1
  module CliTools
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
@@ -52,6 +52,15 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - cli_tools.gemspec
55
+ - examples/ex-ansi.rb
56
+ - examples/ex-block.rb
57
+ - examples/ex-err.rb
58
+ - examples/ex-kb-getkey.rb
59
+ - examples/ex-ok.rb
60
+ - examples/ex-putr.rb
61
+ - examples/ex-puts-vs-putr.rb
62
+ - examples/ex-raise.rb
63
+ - examples/ex-sh.rb
55
64
  - lib/cli_tools.rb
56
65
  - lib/cli_tools/console.rb
57
66
  - lib/cli_tools/include.rb