console_view_helper 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/console_view_helper.rb +162 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ODQwZGVlN2QyY2I2MWJkNjQ1Yjk0Y2NhNDhlYTZlYTFmYTJkMmQwYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NDAyNmUzYjZkZGFiNzA3MzkyOWJmMjdiNDY0NTFhN2RkMjI2ZWI3Zg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmFiZGE2MmY2MjliMjU2NjgwZWU1Y2M0Y2IwNzAzZjk4NDUxMGZkMGVkYWU5
|
10
|
+
Nzg3ZTMxNjkxNDI0ZmFiY2Y5M2QzYmQ1ZTM3NGNiZjRlZTBmYTRiNDdhM2M2
|
11
|
+
YzU3ODVhOGZmYWI1ODY3NGM3Yzc0Njc3ZjQwNDBkZmI3MGRmODM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTJjOGM5OWY2Yjk1MTZhYTQ3YjgyMTVjYjdhNTgyNjA4Yzg3NzkzOWMzNTE0
|
14
|
+
ODA3NjI3ODYxOWZjZDcyNDNjOTU3ZjFlYzNhMzAyMWI4ZmJkYTUwMmExNDIz
|
15
|
+
N2FiYmExMzMyNDA5NDA0NmM2ZDljMTU3ZDM3ZDJiMDNmZWE2NDk=
|
@@ -0,0 +1,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
|
+
subtitle = opts[:subtitle]
|
113
|
+
base_width = (subtitle && subtitle.length > title.length ? subtitle.length : title.length) + 4
|
114
|
+
width = opts[:width] || base_width
|
115
|
+
n = opts[:indent] || 0
|
116
|
+
banner = idt(n) + astk(width + 2) + nl
|
117
|
+
banner << idt(n) + astk + whites(width) + astk + nl
|
118
|
+
banner << idt(n) + astk + align(title, width, :center) + astk + nl
|
119
|
+
banner << idt(n) + astk + align(subtitle, width, :center) + astk + nl if subtitle
|
120
|
+
banner << idt(n) + astk + whites(width) + astk + nl
|
121
|
+
banner << idt(n) + astk(width + 2) + nl
|
122
|
+
end
|
123
|
+
|
124
|
+
# User input
|
125
|
+
def input(label = '>>', n = 0)
|
126
|
+
printi label + whites, n
|
127
|
+
gets.strip.chomp
|
128
|
+
end
|
129
|
+
|
130
|
+
# User input, but hidden
|
131
|
+
def hidden_input(label = '>>', n = 0)
|
132
|
+
printi label + whites, n
|
133
|
+
STDIN.noecho(&:gets).strip.chomp
|
134
|
+
end
|
135
|
+
|
136
|
+
# ---
|
137
|
+
module_function :idt, :astk, :nl, :hyphen, :underscore, :whites, :align, :printi, :putsi, :loading_effect, :explain, :colorize, :banner, :input, :hidden_input
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
# -------------------------- CONSOLE VIEW HELPER ------------------------------
|
142
|
+
# - Helper methods to build simple and clean console application interfaces.
|
143
|
+
|
144
|
+
# --- HOW TO USE ---
|
145
|
+
# - Require and optionally include the module in your view file
|
146
|
+
# require 'cosole_view_helper'
|
147
|
+
# include ConsoleViewHelper
|
148
|
+
|
149
|
+
# - When just required call the methods from the module name
|
150
|
+
# ConsoleViewHelper.input 'username:'
|
151
|
+
|
152
|
+
# - When included call the methods directly
|
153
|
+
# input 'username:'
|
154
|
+
|
155
|
+
# --- WINDOW USERS ---
|
156
|
+
# - In order to use the 'colorize' method you will need the 'win32console' gem.
|
157
|
+
|
158
|
+
# - Get it:
|
159
|
+
# gem install 'win32console'
|
160
|
+
|
161
|
+
# - Require it:
|
162
|
+
# require 'win32console'
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: console_view_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alfonso Mancilla
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colored
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.2'
|
27
|
+
description: Set of methods and components I use commonly when building console application
|
28
|
+
interfaces.
|
29
|
+
email: almancill@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/console_view_helper.rb
|
35
|
+
homepage: http://rubygems.org/gems/console_view_helper
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.4.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Helper methods to build simple and clean console application interfaces
|
59
|
+
test_files: []
|