terminal_helpers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -0
- data/README.md +95 -0
- data/Rakefile +21 -0
- data/lib/terminal_helpers/colors.rb +18 -0
- data/lib/terminal_helpers/errors.rb +6 -0
- data/lib/terminal_helpers/helpers.rb +136 -0
- data/lib/terminal_helpers/validations.rb +30 -0
- data/lib/terminal_helpers/version.rb +3 -0
- data/lib/terminal_helpers.rb +19 -0
- data/terminal_helpers.gemspec +16 -0
- metadata +55 -0
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
*.swp
|
4
|
+
*.tmproj
|
5
|
+
*~
|
6
|
+
.DS_Store
|
7
|
+
.\#*
|
8
|
+
.bundle
|
9
|
+
.config
|
10
|
+
.yardoc
|
11
|
+
Gemfile.lock
|
12
|
+
InstalledFiles
|
13
|
+
\#*
|
14
|
+
_yardoc
|
15
|
+
coverage
|
16
|
+
doc/
|
17
|
+
lib/bundler/man
|
18
|
+
pkg
|
19
|
+
rdoc
|
20
|
+
spec/reports
|
21
|
+
test/tmp
|
22
|
+
test/version_tmp
|
23
|
+
tmp
|
24
|
+
tmtags
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# TerminalHelpers
|
2
|
+
|
3
|
+
This is a collection of helper method for your console-based applications.
|
4
|
+
|
5
|
+
It does not have any gem dependencies and could be easily plugged-in into your existing code.
|
6
|
+
|
7
|
+
Was inspired by ```heroku``` CLI app.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
You can install it using rubygems:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install terminal_helpers
|
15
|
+
```
|
16
|
+
|
17
|
+
Or build it:
|
18
|
+
|
19
|
+
```
|
20
|
+
rake install
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
To start using terminal_helpers just include it into your app or class:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'terminal_helpers'
|
29
|
+
include TerminalHelpers
|
30
|
+
|
31
|
+
# or if you have session or somethin
|
32
|
+
class Session
|
33
|
+
include TerminalHelpers
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
### Input helpers
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
name = ask('Your name')
|
41
|
+
email = ask('Email address', :required => true, :format => :email)
|
42
|
+
zipcode = ask('Zipcode', :format => :zipcode)
|
43
|
+
domain = ask('Domain', :format => :domain)
|
44
|
+
password = ask_password(:confirm => true)
|
45
|
+
sure = ask_yesno('Are you sure about this?')
|
46
|
+
```
|
47
|
+
|
48
|
+
### Message helpers
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
empty_line # print just an empty line
|
52
|
+
empty_line 50 # print 50 empty lines
|
53
|
+
prompt # print '>' and wait for user input
|
54
|
+
```
|
55
|
+
|
56
|
+
Various informative methods:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
info 'This is INFO message!'
|
60
|
+
display 'This is another INFO message!'
|
61
|
+
success 'This is SUCCESS message!'
|
62
|
+
warning 'This is WARNING message!'
|
63
|
+
error 'This is ERROR message', true # true means termination after printing
|
64
|
+
```
|
65
|
+
|
66
|
+
To enable disable colors support:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
TerminalHelpers.use_colors = false # colors are enabled by default
|
70
|
+
```
|
71
|
+
|
72
|
+
### Extras
|
73
|
+
|
74
|
+
Execute a shell command:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
result = shell 'uptime'
|
78
|
+
```
|
79
|
+
|
80
|
+
Enable/disable terminal echo (helpful for password input):
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
echo_on
|
84
|
+
echo_off
|
85
|
+
```
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
Copyright © 2011 Dan Sosedoff.
|
90
|
+
|
91
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
92
|
+
|
93
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
94
|
+
|
95
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
task :install do
|
2
|
+
gem_name = "terminal_helpers"
|
3
|
+
|
4
|
+
require 'fileutils'
|
5
|
+
require File.expand_path("../lib/#{gem_name}/version", __FILE__)
|
6
|
+
|
7
|
+
gem_file = "#{gem_name}-#{TerminalHelpers::VERSION}.gem"
|
8
|
+
|
9
|
+
if File.exists?(gem_file)
|
10
|
+
FileUtils.rm_f(gem_file)
|
11
|
+
end
|
12
|
+
|
13
|
+
puts "-> Uninstalling..."
|
14
|
+
puts `gem uninstall -x #{gem_name} --version=#{TerminalHelpers::VERSION}`
|
15
|
+
|
16
|
+
puts "-> Building..."
|
17
|
+
puts `gem build #{gem_name}.gemspec`
|
18
|
+
|
19
|
+
puts "-> Installing..."
|
20
|
+
puts `gem install #{gem_file}`
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Colors
|
2
|
+
def red; colorize(self, "\e[1m\e[31m"); end
|
3
|
+
def green; colorize(self, "\e[1m\e[32m"); end
|
4
|
+
def dark_green; colorize(self, "\e[32m"); end
|
5
|
+
def yellow; colorize(self, "\e[1m\e[33m"); end
|
6
|
+
def blue; colorize(self, "\e[1m\e[34m"); end
|
7
|
+
def dark_blue; colorize(self, "\e[34m"); end
|
8
|
+
def purple; colorize(self, "\e[1m\e[35m"); end
|
9
|
+
def magenta; colorize(self, "\e[1m\e[36m"); end
|
10
|
+
|
11
|
+
def colorize(text, color_code)
|
12
|
+
"#{color_code}#{text}\e[0m"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class String
|
17
|
+
include Colors
|
18
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module TerminalHelpers
|
4
|
+
# Ask for the user input
|
5
|
+
#
|
6
|
+
# title - Input prompt text
|
7
|
+
# options - Input options
|
8
|
+
#
|
9
|
+
# options[:default] - Default value if input is blank
|
10
|
+
# options[:required] - Make input mandatory (default: false)
|
11
|
+
# options[:format] - Validate input data using format
|
12
|
+
# options[:echo] - Enable/disable echo (default: true)
|
13
|
+
#
|
14
|
+
def ask(title, options={})
|
15
|
+
title_str = title.dup
|
16
|
+
title_str += " [#{options[:default]}]" unless options[:default].to_s.empty?
|
17
|
+
title_str += ": "
|
18
|
+
|
19
|
+
echo_off if options[:echo] == false
|
20
|
+
|
21
|
+
print(title_str)
|
22
|
+
result = gets.strip
|
23
|
+
result = options[:default] if result.empty? && options.key?(:default)
|
24
|
+
#result = result.scan(/[a-z\d\_\-]{1,}/i) if options.key?(:array)
|
25
|
+
|
26
|
+
if result.empty? && options[:required]
|
27
|
+
empty_line if options[:echo] == false
|
28
|
+
return ask(title, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
empty_line if options[:echo] == false
|
32
|
+
echo_on
|
33
|
+
|
34
|
+
Validations.validate(result, options[:format], true) if options[:format]
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
# Prompt for password
|
39
|
+
#
|
40
|
+
# options[:confirm] - Require a password confirmation (default: false)
|
41
|
+
#
|
42
|
+
def ask_password(options={})
|
43
|
+
password = ask('Password', :required => true, :echo => false)
|
44
|
+
if options[:confirm] == true
|
45
|
+
confirmation = ask('Confirm password', :required => true, :echo => false)
|
46
|
+
if confirmation != password
|
47
|
+
raise InputError, "Password confirmation should match."
|
48
|
+
end
|
49
|
+
end
|
50
|
+
password
|
51
|
+
end
|
52
|
+
|
53
|
+
# Prompt for Y/N question
|
54
|
+
#
|
55
|
+
def ask_yesno(title, required='y', options={})
|
56
|
+
answer = ask("#{title} (y/n)", options).downcase
|
57
|
+
answer == required
|
58
|
+
end
|
59
|
+
|
60
|
+
# Prints an empty line N times
|
61
|
+
#
|
62
|
+
def empty_line(num=1)
|
63
|
+
num.times { info("") }
|
64
|
+
end
|
65
|
+
|
66
|
+
# Regular prompt with prefix
|
67
|
+
#
|
68
|
+
def prompt(prefix='> ')
|
69
|
+
print("#{prefix}")
|
70
|
+
gets.strip
|
71
|
+
end
|
72
|
+
|
73
|
+
# Print an information message
|
74
|
+
#
|
75
|
+
# message - Information text
|
76
|
+
# color - Specify message color (default: none)
|
77
|
+
#
|
78
|
+
def info(message, color=nil)
|
79
|
+
if TerminalHelpers.use_colors
|
80
|
+
message = message.send(color) unless color.nil?
|
81
|
+
end
|
82
|
+
STDOUT.puts(message)
|
83
|
+
STDOUT.flush
|
84
|
+
end
|
85
|
+
|
86
|
+
# Show success message
|
87
|
+
#
|
88
|
+
def success(message)
|
89
|
+
info(message, :green)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Show warning message
|
93
|
+
#
|
94
|
+
def warning(message)
|
95
|
+
info(message, :yellow)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Show error message
|
99
|
+
#
|
100
|
+
def error(message, exit_after=false)
|
101
|
+
if TerminalHelpers.use_colors
|
102
|
+
message = message.red
|
103
|
+
end
|
104
|
+
STDERR.puts(message)
|
105
|
+
exit(1) if exit_after
|
106
|
+
end
|
107
|
+
|
108
|
+
# Print message
|
109
|
+
#
|
110
|
+
def display(message, newline=true)
|
111
|
+
if newline
|
112
|
+
STDOUT.puts(message)
|
113
|
+
else
|
114
|
+
STDOUT.print(message)
|
115
|
+
STDOUT.flush
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Execure a shell command
|
120
|
+
#
|
121
|
+
def shell(cmd)
|
122
|
+
FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`}
|
123
|
+
end
|
124
|
+
|
125
|
+
# Disables terminal prompt
|
126
|
+
#
|
127
|
+
def echo_off
|
128
|
+
system("stty -echo")
|
129
|
+
end
|
130
|
+
|
131
|
+
# Enables terminal prompt
|
132
|
+
#
|
133
|
+
def echo_on
|
134
|
+
system("stty echo")
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module TerminalHelpers
|
2
|
+
module Validations
|
3
|
+
extend self
|
4
|
+
|
5
|
+
FORMATS = {
|
6
|
+
:domain => /^[a-z\d-]+(\.[a-z\d-]+)*\.(([\d]{1,3})|([a-z]{2,3})|(aero|coop|info|museum|name))$/i,
|
7
|
+
:email => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
|
8
|
+
:zipcode => /^[\d]{5}$/,
|
9
|
+
:ipv4 => /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/
|
10
|
+
}
|
11
|
+
|
12
|
+
# Validate data against provided format
|
13
|
+
#
|
14
|
+
def validate(value, format, raise_error=false)
|
15
|
+
unless FORMATS.key?(format)
|
16
|
+
raise FormatError, "Invalid data format: #{format}"
|
17
|
+
end
|
18
|
+
result = value =~ FORMATS[format] ? true : false
|
19
|
+
if raise_error && !result
|
20
|
+
raise ValidationError, "Invalid value \"#{value}\" for #{format}"
|
21
|
+
end
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def email(value) ; validate(value, :email) ; end
|
26
|
+
def domain(value) ; validate(value, :domain) ; end
|
27
|
+
def zipcode(value) ; validate(value, :zipcode) ; end
|
28
|
+
def ipv4(value) ; validate(value, :ipv4) ; end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'terminal_helpers/version'
|
2
|
+
require 'terminal_helpers/errors'
|
3
|
+
require 'terminal_helpers/validations'
|
4
|
+
require 'terminal_helpers/colors'
|
5
|
+
require 'terminal_helpers/helpers'
|
6
|
+
|
7
|
+
module TerminalHelpers
|
8
|
+
@@use_colors = true
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def use_colors
|
12
|
+
@@use_colors
|
13
|
+
end
|
14
|
+
|
15
|
+
def use_colors=(value)
|
16
|
+
@@use_colors = value == true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path('../lib/terminal_helpers/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "terminal_helpers"
|
5
|
+
s.version = TerminalHelpers::VERSION.dup
|
6
|
+
s.summary = "Various helpers for console-based applications"
|
7
|
+
s.description = "Various helpers for console-based applications"
|
8
|
+
s.homepage = "http://github.com/sosedoff/terminal_helpers"
|
9
|
+
s.authors = ["Dan Sosedoff"]
|
10
|
+
s.email = ["dan.sosedoff@gmail.com"]
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: terminal_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dan Sosedoff
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-05 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Various helpers for console-based applications
|
15
|
+
email:
|
16
|
+
- dan.sosedoff@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/terminal_helpers.rb
|
25
|
+
- lib/terminal_helpers/colors.rb
|
26
|
+
- lib/terminal_helpers/errors.rb
|
27
|
+
- lib/terminal_helpers/helpers.rb
|
28
|
+
- lib/terminal_helpers/validations.rb
|
29
|
+
- lib/terminal_helpers/version.rb
|
30
|
+
- terminal_helpers.gemspec
|
31
|
+
homepage: http://github.com/sosedoff/terminal_helpers
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.10
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Various helpers for console-based applications
|
55
|
+
test_files: []
|