tty-box 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22f794bf37522fed08b625102db5d264f79aca278567b6b1c1963d7c51e13d81
4
- data.tar.gz: 5bc28c0eb6c16fc60d5325987c5f03e7a68e608c0cb1bea6671bee08733c4f33
3
+ metadata.gz: 5f6930141848be48d0af21960b3db9857af6aa5b22fa4bab1f3163406c9c1c54
4
+ data.tar.gz: c05ed307504d3a16fc4f1aa00d4d622fd7566a27b65ed22a1309397e596d2bb6
5
5
  SHA512:
6
- metadata.gz: ffe110f742b307902689234d61eee012709dac09b5723dc08617a5f605c0585e01c7d4e65282ab888a62719e8ca8a9b3d7f1d7e726e8d854ae84caf6d322c3d6
7
- data.tar.gz: 6d966972cbd28e03edd08c6e80ef96db438ccdb2ac3343fb8cd4c6eea0db7bb125e510ed0b1b4d20864b6f252f590a9d8abf3e36a7cb0eae1d2b7f5b36b6c0f8
6
+ metadata.gz: 3c493c40374897de1b34b1295101ce33ff3ce401005c3fe300c314d31d413ff1b93fdbb1af29a1352fa6cebb12c5f45f46032b58ef68d4f9afe9b6d8892561df
7
+ data.tar.gz: db800e05bef214944f5e2d6aff324b9cf9700de701652004ac90113148764079364f4457eed82df1cf7c28f322ff28d9b282a111f6cb9c0578c8ecde57b06008
@@ -1,5 +1,13 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.4.1] - 2019-08-28
4
+
5
+ ### Added
6
+ * Add example to demonstrate different line endings
7
+
8
+ ### Fixed
9
+ * Fix to handle different line endings
10
+
3
11
  ## [v0.4.0] - 2019-06-05
4
12
 
5
13
  ### Changed
@@ -34,6 +42,7 @@
34
42
 
35
43
  * Initial implementation and release
36
44
 
45
+ [v0.4.1]: https://github.com/piotrmurach/tty-box/compare/v0.4.0...v0.4.1
37
46
  [v0.4.0]: https://github.com/piotrmurach/tty-box/compare/v0.3.0...v0.4.0
38
47
  [v0.3.0]: https://github.com/piotrmurach/tty-box/compare/v0.2.1...v0.3.0
39
48
  [v0.2.1]: https://github.com/piotrmurach/tty-box/compare/v0.2.0...v0.2.1
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/tty-box"
4
+
5
+ box = TTY::Box.frame(
6
+ width: 29,
7
+ height: 7,
8
+ align: :center,
9
+ padding: 1
10
+ ) do
11
+ "Closes #360\r\n\r\nCloses !217"
12
+ end
13
+
14
+ puts box
@@ -11,6 +11,10 @@ module TTY
11
11
  module Box
12
12
  module_function
13
13
 
14
+ NEWLINE = "\n"
15
+
16
+ LINE_BREAK = %r{\r\n|\r|\n}.freeze
17
+
14
18
  BOX_CHARS = {
15
19
  ascii: %w[+ + + + + + + + - | +],
16
20
  light: %w[┘ ┐ ┌ └ ┤ ┴ ┬ ├ ─ │ ┼],
@@ -76,6 +80,7 @@ module TTY
76
80
  title: {}, border: :light, style: {})
77
81
  output = []
78
82
  content = []
83
+ sep = NEWLINE
79
84
  position = top && left
80
85
 
81
86
  border = Border.parse(border)
@@ -85,7 +90,9 @@ module TTY
85
90
  right_size = border.right ? 1 : 0
86
91
 
87
92
  if block_given?
88
- content = format(yield, width, padding, align)
93
+ str = yield
94
+ sep = str[LINE_BREAK] || NEWLINE # infer line break
95
+ content = format(str, width, padding, align)
89
96
  end
90
97
 
91
98
  fg, bg = *extract_style(style)
@@ -94,7 +101,7 @@ module TTY
94
101
  if border.top?
95
102
  output << cursor.move_to(left, top) if position
96
103
  output << top_border(title, width, border, style)
97
- output << "\n" unless position
104
+ output << sep unless position
98
105
  end
99
106
 
100
107
  (height - top_size - bottom_size).times do |i|
@@ -106,7 +113,8 @@ module TTY
106
113
  content_size = width - left_size - right_size
107
114
  unless content[i].nil?
108
115
  output << bg.(fg.(content[i]))
109
- content_size -= Strings::ANSI.sanitize(content[i]).size
116
+ size = Strings::ANSI.sanitize(content[i]).scan(/[[:print:]]/).join.size
117
+ content_size -= size
110
118
  end
