term 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e51b6635d1ddd8ec42d76472d61136d793ce0fdc
4
+ data.tar.gz: 136a6f2a1c9806d3ae1151c2ac6018f62fa5cd3f
5
+ SHA512:
6
+ metadata.gz: 5b19ee7b84affd20ab6d3bafa73f9fe1bd85cfbafa7fa32e02130130b2ac9cce41fbf98fe49db40a842b45dc1439dc4bca8ee688cd84858aba4d56742f8b35f8
7
+ data.tar.gz: bcba95e7207d0ac572c1adf28d81380fc3d246971a102e527908b52af6e851c2dbc639011159aae106856b58b9176ddfbc839303af67e214820a56fa9969d4ba
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 cuihq
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Term
2
+
3
+ A toolkit of Terminal. Rich support for color, mouse, and lightweight component.
4
+
5
+ > http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
6
+
7
+ ## Installation
8
+
9
+ ### required
10
+
11
+ > \>ruby 2.0
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'term'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install term
26
+
27
+ ## Usage
28
+
29
+ see the examples.
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cuihq/term. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ railties_path = File.expand_path('..', __FILE__)
4
+ $:.unshift(railties_path)
5
+
6
+ require 'term/version'
7
+ require 'term/bell'
8
+ require 'term/checkbox'
9
+ require 'term/code'
10
+ require 'term/color'
11
+ require 'term/confirm'
12
+ require 'term/cursor'
13
+ require 'term/font'
14
+ require 'term/input'
15
+ require 'term/password'
16
+ require 'term/process_bar'
17
+ require 'term/qrcode'
18
+ require 'term/redio'
19
+ require 'term/style'
20
+ require 'term/table'
21
+ require 'term/window'
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ # @author {mailto:cuihaiqin@gmail.com cuihq}
4
+ class IO
5
+ # ring a bell to a terminal.
6
+ #
7
+ # @example
8
+ # IO.bell
9
+ # @return self
10
+ def self.bell
11
+ print "\a"
12
+ self
13
+ end
14
+ end
@@ -0,0 +1,85 @@
1
+ # coding: utf-8
2
+
3
+ require 'term/cursor'
4
+ require 'term/input'
5
+
6
+ # @author {mailto:cuihaiqin@gmail.com cuihq}
7
+ module Enumerable
8
+ # the checkbox widget.
9
+ #
10
+ # A checkbox allows a user to select same value from Enumerable.
11
+ # @example
12
+ # ['a', 'b', 7].checkbox
13
+ # @return [Array] the selected of values
14
+ def checkbox
15
+ size.times { print $/ }
16
+ @begin_pos = IO.up(size).hide.pos
17
+ @checkbox_res = []
18
+ process_input_checkbox
19
+ IO.show
20
+ @checkbox_res
21
+ end
22
+
23
+ private
24
+
25
+ # select checkbox item.
26
+ def select_checkbox_item(index)
27
+ index = index.to_s.to_i % size
28
+ @checkbox_res.delete(index) || @checkbox_res.push(index)
29
+ print_checkbox index
30
+ end
31
+
32
+ # print checkbox.
33
+ def print_checkbox(index = 0)
34
+ IO.to @begin_pos
35
+ @res = index.to_s.to_i % size
36
+ @item_ys = []
37
+ each_with_index do |item, num|
38
+ print_checkbox_item item, num
39
+ end
40
+ end
41
+
42
+ # print checkbox item.
43
+ def print_checkbox_item(item, num)
44
+ @item_ys[num] = IO.pos[:y]
45
+ num == @res ? print('➡️ ') : print(' ')
46
+ if @checkbox_res.include? num
47
+ print " ☑️ #{num}. #{item}#{$/}"
48
+ else
49
+ print " ⬜️ #{num}. #{item}#{$/}"
50
+ end
51
+ end
52
+
53
+ # process checkbox input.
54
+ def process_input_checkbox
55
+ print_checkbox
56
+ @checkbox_loop = true
57
+ while @checkbox_loop
58
+ input = IO.input
59
+ process_checkbox_mouse_click input
60
+ process_checkbox_keyboard_input input[:key]
61
+ end
62
+ end
63
+
64
+ # process checkbox mouse click event.
65
+ def process_checkbox_mouse_click(input)
66
+ type = input[:type]
67
+ pos = input[:pos]
68
+ if type == :left_pressed
69
+ list_item = @item_ys.find_index pos[:y]
70
+ select_checkbox_item list_item if list_item
71
+ end
72
+ end
73
+
74
+ # process checkbox keyboard input event.
75
+ def process_checkbox_keyboard_input(key)
76
+ case key
77
+ when :enter then @checkbox_loop = false
78
+ when /\d/ then select_checkbox_item(key)
79
+ when :down then print_checkbox(@res.succ)
80
+ when :up then print_checkbox(@res.pred)
81
+ when :space, :right, :left, :tab
82
+ select_checkbox_item @res
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,24 @@
1
+ # cording: utf-8
2
+
3
+ require 'rouge'
4
+
5
+ # @author {mailto:cuihaiqin@gmail.com cuihq}
6
+ class String
7
+ # a code syntax highlighter.
8
+ #
9
+ # @example Code
10
+ # code_str = <<-EOF
11
+ # class A
12
+ # def b
13
+ # puts 'c'
14
+ # end
15
+ # end
16
+ # EOF
17
+ # code_str.code 'ruby'
18
+ # @see https://github.com/jneen/rouge rouge
19
+ # @param name [String] the code name
20
+ # @return [String] the format code string with color
21
+ def code(name)
22
+ Rouge.highlight(self, name.to_s, 'terminal256')
23
+ end
24
+ end
@@ -0,0 +1,252 @@
1
+ # coding: utf-8
2
+
3
+ # @author {mailto:cuihaiqin@gmail.com cuihq}
4
+ class String
5
+ # string colors.
6
+ # @see http://www.termsys.demon.co.uk/vtansi.htm ANSI/VT100 Control sequences.
7
+ COLOR = {
8
+ black: 30,
9
+ red: 31,
10
+ green: 32,
11
+ yellow: 33,
12
+ blue: 34,
13
+ magenta: 35,
14
+ cyan: 36,
15
+ light_gray: 37,
16
+ default_color: 39,
17
+ dark_gray: 90,
18
+ light_red: 91,
19
+ light_green: 92,
20
+ light_yellow: 93,
21
+ light_blue: 94,
22
+ light_magenta: 95,
23
+ light_cyan: 96,
24
+ white: 97
25
+ }.freeze
26
+
27
+ # @!method black
28
+ # black string.
29
+ #
30
+ # @return [String] the formatting string
31
+
32
+ # @!method black_background
33
+ # black background string.
34
+ #
35
+ # @return [String] the formatting string
36
+
37
+ # @!method red
38
+ # red string.
39
+ #
40
+ # @return [String] the formatting string
41
+
42
+ # @!method red_background
43
+ # red background string.
44
+ #
45
+ # @return [String] the formatting string
46
+
47
+ # @!method green
48
+ # green string.
49
+ #
50
+ # @return [String] the formatting string
51
+
52
+ # @!method green_background
53
+ # green background string.
54
+ #
55
+ # @return [String] the formatting string
56
+
57
+ # @!method yellow
58
+ # yellow string.
59
+ #
60
+ # @return [String] the formatting string
61
+
62
+ # @!method yellow_background
63
+ # yellow background string.
64
+ #
65
+ # @return [String] the formatting string
66
+
67
+ # @!method blue
68
+ # blue string.
69
+ #
70
+ # @return [String] the formatting string
71
+
72
+ # @!method blue_background
73
+ # blue background string.
74
+ #
75
+ # @return [String] the formatting string
76
+
77
+ # @!method magenta
78
+ # magenta string.
79
+ #
80
+ # @return [String] the formatting string
81
+
82
+ # @!method magenta_background
83
+ # magenta background string.
84
+ #
85
+ # @return [String] the formatting string
86
+
87
+ # @!method cyan
88
+ # cyan string.
89
+ #
90
+ # @return [String] the formatting string
91
+
92
+ # @!method cyan_background
93
+ # cyan background string.
94
+ #
95
+ # @return [String] the formatting string
96
+
97
+ # @!method light_gray
98
+ # light gray string.
99
+ #
100
+ # @return [String] the formatting string
101
+
102
+ # @!method light_gray_background
103
+ # light gray background string.
104
+ #
105
+ # @return [String] the formatting string
106
+
107
+ # @!method default_color
108
+ # default color string.
109
+ #
110
+ # @return [String] the formatting string
111
+
112
+ # @!method default_color_background
113
+ # default color background string.
114
+ #
115
+ # @return [String] the formatting string
116
+
117
+ # @!method dark_gray
118
+ # dark gray string.
119
+ #
120
+ # @return [String] the formatting string
121
+
122
+ # @!method dark_gray_background
123
+ # dark gray background string.
124
+ #
125
+ # @return [String] the formatting string
126
+
127
+ # @!method light_red
128
+ # light red string.
129
+ #
130
+ # @return [String] the formatting string
131
+
132
+ # @!method light_red_background
133
+ # light red background string.
134
+ #
135
+ # @return [String] the formatting string
136
+
137
+ # @!method light_green
138
+ # light green string.
139
+ #
140
+ # @return [String] the formatting string
141
+
142
+ # @!method light_green_background
143
+ # light green background string.
144
+ #
145
+ # @return [String] the formatting string
146
+
147
+ # @!method light_yellow
148
+ # light yellow string.
149
+ #
150
+ # @return [String] the formatting string
151
+
152
+ # @!method light_yellow_background
153
+ # light yellow background string.
154
+ #
155
+ # @return [String] the formatting string
156
+
157
+ # @!method light_blue
158
+ # light blue string.
159
+ #
160
+ # @return [String] the formatting string
161
+
162
+ # @!method light_blue_background
163
+ # light blue background string.
164
+ #
165
+ # @return [String] the formatting string
166
+
167
+ # @!method light_magenta
168
+ # light magenta string.
169
+ #
170
+ # @return [String] the formatting string
171
+
172
+ # @!method light_magenta_background
173
+ # light magenta background string.
174
+ #
175
+ # @return [String] the formatting string
176
+
177
+ # @!method light_cyan
178
+ # light cyan string.
179
+ #
180
+ # @return [String] the formatting string
181
+
182
+ # @!method light_cyan_background
183
+ # light cyan background string.
184
+ #
185
+ # @return [String] the formatting string
186
+
187
+ # @!method white
188
+ # white string.
189
+ #
190
+ # @return [String] the formatting string
191
+
192
+ # @!method white_background
193
+ # white background string.
194
+ #
195
+ # @return [String] the formatting string
196
+ COLOR.each do |name, value|
197
+ define_method(name) do
198
+ "\e[#{value}m#{self}\e[0m"
199
+ end
200
+ define_method("#{name}_background") do
201
+ "\e[#{10 + value}m#{self}\e[0m"
202
+ end
203
+ end
204
+
205
+ # string color.
206
+ #
207
+ # @example color
208
+ # "mystring".color :black # keywords
209
+ # "mystring".color 123 # 256 colors
210
+ # "mystring".color '#000' # true color
211
+ # @param name [Object] the color name
212
+ # @return [String] the formatting string
213
+ def color(name)
214
+ set_256_color(name) || set_rbg_color(name) ||
215
+ set_keyword_color(name) || self
216
+ end
217
+
218
+ # string background color.
219
+ #
220
+ # @example background color
221
+ # "mystring".background_color :black # keywords
222
+ # "mystring".background_color 123 # 256 colors
223
+ # "mystring".background_color '#000' # true color
224
+ # @param name [Object] the background color name
225
+ # @return [String] the formatting string
226
+ def background_color(name)
227
+ set_256_color(name, 48) || set_rbg_color(name, 48) ||
228
+ set_keyword_color(name, '_background') || self
229
+ end
230
+
231
+ private
232
+
233
+ # set 256 color.
234
+ def set_256_color(name, num = 38)
235
+ color_index = name.to_i
236
+ "\e[#{num};5;#{color_index}m#{self}\e[0m" if color_index.between? 1, 256
237
+ end
238
+
239
+ # set rgb color.
240
+ def set_rbg_color(name, num = 38)
241
+ color_name = name.to_s
242
+ if /^#(?<r>\h{1,2})(?<g>\h{1,2})(?<b>\h{1,2})$/ =~ color_name
243
+ "\e[#{num};2;#{r.to_i(16)};#{g.to_i(16)};#{b.to_i(16)}m#{self}\e[0m"
244
+ end
245
+ end
246
+
247
+ # set keyword color.
248
+ def set_keyword_color(name, suffix = '')
249
+ color_name = name.to_s.to_sym
250
+ send "#{color_name}#{suffix}" if COLOR.key? color_name
251
+ end
252
+ end