tty 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -3
- data/CHANGELOG.md +8 -0
- data/README.md +74 -42
- data/lib/tty.rb +7 -9
- data/lib/tty/conversion.rb +16 -0
- data/lib/tty/conversion/converter/array.rb +35 -0
- data/lib/tty/{coercer → conversion/converter}/boolean.rb +20 -7
- data/lib/tty/conversion/converter/float.rb +28 -0
- data/lib/tty/conversion/converter/integer.rb +28 -0
- data/lib/tty/{coercer → conversion/converter}/range.rb +16 -7
- data/lib/tty/logger.rb +1 -1
- data/lib/tty/shell/question.rb +2 -1
- data/lib/tty/shell/response.rb +9 -4
- data/lib/tty/shell/statement.rb +3 -2
- data/lib/tty/table.rb +3 -3
- data/lib/tty/table/border.rb +8 -5
- data/lib/tty/table/border/row_line.rb +2 -2
- data/lib/tty/table/operation/alignment_set.rb +2 -1
- data/lib/tty/table/padder.rb +1 -1
- data/lib/tty/table/renderer/basic.rb +1 -1
- data/lib/tty/terminal.rb +1 -118
- data/lib/tty/text/truncation.rb +4 -1
- data/lib/tty/text/wrapping.rb +4 -1
- data/lib/tty/vector.rb +4 -3
- data/lib/tty/version.rb +2 -2
- data/spec/tty/{support/conversion_spec.rb → conversion/converter/array/convert_spec.rb} +4 -5
- data/spec/tty/{coercer/boolean/coerce_spec.rb → conversion/converter/boolean/convert_spec.rb} +5 -4
- data/spec/tty/{coercer/float/coerce_spec.rb → conversion/converter/float/convert_spec.rb} +3 -2
- data/spec/tty/{coercer/integer/coerce_spec.rb → conversion/converter/integer/convert_spec.rb} +4 -2
- data/spec/tty/{coercer/range/coerce_spec.rb → conversion/converter/range/convert_spec.rb} +3 -2
- data/spec/tty/shell/error_spec.rb +4 -1
- data/spec/tty/shell/response/read_bool_spec.rb +1 -1
- data/spec/tty/shell/say_spec.rb +3 -0
- data/spec/tty/shell/statement/initialize_spec.rb +2 -2
- data/spec/tty/shell/warn_spec.rb +3 -0
- data/spec/tty/table/field/equality_spec.rb +1 -1
- data/spec/tty/table/render_spec.rb +3 -0
- data/spec/tty/table/render_with_spec.rb +8 -5
- data/spec/tty/table/renderer/basic/coloring_spec.rb +10 -9
- data/spec/tty/table/renderer/style_spec.rb +3 -0
- data/spec/tty/terminal/color_spec.rb +6 -3
- data/tasks/console.rake +1 -1
- data/tty.gemspec +7 -1
- metadata +88 -30
- data/lib/tty/coercer.rb +0 -13
- data/lib/tty/coercer/float.rb +0 -20
- data/lib/tty/coercer/integer.rb +0 -20
- data/lib/tty/support/conversion.rb +0 -35
- data/lib/tty/support/equatable.rb +0 -152
- data/lib/tty/terminal/color.rb +0 -157
- data/spec/tty/support/equatable_spec.rb +0 -201
- data/spec/tty/terminal/color/code_spec.rb +0 -19
- data/spec/tty/terminal/color/remove_spec.rb +0 -44
- data/spec/tty/terminal/color/set_spec.rb +0 -29
- data/spec/tty/terminal/size_spec.rb +0 -97
data/lib/tty/logger.rb
CHANGED
data/lib/tty/shell/question.rb
CHANGED
@@ -64,6 +64,7 @@ module TTY
|
|
64
64
|
@validation = Validation.new options.fetch(:validation) { nil }
|
65
65
|
@default_value = nil
|
66
66
|
@error = false
|
67
|
+
@range_converter = TTY::Conversion::RangeConverter.new
|
67
68
|
end
|
68
69
|
|
69
70
|
# Set a new prompt
|
@@ -248,7 +249,7 @@ module TTY
|
|
248
249
|
# @api public
|
249
250
|
def in(value = nil)
|
250
251
|
return @in if value.nil?
|
251
|
-
@in =
|
252
|
+
@in = @range_converter.convert(value)
|
252
253
|
self
|
253
254
|
end
|
254
255
|
|
data/lib/tty/shell/response.rb
CHANGED
@@ -30,6 +30,11 @@ module TTY
|
|
30
30
|
#
|
31
31
|
# @api public
|
32
32
|
def initialize(question, shell = Shell.new)
|
33
|
+
@bool_converter = Conversion::BooleanConverter.new
|
34
|
+
@float_converter = Conversion::FloatConverter.new
|
35
|
+
@range_converter = Conversion::RangeConverter.new
|
36
|
+
@int_converter = Conversion::IntegerConverter.new
|
37
|
+
|
33
38
|
@question = question
|
34
39
|
@shell = shell
|
35
40
|
@reader = Reader.new(shell)
|
@@ -103,14 +108,14 @@ module TTY
|
|
103
108
|
#
|
104
109
|
# @api public
|
105
110
|
def read_int(error = nil)
|
106
|
-
question.evaluate_response
|
111
|
+
question.evaluate_response(@int_converter.convert(read_input))
|
107
112
|
end
|
108
113
|
|
109
114
|
# Read float value
|
110
115
|
#
|
111
116
|
# @api public
|
112
117
|
def read_float(error = nil)
|
113
|
-
question.evaluate_response
|
118
|
+
question.evaluate_response(@float_converter.convert(read_input))
|
114
119
|
end
|
115
120
|
|
116
121
|
# Read regular expression
|
@@ -124,7 +129,7 @@ module TTY
|
|
124
129
|
#
|
125
130
|
# @api public
|
126
131
|
def read_range
|
127
|
-
question.evaluate_response
|
132
|
+
question.evaluate_response(@range_converter.convert(read_input))
|
128
133
|
end
|
129
134
|
|
130
135
|
# Read date
|
@@ -145,7 +150,7 @@ module TTY
|
|
145
150
|
#
|
146
151
|
# @api public
|
147
152
|
def read_bool(error = nil)
|
148
|
-
question.evaluate_response
|
153
|
+
question.evaluate_response(@bool_converter.convert(read_input))
|
149
154
|
end
|
150
155
|
|
151
156
|
# Read file contents
|
data/lib/tty/shell/statement.rb
CHANGED
@@ -34,8 +34,9 @@ module TTY
|
|
34
34
|
# @api public
|
35
35
|
def initialize(shell = Shell.new, options = {})
|
36
36
|
@shell = shell
|
37
|
+
@pastel = Pastel.new
|
37
38
|
@newline = options.fetch(:newline, true)
|
38
|
-
@color = options.fetch(:color,
|
39
|
+
@color = options.fetch(:color, false)
|
39
40
|
end
|
40
41
|
|
41
42
|
# Output the message to the shell
|
@@ -45,7 +46,7 @@ module TTY
|
|
45
46
|
#
|
46
47
|
# @api public
|
47
48
|
def declare(message)
|
48
|
-
message =
|
49
|
+
message = @pastel.decorate message, *color if color
|
49
50
|
|
50
51
|
if newline && /( |\t)(\e\[\d+(;\d+)*m)?\Z/ !~ message
|
51
52
|
shell.output.puts message
|
data/lib/tty/table.rb
CHANGED
@@ -12,8 +12,7 @@ module TTY
|
|
12
12
|
# Once the data is stored in a TTY::Table various operations can be performed
|
13
13
|
# before the information is dumped into a stdout.
|
14
14
|
class Table
|
15
|
-
include Comparable, Enumerable,
|
16
|
-
include Validatable, Equatable
|
15
|
+
include Comparable, Enumerable, Validatable, Equatable
|
17
16
|
extend Forwardable
|
18
17
|
|
19
18
|
# The table header
|
@@ -111,6 +110,7 @@ module TTY
|
|
111
110
|
# @api private
|
112
111
|
def initialize(options = {}, &block)
|
113
112
|
validate_options! options
|
113
|
+
@array_converter = Conversion::ArrayConverter.new
|
114
114
|
@header = (value = options[:header]) ? Header.new(value) : nil
|
115
115
|
@rows = coerce(options.fetch(:rows) { Row.new([]) })
|
116
116
|
@rotated = false
|
@@ -457,7 +457,7 @@ module TTY
|
|
457
457
|
#
|
458
458
|
# @api public
|
459
459
|
def coerce(rows)
|
460
|
-
rows =
|
460
|
+
rows = @array_converter.convert(rows)
|
461
461
|
rows.map { |row| to_row(row, header) }
|
462
462
|
end
|
463
463
|
|
data/lib/tty/table/border.rb
CHANGED
@@ -5,7 +5,7 @@ module TTY
|
|
5
5
|
# Abstract base class that is responsible for building the table border.
|
6
6
|
class Border
|
7
7
|
include Unicode
|
8
|
-
include
|
8
|
+
include Equatable
|
9
9
|
|
10
10
|
EMPTY_CHAR = ''.freeze
|
11
11
|
|
@@ -23,6 +23,8 @@ module TTY
|
|
23
23
|
# The table custom border characters
|
24
24
|
attr_reader :border
|
25
25
|
|
26
|
+
# The table
|
27
|
+
|
26
28
|
class << self
|
27
29
|
# Store characters for border
|
28
30
|
#
|
@@ -46,6 +48,7 @@ module TTY
|
|
46
48
|
else
|
47
49
|
@widths = column_widths
|
48
50
|
@border = TTY::Table::BorderOptions.from options
|
51
|
+
@color = Pastel.new
|
49
52
|
end
|
50
53
|
end
|
51
54
|
|
@@ -96,8 +99,8 @@ module TTY
|
|
96
99
|
# @return [Array[String]]
|
97
100
|
#
|
98
101
|
# @api public
|
99
|
-
def
|
100
|
-
strings.map { |string|
|
102
|
+
def set_color(color, *strings)
|
103
|
+
strings.map { |string| @color.decorate(string, color) }
|
101
104
|
end
|
102
105
|
|
103
106
|
# A line spanning all columns marking top of a table.
|
@@ -137,7 +140,7 @@ module TTY
|
|
137
140
|
# @api public
|
138
141
|
def row_line(row)
|
139
142
|
line = RowLine.new(self['left'], self['center'], self['right'])
|
140
|
-
line.colorize(border.style) if color?
|
143
|
+
line.colorize(self, border.style) if color?
|
141
144
|
|
142
145
|
result = row_heights(row, line)
|
143
146
|
result.empty? ? EMPTY_CHAR : result
|
@@ -197,7 +200,7 @@ module TTY
|
|
197
200
|
self["#{type}_mid"])
|
198
201
|
|
199
202
|
if color? && !line.empty?
|
200
|
-
line =
|
203
|
+
line = set_color(border.style, line)
|
201
204
|
end
|
202
205
|
line
|
203
206
|
end
|
@@ -8,8 +8,8 @@ module TTY
|
|
8
8
|
# Colorize characters with a given style
|
9
9
|
#
|
10
10
|
# @api public
|
11
|
-
def colorize(style)
|
12
|
-
colorized_chars =
|
11
|
+
def colorize(border, style)
|
12
|
+
colorized_chars = border.set_color(style, right, center, left)
|
13
13
|
self.right, self.center, self.left = colorized_chars
|
14
14
|
end
|
15
15
|
end # RowLine
|
data/lib/tty/table/padder.rb
CHANGED
@@ -112,7 +112,7 @@ module TTY
|
|
112
112
|
@column_widths = options.fetch(:column_widths, nil)
|
113
113
|
@column_aligns = Array(options.delete(:column_aligns)).map(&:to_sym)
|
114
114
|
@filter = options.fetch(:filter) { proc { |val, _| val } }
|
115
|
-
@width = options.fetch(:width) { TTY.
|
115
|
+
@width = options.fetch(:width) { TTY::Screen.width }
|
116
116
|
@border_class = options.fetch(:border_class) { Border::Null }
|
117
117
|
@indent = options.fetch(:indent) { 0 }
|
118
118
|
@resize = options.fetch(:resize) { false }
|
data/lib/tty/terminal.rb
CHANGED
@@ -2,26 +2,6 @@
|
|
2
2
|
|
3
3
|
module TTY
|
4
4
|
class Terminal
|
5
|
-
# Return default width of terminal
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# default_width = TTY::Terminal.default_width
|
9
|
-
#
|
10
|
-
# @return [Integer]
|
11
|
-
#
|
12
|
-
# @api public
|
13
|
-
attr_reader :default_width
|
14
|
-
|
15
|
-
# Return default height of terminal
|
16
|
-
#
|
17
|
-
# @example
|
18
|
-
# default_height = TTY::Terminal.default_height
|
19
|
-
#
|
20
|
-
# @return [Integer]
|
21
|
-
#
|
22
|
-
# @api public
|
23
|
-
attr_reader :default_height
|
24
|
-
|
25
5
|
# Return access to color terminal
|
26
6
|
#
|
27
7
|
# @return [TTY::Terminal::Color]
|
@@ -40,107 +20,10 @@ module TTY
|
|
40
20
|
#
|
41
21
|
# @api public
|
42
22
|
def initialize(options = {})
|
43
|
-
@color =
|
23
|
+
@color = Pastel.new
|
44
24
|
@echo = TTY::Terminal::Echo.new
|
45
25
|
@pager = TTY::Terminal::Pager
|
46
26
|
@home = Home.new
|
47
|
-
@default_width = options.fetch(:default_width) { 80 }
|
48
|
-
@default_height = options.fetch(:default_height) { 24 }
|
49
|
-
end
|
50
|
-
|
51
|
-
# Determine current width
|
52
|
-
#
|
53
|
-
# @return [Integer] width
|
54
|
-
#
|
55
|
-
# @api width
|
56
|
-
def width
|
57
|
-
env_tty_columns = ENV['TTY_COLUMNS']
|
58
|
-
if env_tty_columns =~ /^\d+$/
|
59
|
-
env_tty_columns.to_i
|
60
|
-
else
|
61
|
-
TTY::System.unix? ? dynamic_width : default_width
|
62
|
-
end
|
63
|
-
rescue
|
64
|
-
default_width
|
65
|
-
end
|
66
|
-
|
67
|
-
# Determine current height
|
68
|
-
#
|
69
|
-
# @return [Integer] height
|
70
|
-
#
|
71
|
-
# @api public
|
72
|
-
def height
|
73
|
-
env_tty_lines = ENV['TTY_LINES']
|
74
|
-
if env_tty_lines =~ /^\d+$/
|
75
|
-
env_tty_lines.to_i
|
76
|
-
else
|
77
|
-
TTY::System.unix? ? dynamic_height : default_height
|
78
|
-
end
|
79
|
-
rescue
|
80
|
-
default_height
|
81
|
-
end
|
82
|
-
|
83
|
-
# Calculate dynamic width of the terminal
|
84
|
-
#
|
85
|
-
# @return [Integer] width
|
86
|
-
#
|
87
|
-
# @api public
|
88
|
-
def dynamic_width
|
89
|
-
@dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
|
90
|
-
end
|
91
|
-
|
92
|
-
# Calculate dynamic height of the terminal
|
93
|
-
#
|
94
|
-
# @return [Integer] height
|
95
|
-
#
|
96
|
-
# @api public
|
97
|
-
def dynamic_height
|
98
|
-
@dynamic_height ||= (dynamic_height_stty.nonzero? || dynamic_height_tput)
|
99
|
-
end
|
100
|
-
|
101
|
-
# Detect terminal width with stty utility
|
102
|
-
#
|
103
|
-
# @return [Integer] width
|
104
|
-
#
|
105
|
-
# @api public
|
106
|
-
def dynamic_width_stty
|
107
|
-
%x(tty size 2>/dev/null).split[1].to_i
|
108
|
-
end
|
109
|
-
|
110
|
-
# Detect terminal height with stty utility
|
111
|
-
#
|
112
|
-
# @return [Integer] height
|
113
|
-
#
|
114
|
-
# @api public
|
115
|
-
def dynamic_height_stty
|
116
|
-
%x(tty size 2>/dev/null).split[0].to_i
|
117
|
-
end
|
118
|
-
|
119
|
-
# Detect terminal width with tput utility
|
120
|
-
#
|
121
|
-
# @return [Integer] width
|
122
|
-
#
|
123
|
-
# @api public
|
124
|
-
def dynamic_width_tput
|
125
|
-
%x(tput cols 2>/dev/null).to_i
|
126
|
-
end
|
127
|
-
|
128
|
-
# Detect terminal height with tput utility
|
129
|
-
#
|
130
|
-
# @return [Integer] height
|
131
|
-
#
|
132
|
-
# @api public
|
133
|
-
def dynamic_height_tput
|
134
|
-
%x(tput lines 2>/dev/null).to_i
|
135
|
-
end
|
136
|
-
|
137
|
-
# Check if terminal supports color
|
138
|
-
#
|
139
|
-
# @return [Boolean]
|
140
|
-
#
|
141
|
-
# @api public
|
142
|
-
def color?
|
143
|
-
%x(tput colors 2>/dev/null).to_i > 2
|
144
27
|
end
|
145
28
|
|
146
29
|
# Switch echo on
|
data/lib/tty/text/truncation.rb
CHANGED
@@ -20,6 +20,8 @@ module TTY
|
|
20
20
|
|
21
21
|
attr_reader :escape
|
22
22
|
|
23
|
+
attr_reader :color
|
24
|
+
|
23
25
|
# Initialize a Truncation
|
24
26
|
#
|
25
27
|
# @param [String] text
|
@@ -42,6 +44,7 @@ module TTY
|
|
42
44
|
# @api private
|
43
45
|
def initialize(text, *args)
|
44
46
|
options = Utils.extract_options!(args)
|
47
|
+
@color = Pastel.new
|
45
48
|
@text = text
|
46
49
|
@length = options.fetch(:length) { DEFAULT_TRUNCATION_LENGTH }
|
47
50
|
@length = args[0] unless args.empty?
|
@@ -77,7 +80,7 @@ module TTY
|
|
77
80
|
#
|
78
81
|
# @api private
|
79
82
|
def escape_text
|
80
|
-
|
83
|
+
color.strip text.dup
|
81
84
|
end
|
82
85
|
|
83
86
|
# Leave space for the trailing characters
|
data/lib/tty/text/wrapping.rb
CHANGED
@@ -14,6 +14,8 @@ module TTY
|
|
14
14
|
|
15
15
|
attr_reader :padding
|
16
16
|
|
17
|
+
attr_reader :color
|
18
|
+
|
17
19
|
# Initialize a Wrapping
|
18
20
|
#
|
19
21
|
# @param [String] text
|
@@ -38,6 +40,7 @@ module TTY
|
|
38
40
|
@indent = options.fetch(:indent) { 0 }
|
39
41
|
@padding = options.fetch(:padding) { [] }
|
40
42
|
@length = args[0] unless args.empty?
|
43
|
+
@color = Pastel.new
|
41
44
|
end
|
42
45
|
|
43
46
|
# Wrap a text into lines no longer than length
|
@@ -63,7 +66,7 @@ module TTY
|
|
63
66
|
#
|
64
67
|
# @api private
|
65
68
|
def actual_length(string)
|
66
|
-
length + (string.length -
|
69
|
+
length + (string.length - color.strip(string).length)
|
67
70
|
end
|
68
71
|
|
69
72
|
# Wrap line at given length
|
data/lib/tty/vector.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module TTY
|
4
4
|
# This class represents a mathematical vector.
|
5
5
|
class Vector
|
6
|
-
include Enumerable, Equatable
|
6
|
+
include Enumerable, Equatable
|
7
7
|
|
8
8
|
attr_reader :elements
|
9
9
|
protected :elements
|
@@ -16,7 +16,7 @@ module TTY
|
|
16
16
|
#
|
17
17
|
# @api public
|
18
18
|
def self.[](*array)
|
19
|
-
new
|
19
|
+
new(array)
|
20
20
|
end
|
21
21
|
|
22
22
|
# Instantiate a Vector
|
@@ -27,7 +27,8 @@ module TTY
|
|
27
27
|
#
|
28
28
|
# @api public
|
29
29
|
def initialize(array = [])
|
30
|
-
@
|
30
|
+
@array_converter = Conversion::ArrayConverter.new
|
31
|
+
@elements = @array_converter.convert(array)
|
31
32
|
end
|
32
33
|
|
33
34
|
# Return element at index.
|
data/lib/tty/version.rb
CHANGED
@@ -2,12 +2,11 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe TTY::Conversion do
|
6
|
-
let(:
|
7
|
-
let(:object) { new_class.new }
|
5
|
+
describe TTY::Conversion::ArrayConverter do
|
6
|
+
let(:object) { described_class.new }
|
8
7
|
let(:enumerable) { [] }
|
9
8
|
|
10
|
-
subject { object.
|
9
|
+
subject { object.convert(enumerable) }
|
11
10
|
|
12
11
|
context 'Array type' do
|
13
12
|
it { is_expected.to eq(enumerable) }
|
@@ -23,7 +22,7 @@ describe TTY::Conversion do
|
|
23
22
|
|
24
23
|
context 'responds to #to_ary' do
|
25
24
|
let(:converted) { [] }
|
26
|
-
let(:enumerable) { double('Enumerable', :
|
25
|
+
let(:enumerable) { double('Enumerable', to_ary: converted) }
|
27
26
|
|
28
27
|
it { is_expected.to eq(converted) }
|
29
28
|
end
|