console_view_helper 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/console_view_helper.rb +201 -162
- metadata +17 -7
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
N2I4ZWY4MmQ5NjIwYWI1NWJkZjUzZWJhMTE3OGM1YTk3NmJjN2QyMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWQ3NWU0ZGRjZDVkNzdiYjEzNmE3YmU3NTkwNDZiM2Q1MmU5ZWU0Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmEwYjljOWZlZjUwZGRhOGU1NTU3NjNhNzk4Yzk2NTRkY2E3M2U4YjIwZTRl
|
10
|
+
N2RiNDA0OGFhMDRlYTA1NWJiODAxNWY2ZGI5N2U1ZDk2YTUwMTI0OTI5YjM4
|
11
|
+
NzEzYjQ5YmFhYWZkNzAzZTQzYjYyYTQ5ZmJiYmE3OTUwNWEyZjk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTZlZjdmZmUzNDM2OGJjZjZjZDRmZWMwZWI0NjIxOGJjN2U5OTMwY2NlOWE3
|
14
|
+
YzNhYWY2NGUxYmIxYmI2MmE4NWE0MWUwNDY4N2VjYmZiM2Y4Y2JkZWEzYTBk
|
15
|
+
ZWRkZWIxNjFmOTExNTIzODQ2MzU4ZGYwMDUwMDZjMTNhODIyZGQ=
|
data/lib/console_view_helper.rb
CHANGED
@@ -1,162 +1,201 @@
|
|
1
|
-
# Encoding: utf-8
|
2
|
-
|
3
|
-
# Requires
|
4
|
-
require 'io/console'
|
5
|
-
require 'colored'
|
6
|
-
|
7
|
-
module ConsoleViewHelper
|
8
|
-
|
9
|
-
#########################
|
10
|
-
# GENERAL #
|
11
|
-
#########################
|
12
|
-
|
13
|
-
# Indent n times
|
14
|
-
def idt(n = 1)
|
15
|
-
"\t" * n
|
16
|
-
end
|
17
|
-
|
18
|
-
# Asterisk n times
|
19
|
-
def astk(n = 1)
|
20
|
-
'*' * n
|
21
|
-
end
|
22
|
-
|
23
|
-
# New n lines
|
24
|
-
def nl(n = 1)
|
25
|
-
"\n" * n
|
26
|
-
end
|
27
|
-
|
28
|
-
# Hyphen n times
|
29
|
-
def hyphen(n = 1)
|
30
|
-
'-' * n
|
31
|
-
end
|
32
|
-
|
33
|
-
# Underscore n times
|
34
|
-
def underscore(n = 1)
|
35
|
-
'_' * n
|
36
|
-
end
|
37
|
-
|
38
|
-
# Bar n times
|
39
|
-
def bar(n = 1)
|
40
|
-
'|' * n
|
41
|
-
end
|
42
|
-
|
43
|
-
# Whitespace n times
|
44
|
-
def whites(n = 1)
|
45
|
-
' ' * n
|
46
|
-
end
|
47
|
-
|
48
|
-
# Align text
|
49
|
-
def align(text, size, direction = :left, append = ' ')
|
50
|
-
case direction
|
51
|
-
when :right
|
52
|
-
text.rjust(size, append)
|
53
|
-
when :center
|
54
|
-
text.center(size, append)
|
55
|
-
else
|
56
|
-
text.ljust(size, append)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# 'print' text indented n times
|
61
|
-
def printi(text, n = 1)
|
62
|
-
print idt(n) + text
|
63
|
-
end
|
64
|
-
|
65
|
-
# 'put' text indented n times
|
66
|
-
def putsi(text, n = 1)
|
67
|
-
puts idt(n) + text
|
68
|
-
end
|
69
|
-
|
70
|
-
# Display a fake loading effect
|
71
|
-
def loading_effect(n = 3, opts = {})
|
72
|
-
delay = opts[:delay] || 0.3
|
73
|
-
symbol = opts[:symbol] || '.'
|
74
|
-
1.upto(n) do
|
75
|
-
print symbol
|
76
|
-
sleep delay
|
77
|
-
end
|
78
|
-
nil
|
79
|
-
end
|
80
|
-
|
81
|
-
# Explain the action being performed
|
82
|
-
def explain(doing_txt, done_txt, n = 1, &block)
|
83
|
-
printi doing_txt, n
|
84
|
-
loading_effect
|
85
|
-
result = block.call
|
86
|
-
puts done_txt
|
87
|
-
result
|
88
|
-
end
|
89
|
-
|
90
|
-
# Highlight text with color
|
91
|
-
def colorize(text, status = :normal)
|
92
|
-
case status
|
93
|
-
when :success
|
94
|
-
text.green
|
95
|
-
when :error
|
96
|
-
text.red
|
97
|
-
when :warning
|
98
|
-
text.yellow
|
99
|
-
when :neutral
|
100
|
-
text.blue
|
101
|
-
else
|
102
|
-
text.white
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
#########################
|
107
|
-
# COMPONENTS #
|
108
|
-
#########################
|
109
|
-
|
110
|
-
# Banner
|
111
|
-
def banner(title, opts = {})
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
banner
|
119
|
-
banner << idt(n) +
|
120
|
-
banner << idt(n) +
|
121
|
-
banner << idt(n) +
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
end
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
1
|
+
# Encoding: utf-8
|
2
|
+
|
3
|
+
# Requires
|
4
|
+
require 'io/console'
|
5
|
+
require 'colored'
|
6
|
+
|
7
|
+
module ConsoleViewHelper
|
8
|
+
|
9
|
+
#########################
|
10
|
+
# GENERAL #
|
11
|
+
#########################
|
12
|
+
|
13
|
+
# Indent n times
|
14
|
+
def idt(n = 1)
|
15
|
+
"\t" * n
|
16
|
+
end
|
17
|
+
|
18
|
+
# Asterisk n times
|
19
|
+
def astk(n = 1)
|
20
|
+
'*' * n
|
21
|
+
end
|
22
|
+
|
23
|
+
# New n lines
|
24
|
+
def nl(n = 1)
|
25
|
+
"\n" * n
|
26
|
+
end
|
27
|
+
|
28
|
+
# Hyphen n times
|
29
|
+
def hyphen(n = 1)
|
30
|
+
'-' * n
|
31
|
+
end
|
32
|
+
|
33
|
+
# Underscore n times
|
34
|
+
def underscore(n = 1)
|
35
|
+
'_' * n
|
36
|
+
end
|
37
|
+
|
38
|
+
# Bar n times
|
39
|
+
def bar(n = 1)
|
40
|
+
'|' * n
|
41
|
+
end
|
42
|
+
|
43
|
+
# Whitespace n times
|
44
|
+
def whites(n = 1)
|
45
|
+
' ' * n
|
46
|
+
end
|
47
|
+
|
48
|
+
# Align text
|
49
|
+
def align(text, size, direction = :left, append = ' ')
|
50
|
+
case direction
|
51
|
+
when :right
|
52
|
+
text.rjust(size, append)
|
53
|
+
when :center
|
54
|
+
text.center(size, append)
|
55
|
+
else
|
56
|
+
text.ljust(size, append)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# 'print' text indented n times
|
61
|
+
def printi(text, n = 1)
|
62
|
+
print idt(n) + text
|
63
|
+
end
|
64
|
+
|
65
|
+
# 'put' text indented n times
|
66
|
+
def putsi(text, n = 1)
|
67
|
+
puts idt(n) + text
|
68
|
+
end
|
69
|
+
|
70
|
+
# Display a fake loading effect
|
71
|
+
def loading_effect(n = 3, opts = {})
|
72
|
+
delay = opts[:delay] || 0.3
|
73
|
+
symbol = opts[:symbol] || '.'
|
74
|
+
1.upto(n) do
|
75
|
+
print symbol
|
76
|
+
sleep delay
|
77
|
+
end
|
78
|
+
nil
|
79
|
+
end
|
80
|
+
|
81
|
+
# Explain the action being performed
|
82
|
+
def explain(doing_txt, done_txt = '', n = 1, &block)
|
83
|
+
printi doing_txt, n
|
84
|
+
loading_effect
|
85
|
+
result = block.call
|
86
|
+
puts done_txt
|
87
|
+
result
|
88
|
+
end
|
89
|
+
|
90
|
+
# Highlight text with color
|
91
|
+
def colorize(text, status = :normal)
|
92
|
+
case status
|
93
|
+
when :success
|
94
|
+
text.green
|
95
|
+
when :error
|
96
|
+
text.red
|
97
|
+
when :warning
|
98
|
+
text.yellow
|
99
|
+
when :neutral
|
100
|
+
text.blue
|
101
|
+
else
|
102
|
+
text.white
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
#########################
|
107
|
+
# COMPONENTS #
|
108
|
+
#########################
|
109
|
+
|
110
|
+
# Banner
|
111
|
+
def banner(title, opts = {})
|
112
|
+
n = opts[:indent] || 0
|
113
|
+
symbol = opts[:symbol] || '*'
|
114
|
+
subtitle = opts[:subtitle]
|
115
|
+
base_width = (subtitle && subtitle.length > title.length ? subtitle.length : title.length) + 4
|
116
|
+
width = opts[:width] || base_width
|
117
|
+
raise ArgumentError.new("Specified width can't be minor thant #{base_width}. Increase or remove the width value.") if width < base_width
|
118
|
+
banner = idt(n) + (symbol * (width + 2)) + nl
|
119
|
+
banner << idt(n) + symbol + whites(width) + symbol + nl
|
120
|
+
banner << idt(n) + symbol + align(title, width, :center) + symbol + nl
|
121
|
+
banner << idt(n) + symbol + align(subtitle, width, :center) + symbol + nl if subtitle
|
122
|
+
banner << idt(n) + symbol + whites(width) + symbol + nl
|
123
|
+
banner << idt(n) + (symbol * (width + 2)) + nl
|
124
|
+
end
|
125
|
+
|
126
|
+
# Table
|
127
|
+
def table(columns, opts = {})
|
128
|
+
raise ArgumentError.new('Pass table columns as an array of arrays') unless columns.is_a?(Array) && columns.select { |item| !item.is_a?(Array) }.empty?
|
129
|
+
# Set options
|
130
|
+
n = opts[:indent] || 0
|
131
|
+
cell_width = opts[:cell_width] || 12
|
132
|
+
cell_separator = opts[:cell_separator] || bar
|
133
|
+
cell_border = opts[:cell_border] || hyphen
|
134
|
+
if opts[:header]
|
135
|
+
opts[:header].each_with_index do |th, i|
|
136
|
+
columns.push [] unless columns[i]
|
137
|
+
columns[i].unshift(th)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
td_width = cell_width - 2
|
141
|
+
tr_width = (cell_width * columns.length) + columns.length + 1
|
142
|
+
|
143
|
+
# Build table
|
144
|
+
pos, table = 0, ''
|
145
|
+
begin
|
146
|
+
tr_empty_elems = 0
|
147
|
+
tr_str = idt(n) + (cell_border * tr_width) + nl + cell_separator
|
148
|
+
columns.each do |column|
|
149
|
+
td = if column[pos]
|
150
|
+
column[pos]
|
151
|
+
else
|
152
|
+
tr_empty_elems += 1
|
153
|
+
''
|
154
|
+
end
|
155
|
+
td = align(td[0..td_width], cell_width, :center)
|
156
|
+
tr_str << td + cell_separator
|
157
|
+
end
|
158
|
+
tr_str << nl
|
159
|
+
table << tr_str if tr_empty_elems != columns.length
|
160
|
+
pos += 1
|
161
|
+
end while tr_empty_elems != columns.length
|
162
|
+
table << idt(n) + (cell_border * tr_width)
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
# List (unordered)
|
167
|
+
def list(items, opts = {})
|
168
|
+
raise ArgumentError.new('Pass list items in an array.') unless items.is_a? Array
|
169
|
+
n = opts[:indent] || 0
|
170
|
+
li_gap = opts[:li_gap] || 1
|
171
|
+
symbol = opts[:symbol] || '•'
|
172
|
+
list = idt(n)
|
173
|
+
items.each_with_index do |li, i|
|
174
|
+
symbol = opts[:ordered] ? "#{i + 1}." : symbol
|
175
|
+
list << symbol + ' ' + li + nl(li_gap) + idt(n)
|
176
|
+
end
|
177
|
+
list
|
178
|
+
end
|
179
|
+
alias_method :ulist, :list
|
180
|
+
|
181
|
+
# Ordered List
|
182
|
+
def olist(items, opts = {})
|
183
|
+
list items, opts.merge(ordered: true)
|
184
|
+
end
|
185
|
+
alias_method :menu, :olist
|
186
|
+
|
187
|
+
# User input
|
188
|
+
def input(label = '>>', n = 0)
|
189
|
+
printi label + whites, n
|
190
|
+
gets.strip.chomp
|
191
|
+
end
|
192
|
+
|
193
|
+
# User input, but hidden
|
194
|
+
def hidden_input(label = '>>', n = 0)
|
195
|
+
printi label + whites, n
|
196
|
+
STDIN.noecho(&:gets).strip.chomp
|
197
|
+
end
|
198
|
+
|
199
|
+
# ---
|
200
|
+
module_function :idt, :astk, :nl, :hyphen, :underscore, :bar, :whites, :align, :printi, :putsi, :loading_effect, :explain, :colorize, :banner, :menu, :table, :list, :olist, :ulist, :input, :hidden_input
|
201
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_view_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alfonso Mancilla
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colored
|
@@ -24,15 +24,24 @@ dependencies:
|
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.2'
|
27
|
-
description:
|
28
|
-
interfaces.
|
29
|
-
|
27
|
+
description: ! 'The console_view_helper library is used to build clean and beautiful
|
28
|
+
console application interfaces.
|
29
|
+
|
30
|
+
Customizable Components such as banners, tables, menus, lists, text inputs, hidden
|
31
|
+
text inputs and
|
32
|
+
|
33
|
+
methods as colorize, putsi, printi, align, explain, among others,
|
34
|
+
|
35
|
+
will help you build a good console application interface with less code and in less
|
36
|
+
time.'
|
37
|
+
email:
|
38
|
+
- almancill@gmail.com
|
30
39
|
executables: []
|
31
40
|
extensions: []
|
32
41
|
extra_rdoc_files: []
|
33
42
|
files:
|
34
43
|
- lib/console_view_helper.rb
|
35
|
-
homepage:
|
44
|
+
homepage: https://github.com/ammancilla/console_view_helper
|
36
45
|
licenses:
|
37
46
|
- MIT
|
38
47
|
metadata: {}
|
@@ -55,5 +64,6 @@ rubyforge_project:
|
|
55
64
|
rubygems_version: 2.4.5
|
56
65
|
signing_key:
|
57
66
|
specification_version: 4
|
58
|
-
summary:
|
67
|
+
summary: Set of components and methods used to build clean and beautiful console application
|
68
|
+
interfaces.
|
59
69
|
test_files: []
|