tty 0.0.11 → 0.1.0
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.
- 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/support/coercion.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
# A mixin to coerce a value into a specific class.
|
5
5
|
module Coercion
|
6
|
-
|
7
6
|
# Helper to coerce value into a specific class.
|
8
7
|
#
|
9
8
|
# @param [Object] object
|
@@ -14,18 +13,18 @@ module TTY
|
|
14
13
|
#
|
15
14
|
# @api public
|
16
15
|
def coerce_to(object, cls, method)
|
17
|
-
return object if object.
|
16
|
+
return object if object.is_a?(cls)
|
18
17
|
|
19
18
|
begin
|
20
19
|
result = object.__send__(method)
|
21
20
|
rescue Exception => e
|
22
21
|
raise TypeError, "Coercion error #{e.message}"
|
23
22
|
end
|
24
|
-
unless result.
|
25
|
-
|
23
|
+
unless result.is_a?(cls)
|
24
|
+
fail TypeError, "Coercion error: obj.#{method} did not return " \
|
25
|
+
"a #{cls} (was #{result.class})"
|
26
26
|
end
|
27
27
|
result
|
28
28
|
end
|
29
|
-
|
30
29
|
end # Coercion
|
31
30
|
end # TTY
|
@@ -1,9 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
|
-
# A mixin to allow instances conversion to
|
4
|
+
# A mixin to allow instances conversion to array type
|
5
|
+
#
|
6
|
+
# @api public
|
5
7
|
module Conversion
|
6
|
-
|
7
8
|
# Converts the object into an Array. If copy is set to true
|
8
9
|
# a copy of object will be made.
|
9
10
|
#
|
@@ -14,7 +15,7 @@ module TTY
|
|
14
15
|
# @return [Array]
|
15
16
|
#
|
16
17
|
# @api private
|
17
|
-
def convert_to_array(object, copy=false)
|
18
|
+
def convert_to_array(object, copy = false)
|
18
19
|
case object
|
19
20
|
when Array
|
20
21
|
copy ? object.dup : object
|
@@ -24,11 +25,11 @@ module TTY
|
|
24
25
|
begin
|
25
26
|
converted = object.to_ary
|
26
27
|
rescue Exception => e
|
27
|
-
raise TTY::TypeError,
|
28
|
+
raise TTY::TypeError,
|
29
|
+
"Cannot convert #{object.class} into an Array (#{e.message})"
|
28
30
|
end
|
29
31
|
converted
|
30
32
|
end
|
31
33
|
end
|
32
|
-
|
33
34
|
end # Conversion
|
34
35
|
end # TTY
|
@@ -1,9 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
# A mixin to allow delegetable methods to be added
|
5
5
|
module Delegatable
|
6
|
-
|
7
6
|
# Create delegator for each specified method
|
8
7
|
#
|
9
8
|
# @example
|
@@ -17,11 +16,13 @@ module TTY
|
|
17
16
|
#
|
18
17
|
# @api public
|
19
18
|
def delegatable_method(source, *methods)
|
20
|
-
methods.each
|
19
|
+
methods.each do |method|
|
20
|
+
define_delegatable_method(source, method)
|
21
|
+
end
|
21
22
|
self
|
22
23
|
end
|
23
24
|
|
24
|
-
|
25
|
+
private
|
25
26
|
|
26
27
|
# Create a delegator method for the method name
|
27
28
|
#
|
@@ -39,6 +40,5 @@ module TTY
|
|
39
40
|
end
|
40
41
|
RUBY
|
41
42
|
end
|
42
|
-
|
43
43
|
end # Delegatable
|
44
44
|
end # TTY
|
data/lib/tty/support/unicode.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
# A mixin to provide unicode support.
|
@@ -19,18 +19,17 @@ module TTY
|
|
19
19
|
string
|
20
20
|
end
|
21
21
|
|
22
|
-
if
|
22
|
+
if ''.respond_to?(:encode)
|
23
23
|
def as_unicode
|
24
24
|
yield
|
25
25
|
end
|
26
26
|
else
|
27
27
|
def as_unicode
|
28
|
-
old, $KCODE = $KCODE,
|
28
|
+
old, $KCODE = $KCODE, 'U'
|
29
29
|
yield
|
30
30
|
ensure
|
31
31
|
$KCODE = old
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
34
|
end # Unicode
|
36
35
|
end # TTY
|
data/lib/tty/support/utils.rb
CHANGED
data/lib/tty/system.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'rbconfig'
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class System
|
7
|
-
|
8
7
|
# Check if windows platform.
|
9
8
|
#
|
10
9
|
# @return [Boolean]
|
11
10
|
#
|
12
11
|
# @api public
|
13
12
|
def self.windows?
|
14
|
-
|
13
|
+
match_os?(/msdos|mswin|djgpp|mingw|windows/i)
|
15
14
|
end
|
16
15
|
|
17
16
|
# Check if unix platform
|
@@ -20,7 +19,16 @@ module TTY
|
|
20
19
|
#
|
21
20
|
# @api public
|
22
21
|
def self.unix?
|
23
|
-
|
22
|
+
match_os?(/(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Check if platform matches given systems
|
26
|
+
#
|
27
|
+
# @return [Boolean]
|
28
|
+
#
|
29
|
+
# @api public
|
30
|
+
def self.match_os?(matcher)
|
31
|
+
!!(RbConfig::CONFIG['host_os'] =~ matcher)
|
24
32
|
end
|
25
33
|
|
26
34
|
# Find an executable in the PATH
|
@@ -39,7 +47,7 @@ module TTY
|
|
39
47
|
#
|
40
48
|
# @api public
|
41
49
|
def self.exists?(name)
|
42
|
-
!!
|
50
|
+
!!which(name)
|
43
51
|
end
|
44
52
|
|
45
53
|
# Proxy to editor object
|
@@ -50,6 +58,5 @@ module TTY
|
|
50
58
|
def self.editor
|
51
59
|
TTY::System::Editor
|
52
60
|
end
|
53
|
-
|
54
61
|
end # System
|
55
62
|
end # TTY
|
data/lib/tty/system/editor.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'shellwords'
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
class System
|
7
|
-
|
8
7
|
# A class responsible for launching an editor
|
8
|
+
#
|
9
|
+
# @api private
|
9
10
|
class Editor
|
10
|
-
|
11
11
|
attr_reader :file
|
12
12
|
|
13
|
+
@command = nil
|
14
|
+
|
13
15
|
# Initialize an Editor
|
14
16
|
#
|
15
17
|
# @param [String] file
|
16
18
|
#
|
17
19
|
# @api public
|
18
20
|
def initialize(file)
|
19
|
-
@file
|
21
|
+
@file = file
|
20
22
|
end
|
21
23
|
|
22
24
|
# List possible executable for editor command
|
@@ -25,7 +27,7 @@ module TTY
|
|
25
27
|
#
|
26
28
|
# @api private
|
27
29
|
def self.executables
|
28
|
-
[
|
30
|
+
[ENV['VISUAL'], ENV['EDITOR'], 'vi', 'emacs']
|
29
31
|
end
|
30
32
|
|
31
33
|
# Find available command
|
@@ -36,7 +38,7 @@ module TTY
|
|
36
38
|
#
|
37
39
|
# @api public
|
38
40
|
def self.available(*commands)
|
39
|
-
commands = commands.empty? ?
|
41
|
+
commands = commands.empty? ? executables : commands
|
40
42
|
commands.compact.uniq.find { |cmd| System.exists?(cmd) }
|
41
43
|
end
|
42
44
|
|
@@ -48,7 +50,7 @@ module TTY
|
|
48
50
|
#
|
49
51
|
# @api public
|
50
52
|
def self.command(*commands)
|
51
|
-
@command = if
|
53
|
+
@command = if @command && commands.empty?
|
52
54
|
@command
|
53
55
|
else
|
54
56
|
available(*commands)
|
@@ -66,9 +68,8 @@ module TTY
|
|
66
68
|
#
|
67
69
|
# @api public
|
68
70
|
def self.open(file)
|
69
|
-
unless
|
70
|
-
|
71
|
-
exit 1
|
71
|
+
unless command
|
72
|
+
fail CommandInvocationError, 'Please export $VISUAL or $EDITOR'
|
72
73
|
end
|
73
74
|
|
74
75
|
new(file).invoke
|
@@ -80,7 +81,14 @@ module TTY
|
|
80
81
|
#
|
81
82
|
# @api private
|
82
83
|
def build
|
83
|
-
|
84
|
+
"#{Editor.command} #{escape_file}"
|
85
|
+
end
|
86
|
+
|
87
|
+
# Escape file path
|
88
|
+
#
|
89
|
+
# @api private
|
90
|
+
def escape_file
|
91
|
+
if System.unix?
|
84
92
|
# Escape file string so it can be safely used in a Bourne shell
|
85
93
|
Shellwords.shellescape(file)
|
86
94
|
elsif System.windows?
|
@@ -88,7 +96,6 @@ module TTY
|
|
88
96
|
else
|
89
97
|
file
|
90
98
|
end
|
91
|
-
"#{Editor.command} #{escaped_file}"
|
92
99
|
end
|
93
100
|
|
94
101
|
# Inovke editor command in a shell
|
@@ -99,13 +106,9 @@ module TTY
|
|
99
106
|
def invoke
|
100
107
|
command_invocation = build
|
101
108
|
status = system(*Shellwords.split(command_invocation))
|
102
|
-
|
103
|
-
|
104
|
-
raise CommandInvocationError, "`#{command_invocation}` failed with status: #{$? ? $?.exitstatus : nil}"
|
105
|
-
exit status
|
106
|
-
end
|
109
|
+
return status if status
|
110
|
+
fail CommandInvocationError, "`#{command_invocation}` failed with status: #{$? ? $?.exitstatus : nil}"
|
107
111
|
end
|
108
|
-
|
109
112
|
end # Editor
|
110
113
|
end # System
|
111
114
|
end # TTY
|
data/lib/tty/system/which.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class System
|
5
|
-
|
6
5
|
# A class responsible for finding an executable in the PATH
|
7
6
|
class Which
|
8
|
-
|
7
|
+
# The command to find
|
9
8
|
attr_reader :command
|
10
9
|
|
11
10
|
# Initialize a Which
|
@@ -38,7 +37,7 @@ module TTY
|
|
38
37
|
return exec if File.executable? exec
|
39
38
|
end
|
40
39
|
end
|
41
|
-
|
40
|
+
nil
|
42
41
|
end
|
43
42
|
|
44
43
|
# Find default system paths
|
@@ -47,7 +46,6 @@ module TTY
|
|
47
46
|
def default_system_path
|
48
47
|
ENV['PATH'].split(File::PATH_SEPARATOR)
|
49
48
|
end
|
50
|
-
|
51
49
|
end # Which
|
52
50
|
end # System
|
53
51
|
end # TTY
|
data/lib/tty/table.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
4
|
require 'tty/table/renderer'
|
@@ -8,7 +8,6 @@ require 'tty/table/header'
|
|
8
8
|
require 'tty/table/row'
|
9
9
|
|
10
10
|
module TTY
|
11
|
-
|
12
11
|
# A core class intended for storing data in a structured, tabular form.
|
13
12
|
# Once the data is stored in a TTY::Table various operations can be performed
|
14
13
|
# before the information is dumped into a stdout.
|
@@ -60,11 +59,11 @@ module TTY
|
|
60
59
|
# Create a new Table where each argument is a row
|
61
60
|
#
|
62
61
|
# @example
|
63
|
-
# table = TTY::Table
|
62
|
+
# table = TTY::Table[['a1', 'a2'], ['b1', 'b2']]
|
64
63
|
#
|
65
64
|
# @api public
|
66
65
|
def self.[](*rows)
|
67
|
-
|
66
|
+
new(rows: rows)
|
68
67
|
end
|
69
68
|
|
70
69
|
# Instantiate a new Table
|
@@ -79,7 +78,7 @@ module TTY
|
|
79
78
|
#
|
80
79
|
# @example of parameters passed as options
|
81
80
|
# rows = [ ['a1', 'a2'], ['b1', 'b2'] ]
|
82
|
-
# table = Table.new :
|
81
|
+
# table = Table.new header: ['Header 1', 'Header 2'], rows: rows
|
83
82
|
#
|
84
83
|
# @example of parameters passed as hash
|
85
84
|
# Table.new [ {'Header1' => ['a1','a2'], 'Header2' => ['b1', 'b2'] }] }
|
@@ -110,7 +109,7 @@ module TTY
|
|
110
109
|
# @return [TTY::Table]
|
111
110
|
#
|
112
111
|
# @api private
|
113
|
-
def initialize(options={}, &block)
|
112
|
+
def initialize(options = {}, &block)
|
114
113
|
validate_options! options
|
115
114
|
@header = (value = options[:header]) ? Header.new(value) : nil
|
116
115
|
@rows = coerce(options.fetch(:rows) { Row.new([]) })
|
@@ -138,7 +137,7 @@ module TTY
|
|
138
137
|
#
|
139
138
|
# @api public
|
140
139
|
def orientation=(value)
|
141
|
-
@orientation = Orientation.coerce
|
140
|
+
@orientation = Orientation.coerce(value)
|
142
141
|
end
|
143
142
|
|
144
143
|
# Marks this table as rotated
|
@@ -175,26 +174,25 @@ module TTY
|
|
175
174
|
#
|
176
175
|
# @api private
|
177
176
|
def rotate_horizontal
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
end
|
177
|
+
return unless rotated?
|
178
|
+
head, body = orientation.slice(self)
|
179
|
+
if header && header.empty?
|
180
|
+
@header = head[0]
|
181
|
+
@rows = body.map { |row| to_row(row, @header) }
|
182
|
+
else
|
183
|
+
@rows = body.map { |row| to_row(row) }
|
186
184
|
end
|
187
185
|
end
|
188
186
|
|
189
187
|
# Lookup element of the table given a row(i) and column(j)
|
190
188
|
#
|
191
189
|
# @api public
|
192
|
-
def [](row_index, column_index=false)
|
190
|
+
def [](row_index, column_index = false)
|
193
191
|
return row(row_index) unless column_index
|
194
192
|
if row_index >= 0 && column_index >= 0
|
195
193
|
rows.fetch(row_index) { return nil }[column_index]
|
196
194
|
else
|
197
|
-
|
195
|
+
fail TTY::Table::TupleMissing.new(row_index, column_index)
|
198
196
|
end
|
199
197
|
end
|
200
198
|
alias at []
|
@@ -334,8 +332,7 @@ module TTY
|
|
334
332
|
# @api public
|
335
333
|
# TODO: renmae to columns_size
|
336
334
|
def column_size
|
337
|
-
|
338
|
-
return 0
|
335
|
+
rows.size > 0 ? rows[0].size : 0
|
339
336
|
end
|
340
337
|
|
341
338
|
# Return the number of rows
|
@@ -398,7 +395,7 @@ module TTY
|
|
398
395
|
# @param [Hash] options
|
399
396
|
# the renderer options
|
400
397
|
#
|
401
|
-
def renderer(type
|
398
|
+
def renderer(type = :basic, options = {})
|
402
399
|
@renderer ||= Renderer.select(type).new(self, options)
|
403
400
|
end
|
404
401
|
|
@@ -473,8 +470,7 @@ module TTY
|
|
473
470
|
# @api private
|
474
471
|
def yield_or_eval(&block)
|
475
472
|
return unless block
|
476
|
-
block.arity > 0 ? yield(self) :
|
473
|
+
block.arity > 0 ? yield(self) : instance_eval(&block)
|
477
474
|
end
|
478
|
-
|
479
475
|
end # Table
|
480
476
|
end # TTY
|