111
119
  if style[:fg] || style[:bg] || !position # something to color
112
120
  output << bg.(fg.(' ' * content_size))
@@ -118,13 +126,13 @@ module TTY
118
126
  end
119
127
  output << border_bg.(border_fg.(pipe_char(border.type)))
120
128
  end
121
- output << "\n" unless position
129
+ output << sep unless position
122
130
  end
123
131
 
124
132
  if border.bottom?
125
133
  output << cursor.move_to(left, top + height - bottom_size) if position
126
134
  output << bottom_border(title, width, border, style)
127
- output << "\n" unless position
135
+ output << sep unless position
128
136
  end
129
137
 
130
138
  output.join
@@ -138,11 +146,12 @@ module TTY
138
146
  def format(content, width, padding, align)
139
147
  pad = Strings::Padder.parse(padding)
140
148
  total_width = width - 2 - (pad.left + pad.right)
149
+ sep = content[LINE_BREAK] || NEWLINE # infer line break
141
150
 
142
151
  wrapped = Strings.wrap(content, total_width)
143
152
  aligned = Strings.align(wrapped, total_width, direction: align)
144
153
  padded = Strings.pad(aligned, padding)
145
- padded.split("\n")
154
+ padded.split(sep)
146
155
  end
147
156
 
148
157
  # Convert style keywords into styling
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  module Box
5
- VERSION = "0.4.0"
5
+ VERSION = "0.4.1"
6
6
  end # Box
7
7
  end # TTY
@@ -1,4 +1,6 @@
1
- RSpec.describe TTY::Box, '#frame' do
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::Box, "#frame" do
2
4
  it "creates frame with only width & height values" do
3
5
  output = TTY::Box.frame(width: 35, height: 4)
4
6
 
@@ -58,4 +60,23 @@ RSpec.describe TTY::Box, '#frame' do
58
60
  "└─────────────────────────────────┘\n"
59
61
  ].join)
60
62
  end
63
+
64
+ it "handles \r\n line breaks" do
65
+ box = TTY::Box.frame(
66
+ width: 29,
67
+ height: 7
68
+ ) do
69
+ "Closes #360\r\n\r\nCloses !217"
70
+ end
71
+
72
+ expect(box).to eq([
73
+ "┌───────────────────────────┐\r\n",
74
+ "│Closes #360 │\r\n",
75
+ "│ │\r\n",
76
+ "│Closes !217 │\r\n",
77
+ "│ │\r\n",
78
+ "│ │\r\n",
79
+ "└───────────────────────────┘\r\n"
80
+ ].join)
81
+ end
61
82
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe TTY::Box, ':padding option' do
2
4
  it "padds internal content without position arguments" do
3
5
  box = TTY::Box.frame(width: 30, height: 6, padding: 1) do
@@ -43,4 +45,24 @@ RSpec.describe TTY::Box, ':padding option' do
43
45
  "\e[6;1H└────────────────────────────┘"
44
46
  ].join)
45
47
  end
48
+
49
+ it "handles \r\n line breaks when padding" do
50
+ box = TTY::Box.frame(
51
+ width: 29,
52
+ height: 7,
53
+ padding: 1
54
+ ) do
55
+ "Closes #360\r\n\r\nCloses !217"
56
+ end
57
+
58
+ expect(box).to eq([
59
+ "┌───────────────────────────┐\r\n",
60
+ "│ │\r\n",
61
+ "│ Closes #360 │\r\n",
62
+ "│ │\r\n",
63
+ "│ Closes !217 │\r\n",
64
+ "│ │\r\n",
65
+ "└───────────────────────────┘\r\n"
66
+ ].join)
67
+ end
46
68
  end
@@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.required_ruby_version = '>= 2.0.0'
22
22
 
23
- spec.add_dependency 'pastel', '~> 0.7.2'
24
- spec.add_dependency 'tty-cursor', '~> 0.7'
25
- spec.add_dependency 'strings', '~> 0.1.5'
23
+ spec.add_dependency "pastel", "~> 0.7.2"
24
+ spec.add_dependency "tty-cursor", "~> 0.7"
25
+ spec.add_dependency "strings", "~> 0.1.6"
26
26
 
27
27
  spec.add_development_dependency "bundler", ">= 1.5"
28
28
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-04 00:00:00.000000000 Z
11
+ date: 2019-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.1.5
47
+ version: 0.1.6
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.1.5
54
+ version: 0.1.6
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +109,7 @@ files:
109
109
  - bin/setup
110
110
  - examples/commander.rb
111
111
  - examples/connected.rb
112
+ - examples/newline.rb
112
113
  - lib/tty-box.rb
113
114
  - lib/tty/box.rb
114
115
  - lib/tty/box/border.rb