tty 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -7
- data/CHANGELOG.md +6 -0
- data/Gemfile +18 -1
- data/README.md +55 -28
- data/Rakefile +5 -9
- data/images/tty.png +0 -0
- data/lib/tty.rb +7 -1
- data/lib/tty/coercer/boolean.rb +6 -9
- data/lib/tty/coercer/float.rb +14 -17
- data/lib/tty/coercer/integer.rb +14 -17
- data/lib/tty/coercer/range.rb +6 -7
- data/lib/tty/logger.rb +23 -9
- data/lib/tty/plugins.rb +3 -6
- data/lib/tty/plugins/plugin.rb +4 -7
- data/lib/tty/shell.rb +20 -12
- data/lib/tty/shell/question.rb +41 -48
- data/lib/tty/shell/question/modifier.rb +5 -8
- data/lib/tty/shell/question/validation.rb +8 -7
- data/lib/tty/shell/reader.rb +28 -19
- data/lib/tty/shell/response.rb +26 -22
- data/lib/tty/shell/statement.rb +13 -9
- data/lib/tty/shell/suggestion.rb +17 -13
- data/lib/tty/support/coercion.rb +5 -6
- data/lib/tty/support/conversion.rb +7 -6
- data/lib/tty/support/delegatable.rb +5 -5
- data/lib/tty/support/unicode.rb +3 -4
- data/lib/tty/support/utils.rb +1 -2
- data/lib/tty/system.rb +13 -6
- data/lib/tty/system/editor.rb +21 -18
- data/lib/tty/system/which.rb +3 -5
- data/lib/tty/table.rb +18 -22
- data/lib/tty/table/border.rb +2 -4
- data/lib/tty/table/border/ascii.rb +3 -2
- data/lib/tty/table/border/null.rb +1 -3
- data/lib/tty/table/border/row_line.rb +1 -4
- data/lib/tty/table/border/unicode.rb +3 -2
- data/lib/tty/table/border_dsl.rb +21 -17
- data/lib/tty/table/border_options.rb +6 -5
- data/lib/tty/table/column_set.rb +11 -7
- data/lib/tty/table/columns.rb +19 -16
- data/lib/tty/table/error.rb +1 -3
- data/lib/tty/table/field.rb +30 -12
- data/lib/tty/table/header.rb +18 -9
- data/lib/tty/table/indentation.rb +1 -3
- data/lib/tty/table/operation/alignment.rb +8 -9
- data/lib/tty/table/operation/alignment_set.rb +6 -5
- data/lib/tty/table/operation/escape.rb +3 -3
- data/lib/tty/table/operation/filter.rb +3 -4
- data/lib/tty/table/operation/truncation.rb +3 -3
- data/lib/tty/table/operation/wrapped.rb +3 -4
- data/lib/tty/table/operations.rb +19 -22
- data/lib/tty/table/orientation.rb +5 -5
- data/lib/tty/table/orientation/horizontal.rb +9 -6
- data/lib/tty/table/orientation/vertical.rb +10 -7
- data/lib/tty/table/padder.rb +46 -8
- data/lib/tty/table/renderer.rb +21 -14
- data/lib/tty/table/renderer/ascii.rb +6 -5
- data/lib/tty/table/renderer/basic.rb +18 -19
- data/lib/tty/table/renderer/unicode.rb +11 -5
- data/lib/tty/table/row.rb +36 -20
- data/lib/tty/table/transformation.rb +6 -5
- data/lib/tty/table/validatable.rb +32 -17
- data/lib/tty/terminal.rb +26 -42
- data/lib/tty/terminal/color.rb +10 -12
- data/lib/tty/terminal/echo.rb +4 -7
- data/lib/tty/terminal/home.rb +3 -10
- data/lib/tty/terminal/pager.rb +11 -10
- data/lib/tty/terminal/pager/basic.rb +3 -6
- data/lib/tty/terminal/pager/system.rb +2 -5
- data/lib/tty/text.rb +7 -10
- data/lib/tty/text/distance.rb +2 -4
- data/lib/tty/text/truncation.rb +1 -3
- data/lib/tty/text/wrapping.rb +1 -3
- data/lib/tty/vector.rb +3 -5
- data/lib/tty/version.rb +2 -2
- data/spec/spec_helper.rb +4 -11
- data/spec/tty/coercer/boolean/coerce_spec.rb +19 -16
- data/spec/tty/coercer/float/coerce_spec.rb +6 -4
- data/spec/tty/coercer/integer/coerce_spec.rb +5 -4
- data/spec/tty/coercer/range/coerce_spec.rb +12 -11
- data/spec/tty/logger/log_spec.rb +23 -0
- data/spec/tty/logger/new_spec.rb +14 -14
- data/spec/tty/logger/valid_level_spec.rb +6 -6
- data/spec/tty/plugins/find_spec.rb +4 -4
- data/spec/tty/plugins/load_spec.rb +4 -5
- data/spec/tty/plugins/plugin/load_spec.rb +10 -8
- data/spec/tty/plugins/plugin/new_spec.rb +7 -7
- data/spec/tty/shell/ask_spec.rb +6 -7
- data/spec/tty/shell/error_spec.rb +1 -2
- data/spec/tty/shell/print_table_spec.rb +1 -2
- data/spec/tty/shell/question/argument_spec.rb +1 -1
- data/spec/tty/shell/question/character_spec.rb +11 -3
- data/spec/tty/shell/question/default_spec.rb +3 -3
- data/spec/tty/shell/question/in_spec.rb +1 -1
- data/spec/tty/shell/question/initialize_spec.rb +8 -11
- data/spec/tty/shell/question/modifier/apply_to_spec.rb +8 -4
- data/spec/tty/shell/question/modifier/letter_case_spec.rb +4 -4
- data/spec/tty/shell/question/modifier/whitespace_spec.rb +5 -5
- data/spec/tty/shell/question/modify_spec.rb +1 -1
- data/spec/tty/shell/question/valid_spec.rb +4 -4
- data/spec/tty/shell/question/validate_spec.rb +4 -4
- data/spec/tty/shell/question/validation/coerce_spec.rb +2 -3
- data/spec/tty/shell/question/validation/valid_value_spec.rb +3 -3
- data/spec/tty/shell/reader/getc_spec.rb +16 -14
- data/spec/tty/shell/response/read_bool_spec.rb +1 -1
- data/spec/tty/shell/response/read_char_spec.rb +1 -2
- data/spec/tty/shell/response/read_date_spec.rb +1 -1
- data/spec/tty/shell/response/read_email_spec.rb +1 -1
- data/spec/tty/shell/response/read_multiple_spec.rb +3 -4
- data/spec/tty/shell/response/read_number_spec.rb +1 -2
- data/spec/tty/shell/response/read_range_spec.rb +2 -2
- data/spec/tty/shell/response/read_spec.rb +7 -7
- data/spec/tty/shell/response/read_string_spec.rb +2 -2
- data/spec/tty/shell/say_spec.rb +1 -1
- data/spec/tty/shell/statement/initialize_spec.rb +3 -3
- data/spec/tty/shell/suggest_spec.rb +1 -1
- data/spec/tty/shell/warn_spec.rb +1 -2
- data/spec/tty/support/coercion_spec.rb +10 -10
- data/spec/tty/support/conversion_spec.rb +9 -9
- data/spec/tty/support/delegatable_spec.rb +7 -5
- data/spec/tty/support/equatable_spec.rb +32 -34
- data/spec/tty/support/fixtures/classes.rb +4 -2
- data/spec/tty/system/editor/available_spec.rb +12 -12
- data/spec/tty/system/editor/build_spec.rb +8 -16
- data/spec/tty/system/editor/command_spec.rb +3 -2
- data/spec/tty/system/editor/executables_spec.rb +3 -3
- data/spec/tty/system/editor/invoke_spec.rb +10 -14
- data/spec/tty/system/editor/open_spec.rb +9 -7
- data/spec/tty/system/platform_spec.rb +32 -13
- data/spec/tty/system/which/which_spec.rb +9 -9
- data/spec/tty/system/which_spec.rb +4 -4
- data/spec/tty/table/access_spec.rb +24 -21
- data/spec/tty/table/add_row_spec.rb +2 -2
- data/spec/tty/table/border/ascii/rendering_spec.rb +13 -13
- data/spec/tty/table/border/new_spec.rb +5 -4
- data/spec/tty/table/border/null/rendering_spec.rb +12 -12
- data/spec/tty/table/border/options/from_spec.rb +2 -3
- data/spec/tty/table/border/options/new_spec.rb +4 -5
- data/spec/tty/table/border/unicode/rendering_spec.rb +10 -10
- data/spec/tty/table/border_options/new_spec.rb +20 -0
- data/spec/tty/table/border_options/update_spec.rb +18 -0
- data/spec/tty/table/column_set/extract_widths_spec.rb +1 -1
- data/spec/tty/table/column_set/total_width_spec.rb +2 -2
- data/spec/tty/table/column_set/widths_from_spec.rb +1 -2
- data/spec/tty/table/columns/enforce_spec.rb +2 -2
- data/spec/tty/table/columns/widths_spec.rb +2 -0
- data/spec/tty/table/data_spec.rb +3 -3
- data/spec/tty/table/each_spec.rb +9 -11
- data/spec/tty/table/each_with_index_spec.rb +8 -9
- data/spec/tty/table/empty_spec.rb +3 -3
- data/spec/tty/table/eql_spec.rb +6 -6
- data/spec/tty/table/field/equality_spec.rb +11 -11
- data/spec/tty/table/field/length_spec.rb +4 -4
- data/spec/tty/table/field/lines_spec.rb +3 -3
- data/spec/tty/table/field/new_spec.rb +7 -7
- data/spec/tty/table/field/width_spec.rb +6 -4
- data/spec/tty/table/filter_spec.rb +3 -3
- data/spec/tty/table/header/call_spec.rb +3 -3
- data/spec/tty/table/header/height_spec.rb +1 -1
- data/spec/tty/table/header/new_spec.rb +5 -5
- data/spec/tty/table/header/set_spec.rb +9 -4
- data/spec/tty/table/header/to_ary_spec.rb +4 -4
- data/spec/tty/table/header_spec.rb +3 -4
- data/spec/tty/table/indentation/insert_indent_spec.rb +1 -1
- data/spec/tty/table/initialize_spec.rb +13 -13
- data/spec/tty/table/operation/alignment/format_spec.rb +5 -5
- data/spec/tty/table/operation/alignment/new_spec.rb +4 -4
- data/spec/tty/table/operation/alignment_set/call_spec.rb +11 -11
- data/spec/tty/table/operation/alignment_set/each_spec.rb +3 -3
- data/spec/tty/table/operation/alignment_set/new_spec.rb +6 -6
- data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +4 -4
- data/spec/tty/table/operation/escape/call_spec.rb +1 -1
- data/spec/tty/table/operation/filter/call_spec.rb +1 -1
- data/spec/tty/table/operation/truncation/call_spec.rb +1 -1
- data/spec/tty/table/operation/truncation/truncate_spec.rb +4 -4
- data/spec/tty/table/operation/wrapped/call_spec.rb +1 -1
- data/spec/tty/table/operation/wrapped/wrap_spec.rb +3 -3
- data/spec/tty/table/operations/new_spec.rb +1 -1
- data/spec/tty/table/options_spec.rb +8 -8
- data/spec/tty/table/orientation_spec.rb +15 -13
- data/spec/tty/table/padder/parse_spec.rb +1 -1
- data/spec/tty/table/padder/to_s_spec.rb +14 -0
- data/spec/tty/table/padding_spec.rb +1 -1
- data/spec/tty/table/properties_spec.rb +9 -8
- data/spec/tty/table/render_spec.rb +8 -5
- data/spec/tty/table/render_with_spec.rb +5 -4
- data/spec/tty/table/renderer/ascii/indentation_spec.rb +1 -1
- data/spec/tty/table/renderer/ascii/padding_spec.rb +1 -1
- data/spec/tty/table/renderer/ascii/render_spec.rb +7 -7
- data/spec/tty/table/renderer/ascii/resizing_spec.rb +1 -1
- data/spec/tty/table/renderer/ascii/separator_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/alignment_spec.rb +5 -5
- data/spec/tty/table/renderer/basic/coloring_spec.rb +1 -1
- data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +6 -6
- data/spec/tty/table/renderer/basic/filter_spec.rb +4 -4
- data/spec/tty/table/renderer/basic/indentation_spec.rb +5 -3
- data/spec/tty/table/renderer/basic/multiline_content_spec.rb +10 -10
- data/spec/tty/table/renderer/basic/new_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/options_spec.rb +9 -9
- data/spec/tty/table/renderer/basic/padding_spec.rb +1 -1
- data/spec/tty/table/renderer/basic/render_spec.rb +8 -8
- data/spec/tty/table/renderer/basic/resizing_spec.rb +1 -1
- data/spec/tty/table/renderer/basic/separator_spec.rb +2 -2
- data/spec/tty/table/renderer/basic/truncation_spec.rb +4 -4
- data/spec/tty/table/renderer/basic/wrapping_spec.rb +4 -4
- data/spec/tty/table/renderer/border_spec.rb +21 -21
- data/spec/tty/table/renderer/render_spec.rb +7 -7
- data/spec/tty/table/renderer/select_spec.rb +4 -4
- data/spec/tty/table/renderer/style_spec.rb +19 -19
- data/spec/tty/table/renderer/unicode/indentation_spec.rb +1 -1
- data/spec/tty/table/renderer/unicode/padding_spec.rb +1 -1
- data/spec/tty/table/renderer/unicode/render_spec.rb +6 -6
- data/spec/tty/table/renderer/unicode/separator_spec.rb +2 -2
- data/spec/tty/table/renderer_spec.rb +1 -1
- data/spec/tty/table/rotate_spec.rb +27 -27
- data/spec/tty/table/row/access_spec.rb +3 -3
- data/spec/tty/table/row/call_spec.rb +4 -4
- data/spec/tty/table/row/data_spec.rb +5 -5
- data/spec/tty/table/row/each_spec.rb +1 -1
- data/spec/tty/table/row/equality_spec.rb +16 -16
- data/spec/tty/table/row/height_spec.rb +1 -1
- data/spec/tty/table/row/new_spec.rb +11 -11
- data/spec/tty/table/row/to_ary_spec.rb +3 -3
- data/spec/tty/table/to_s_spec.rb +8 -8
- data/spec/tty/table/validatable/validate_options_spec.rb +5 -5
- data/spec/tty/table/validatable_spec.rb +14 -1
- data/spec/tty/terminal/color/code_spec.rb +3 -3
- data/spec/tty/terminal/color/remove_spec.rb +8 -9
- data/spec/tty/terminal/color/set_spec.rb +5 -6
- data/spec/tty/terminal/color_spec.rb +3 -5
- data/spec/tty/terminal/echo_spec.rb +3 -4
- data/spec/tty/terminal/home_spec.rb +12 -14
- data/spec/tty/terminal/page_spec.rb +5 -4
- data/spec/tty/terminal/pager/available_spec.rb +11 -11
- data/spec/tty/terminal/pager/basic/page_spec.rb +8 -8
- data/spec/tty/terminal/pager/command_spec.rb +2 -2
- data/spec/tty/terminal/pager/executables_spec.rb +3 -3
- data/spec/tty/terminal/pager/page_spec.rb +8 -10
- data/spec/tty/terminal/pager/system/page_spec.rb +8 -8
- data/spec/tty/terminal/size_spec.rb +39 -36
- data/spec/tty/text/distance/distance_spec.rb +13 -13
- data/spec/tty/text/distance/initialize_spec.rb +4 -4
- data/spec/tty/text/distance_spec.rb +2 -2
- data/spec/tty/text/truncate_spec.rb +2 -3
- data/spec/tty/text/truncation/initialize_spec.rb +9 -9
- data/spec/tty/text/truncation/truncate_spec.rb +14 -14
- data/spec/tty/text/wrap_spec.rb +2 -3
- data/spec/tty/text/wrapping/initialize_spec.rb +7 -7
- data/spec/tty/text/wrapping/wrap_spec.rb +11 -11
- data/spec/tty/tty_spec.rb +22 -0
- data/spec/tty/vector/new_spec.rb +7 -7
- data/tasks/console.rake +10 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/metrics/cane.rake +2 -0
- data/tasks/metrics/flog.rake +2 -0
- data/tasks/metrics/heckle.rake +15 -0
- data/tasks/metrics/reek.rake +2 -0
- data/tasks/spec.rake +29 -0
- data/tty.gemspec +5 -12
- metadata +34 -91
- data/.rvmrc +0 -34
data/lib/tty/terminal.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# Return default width of terminal
|
7
6
|
#
|
8
7
|
# @example
|
@@ -23,45 +22,30 @@ module TTY
|
|
23
22
|
# @api public
|
24
23
|
attr_reader :default_height
|
25
24
|
|
25
|
+
# Return access to color terminal
|
26
|
+
#
|
27
|
+
# @return [TTY::Terminal::Color]
|
28
|
+
#
|
26
29
|
# @api public
|
27
30
|
attr_reader :color
|
28
31
|
|
29
32
|
# Output pager
|
30
33
|
#
|
31
|
-
# @return [Pager]
|
34
|
+
# @return [TTY::Terminal::Pager]
|
32
35
|
#
|
33
36
|
# @api public
|
34
37
|
attr_reader :pager
|
35
38
|
|
36
|
-
|
39
|
+
# Initialize a Terminal
|
40
|
+
#
|
41
|
+
# @api public
|
42
|
+
def initialize(options = {})
|
37
43
|
@color = TTY::Terminal::Color.new(self.color?)
|
38
44
|
@echo = TTY::Terminal::Echo.new
|
39
45
|
@pager = TTY::Terminal::Pager
|
40
|
-
@
|
41
|
-
@
|
42
|
-
|
43
|
-
|
44
|
-
# Set default width of terminal
|
45
|
-
#
|
46
|
-
# @param [Integer] width
|
47
|
-
#
|
48
|
-
# @return [Integer]
|
49
|
-
#
|
50
|
-
# @api public
|
51
|
-
def default_width=(width)
|
52
|
-
@default_width = width
|
53
|
-
end
|
54
|
-
|
55
|
-
# Set default height of terminal
|
56
|
-
#
|
57
|
-
# @example
|
58
|
-
# default_height = TTY::Terminal.default_height
|
59
|
-
#
|
60
|
-
# @return [Integer]
|
61
|
-
#
|
62
|
-
# @api public
|
63
|
-
def default_height=(height)
|
64
|
-
@default_height = height
|
46
|
+
@home = Home.new
|
47
|
+
@default_width = options.fetch(:default_width) { 80 }
|
48
|
+
@default_height = options.fetch(:default_height) { 24 }
|
65
49
|
end
|
66
50
|
|
67
51
|
# Determine current width
|
@@ -72,9 +56,9 @@ module TTY
|
|
72
56
|
def width
|
73
57
|
env_tty_columns = ENV['TTY_COLUMNS']
|
74
58
|
if env_tty_columns =~ /^\d+$/
|
75
|
-
|
59
|
+
env_tty_columns.to_i
|
76
60
|
else
|
77
|
-
|
61
|
+
TTY::System.unix? ? dynamic_width : default_width
|
78
62
|
end
|
79
63
|
rescue
|
80
64
|
default_width
|
@@ -82,16 +66,18 @@ module TTY
|
|
82
66
|
|
83
67
|
# Determine current height
|
84
68
|
#
|
69
|
+
# @return [Integer] height
|
70
|
+
#
|
85
71
|
# @api public
|
86
72
|
def height
|
87
73
|
env_tty_lines = ENV['TTY_LINES']
|
88
74
|
if env_tty_lines =~ /^\d+$/
|
89
|
-
|
75
|
+
env_tty_lines.to_i
|
90
76
|
else
|
91
|
-
|
77
|
+
TTY::System.unix? ? dynamic_height : default_height
|
92
78
|
end
|
93
79
|
rescue
|
94
|
-
|
80
|
+
default_height
|
95
81
|
end
|
96
82
|
|
97
83
|
# Calculate dynamic width of the terminal
|
@@ -118,7 +104,7 @@ module TTY
|
|
118
104
|
#
|
119
105
|
# @api public
|
120
106
|
def dynamic_width_stty
|
121
|
-
%x
|
107
|
+
%x(tty size 2>/dev/null).split[1].to_i
|
122
108
|
end
|
123
109
|
|
124
110
|
# Detect terminal height with stty utility
|
@@ -127,7 +113,7 @@ module TTY
|
|
127
113
|
#
|
128
114
|
# @api public
|
129
115
|
def dynamic_height_stty
|
130
|
-
%x
|
116
|
+
%x(tty size 2>/dev/null).split[0].to_i
|
131
117
|
end
|
132
118
|
|
133
119
|
# Detect terminal width with tput utility
|
@@ -136,7 +122,7 @@ module TTY
|
|
136
122
|
#
|
137
123
|
# @api public
|
138
124
|
def dynamic_width_tput
|
139
|
-
%x
|
125
|
+
%x(tput cols 2>/dev/null).to_i
|
140
126
|
end
|
141
127
|
|
142
128
|
# Detect terminal height with tput utility
|
@@ -145,7 +131,7 @@ module TTY
|
|
145
131
|
#
|
146
132
|
# @api public
|
147
133
|
def dynamic_height_tput
|
148
|
-
%x
|
134
|
+
%x(tput lines 2>/dev/null).to_i
|
149
135
|
end
|
150
136
|
|
151
137
|
# Check if terminal supports color
|
@@ -154,7 +140,7 @@ module TTY
|
|
154
140
|
#
|
155
141
|
# @api public
|
156
142
|
def color?
|
157
|
-
%x
|
143
|
+
%x(tput colors 2>/dev/null).to_i > 2
|
158
144
|
end
|
159
145
|
|
160
146
|
# Switch echo on
|
@@ -176,7 +162,7 @@ module TTY
|
|
176
162
|
# @param [Boolean] is_on
|
177
163
|
#
|
178
164
|
# @api public
|
179
|
-
def echo(is_on=true, &block)
|
165
|
+
def echo(is_on = true, &block)
|
180
166
|
@echo.echo(is_on, &block)
|
181
167
|
end
|
182
168
|
|
@@ -186,7 +172,6 @@ module TTY
|
|
186
172
|
#
|
187
173
|
# @api public
|
188
174
|
def home
|
189
|
-
@home ||= Home.new
|
190
175
|
@home.home
|
191
176
|
end
|
192
177
|
|
@@ -199,6 +184,5 @@ module TTY
|
|
199
184
|
def page(text)
|
200
185
|
@pager.page(text)
|
201
186
|
end
|
202
|
-
|
203
187
|
end # Terminal
|
204
188
|
end # TTY
|
data/lib/tty/terminal/color.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# A class responsible for coloring strings.
|
7
6
|
class Color
|
8
|
-
|
9
7
|
# Embed in a String to clear all previous ANSI sequences.
|
10
8
|
CLEAR = "\e[0m"
|
11
9
|
# The start of an ANSI bold sequence.
|
@@ -13,7 +11,7 @@ module TTY
|
|
13
11
|
# The start of an ANSI underlined sequence.
|
14
12
|
UNDERLINE = "\e[4m"
|
15
13
|
|
16
|
-
STYLES = %w
|
14
|
+
STYLES = %w( BOLD CLEAR UNDERLINE ).freeze
|
17
15
|
|
18
16
|
# Escape codes for text color.
|
19
17
|
BLACK = "\e[30m"
|
@@ -60,7 +58,7 @@ module TTY
|
|
60
58
|
# Initialize a Terminal Color
|
61
59
|
#
|
62
60
|
# @api public
|
63
|
-
def initialize(enabled=false)
|
61
|
+
def initialize(enabled = false)
|
64
62
|
@enabled = enabled
|
65
63
|
end
|
66
64
|
|
@@ -73,6 +71,8 @@ module TTY
|
|
73
71
|
|
74
72
|
# Check if coloring is on
|
75
73
|
#
|
74
|
+
# @return [Boolean]
|
75
|
+
#
|
76
76
|
# @api public
|
77
77
|
def enabled?
|
78
78
|
@enabled
|
@@ -134,7 +134,7 @@ module TTY
|
|
134
134
|
#
|
135
135
|
# @api public
|
136
136
|
def names
|
137
|
-
(STYLES + BACKGROUND_COLORS + TEXT_COLORS).map { |
|
137
|
+
(STYLES + BACKGROUND_COLORS + TEXT_COLORS).map { |col| col.to_s.downcase }
|
138
138
|
end
|
139
139
|
|
140
140
|
protected
|
@@ -148,12 +148,10 @@ module TTY
|
|
148
148
|
|
149
149
|
# @api private
|
150
150
|
def validate(*colors)
|
151
|
-
|
152
|
-
|
153
|
-
|
151
|
+
return if colors.all? { |color| names.include?(color.to_s) }
|
152
|
+
raise ArgumentError, 'Bad color or unintialized constant, ' \
|
153
|
+
" valid colors are: #{names.join(', ')}."
|
154
154
|
end
|
155
|
-
|
156
155
|
end # Color
|
157
|
-
|
158
|
-
end
|
156
|
+
end # Terminal
|
159
157
|
end # TTY
|
data/lib/tty/terminal/echo.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# A class responsible for toggling echo.
|
7
6
|
class Echo
|
8
|
-
|
9
7
|
# Turn echo on
|
10
8
|
#
|
11
9
|
# @api public
|
@@ -26,16 +24,15 @@ module TTY
|
|
26
24
|
def echo(is_on=true, &block)
|
27
25
|
value = nil
|
28
26
|
begin
|
29
|
-
|
27
|
+
off unless is_on
|
30
28
|
value = block.call if block_given?
|
31
|
-
|
29
|
+
on
|
32
30
|
return value
|
33
31
|
rescue NoMethodError, Interrupt
|
34
|
-
|
32
|
+
on
|
35
33
|
exit
|
36
34
|
end
|
37
35
|
end
|
38
|
-
|
39
36
|
end # Echo
|
40
37
|
end # Terminal
|
41
38
|
end # TTY
|
data/lib/tty/terminal/home.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# A class responsible for locating user home
|
7
6
|
class Home
|
8
|
-
|
9
7
|
# Find user home
|
10
8
|
#
|
11
9
|
# @api public
|
@@ -17,15 +15,10 @@ module TTY
|
|
17
15
|
require 'etc'
|
18
16
|
File.expand_path("~#{Etc.getlogin}")
|
19
17
|
rescue
|
20
|
-
|
21
|
-
"C:/"
|
22
|
-
else
|
23
|
-
"/"
|
24
|
-
end
|
18
|
+
TTY::System.windows? ? 'C:/' : '/'
|
25
19
|
end
|
26
20
|
end
|
27
21
|
end
|
28
|
-
|
29
|
-
end #Home
|
22
|
+
end # Home
|
30
23
|
end # Terminal
|
31
24
|
end # TTY
|
data/lib/tty/terminal/pager.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# A class responsible for paging text inside terminal
|
7
6
|
class Pager
|
8
7
|
|
@@ -10,14 +9,16 @@ module TTY
|
|
10
9
|
|
11
10
|
attr_reader :text
|
12
11
|
|
12
|
+
@command = nil
|
13
|
+
|
13
14
|
# Initialize a Pager
|
14
15
|
#
|
15
16
|
# @param [String] text
|
16
17
|
# the text to page
|
17
18
|
#
|
18
19
|
# @api public
|
19
|
-
def initialize(text=nil)
|
20
|
-
@text
|
20
|
+
def initialize(text = nil)
|
21
|
+
@text = text
|
21
22
|
@enabled = true
|
22
23
|
end
|
23
24
|
|
@@ -36,8 +37,8 @@ module TTY
|
|
36
37
|
#
|
37
38
|
# @api private
|
38
39
|
def self.executables
|
39
|
-
[
|
40
|
-
|
40
|
+
[ENV['GIT_PAGER'], `git config --get-all core.pager`.split.first,
|
41
|
+
ENV['PAGER'], 'less -isr', 'more', 'cat', 'pager']
|
41
42
|
end
|
42
43
|
|
43
44
|
# Find first available system command for paging
|
@@ -48,7 +49,7 @@ module TTY
|
|
48
49
|
#
|
49
50
|
# @api public
|
50
51
|
def self.available(*commands)
|
51
|
-
commands = commands.empty? ?
|
52
|
+
commands = commands.empty? ? executables : commands
|
52
53
|
commands.compact.uniq.find { |cmd| System.exists?(cmd) }
|
53
54
|
end
|
54
55
|
|
@@ -59,7 +60,8 @@ module TTY
|
|
59
60
|
!!available
|
60
61
|
end
|
61
62
|
|
62
|
-
# Finds command to execute pager from shell commands
|
63
|
+
# Finds command to execute pager from shell commands
|
64
|
+
# unless configured is provided.
|
63
65
|
#
|
64
66
|
# @param [Array[String]] commands
|
65
67
|
#
|
@@ -67,7 +69,7 @@ module TTY
|
|
67
69
|
#
|
68
70
|
# @api private
|
69
71
|
def self.command(*commands)
|
70
|
-
@command = if
|
72
|
+
@command = if @command && commands.empty?
|
71
73
|
@command
|
72
74
|
else
|
73
75
|
available(*commands)
|
@@ -89,7 +91,6 @@ module TTY
|
|
89
91
|
BasicPager.new(text).page
|
90
92
|
end
|
91
93
|
end
|
92
|
-
|
93
94
|
end # Pager
|
94
95
|
end # Terminal
|
95
96
|
end # TTY
|
@@ -1,14 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# A class responsible for paging text
|
7
6
|
class BasicPager < Pager
|
8
|
-
|
9
7
|
PROMPT_HEIGHT = 3
|
10
8
|
|
11
|
-
PAGE_BREAK =
|
9
|
+
PAGE_BREAK = '--- Press enter/return to continue (or q to quit) ---'
|
12
10
|
|
13
11
|
# Use ruby to page output text
|
14
12
|
#
|
@@ -37,7 +35,7 @@ module TTY
|
|
37
35
|
question = TTY.shell.ask "\n#{PAGE_BREAK}"
|
38
36
|
return false if question.read_string[/q/i]
|
39
37
|
end
|
40
|
-
|
38
|
+
true
|
41
39
|
end
|
42
40
|
|
43
41
|
# Determine current page size
|
@@ -46,7 +44,6 @@ module TTY
|
|
46
44
|
def page_size
|
47
45
|
@page_size ||= TTY.terminal.height - PROMPT_HEIGHT
|
48
46
|
end
|
49
|
-
|
50
47
|
end # BasicPager
|
51
48
|
end # Terminal
|
52
49
|
end # TTY
|
@@ -1,11 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
|
6
5
|
# A class responsible for paging text
|
7
6
|
class SystemPager < Pager
|
8
|
-
|
9
7
|
# Use system command to page output text
|
10
8
|
#
|
11
9
|
# @api public
|
@@ -24,7 +22,7 @@ module TTY
|
|
24
22
|
begin
|
25
23
|
Kernel.exec(Pager.command)
|
26
24
|
rescue
|
27
|
-
Kernel.exec
|
25
|
+
Kernel.exec '/bin/sh', '-c', command
|
28
26
|
end
|
29
27
|
else
|
30
28
|
# child process
|
@@ -33,7 +31,6 @@ module TTY
|
|
33
31
|
read_io.close
|
34
32
|
end
|
35
33
|
end
|
36
|
-
|
37
34
|
end # SystemPager
|
38
35
|
end # Terminal
|
39
36
|
end # TTY
|
data/lib/tty/text.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
|
5
4
|
# A class responsible for text manipulations
|
6
5
|
class Text
|
6
|
+
SPACE = ' '
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
NEWLINE = "\n".freeze
|
8
|
+
NEWLINE = "\n"
|
11
9
|
|
12
10
|
DEFAULT_WIDTH = 80.freeze
|
13
11
|
|
@@ -24,9 +22,9 @@ module TTY
|
|
24
22
|
# the second string for comparison
|
25
23
|
#
|
26
24
|
# @example
|
27
|
-
# distance("which", "witch")
|
28
|
-
# # => 2
|
25
|
+
# distance("which", "witch") # => 2
|
29
26
|
#
|
27
|
+
# @api public
|
30
28
|
def self.distance(first, second, *args)
|
31
29
|
Distance.new(first, second, *args).distance
|
32
30
|
end
|
@@ -50,10 +48,10 @@ module TTY
|
|
50
48
|
# wrap("Some longish text", 8)
|
51
49
|
# # => "Some\nlongish\ntext"
|
52
50
|
#
|
53
|
-
# wrap("Some longish text", :
|
51
|
+
# wrap("Some longish text", length: 8)
|
54
52
|
# # => "Some\nlongish\ntext"
|
55
53
|
#
|
56
|
-
# wrap("Some longish text", 8, :
|
54
|
+
# wrap("Some longish text", 8, indent: 4)
|
57
55
|
# # => > Some
|
58
56
|
# > longish
|
59
57
|
# > text
|
@@ -96,6 +94,5 @@ module TTY
|
|
96
94
|
def self.truncate(text, *args)
|
97
95
|
Truncation.new(text, *args).truncate
|
98
96
|
end
|
99
|
-
|
100
97
|
end # Text
|
101
98
|
end # TTY
|