strings 0.1.8 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8598a1eef564d4250363a7d49402bd1fe2e45880488042749c55fb4675ac090
4
- data.tar.gz: c33aba524285885f8c9872bc4bea15a1242ebda47b532aaabe864975475ce9a1
3
+ metadata.gz: 4d112bf8f896e770e61346f4f64c2dff39c1d5ec630321d811fadbcf4a96cb0b
4
+ data.tar.gz: 58242b6240dc5400dd1262a4e7692574d60fe2415e6957db8bf7ec0fd52d861c
5
5
  SHA512:
6
- metadata.gz: 5fa16d822454ebc169d6edb15b07e0db169bd5992d29ed00e6a00dc83ba06ce0dba04135cb3eabea5015d48ef41d043f61f8cd98f2afd2d079e4a99d7c981536
7
- data.tar.gz: c4a24318dd99910246a3b047dddfd2e0e04186426d2ae39842ec9f6a6c84a008b5cb3697036dd62c5646fe49b5e50b602c853e76ccf5fbc354b0dae392ac7bf4
6
+ metadata.gz: 68e35d997825c8f3f5349236688db5be5b7c1fc8f5b560f53321e666a4e7dc7dd94c837fe6bf13d97c13d22d34010f0201d3a697956b212c157d835a3549fb15
7
+ data.tar.gz: '087381e615bfa11ddeeacdeca1fbfb866ca4c70977f60df1ad4d02e7fd25b12154e32158e32ada3fe4815ce122288a097112877e85c3ac0cbe4e6bf284b878c0'
@@ -1,5 +1,12 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.2.0] - 2020-08-11
4
+
5
+ ### Changed
6
+ * Change String#wrap to preserve newline character breaks
7
+ * Change gemspec to remove test artefacts and bundler dev dependency
8
+ * Change gemspec to require Ruby >= 2.0.0
9
+
3
10
  ## [v0.1.8] - 2019-11-24
4
11
 
5
12
  ### Fixed
@@ -53,6 +60,7 @@
53
60
 
54
61
  * Initial implementation and release
55
62
 
63
+ [v0.2.0]: https://github.com/piotrmurach/strings/compare/v0.1.8...v0.2.0
56
64
  [v0.1.8]: https://github.com/piotrmurach/strings/compare/v0.1.7...v0.1.8
57
65
  [v0.1.7]: https://github.com/piotrmurach/strings/compare/v0.1.6...v0.1.7
58
66
  [v0.1.6]: https://github.com/piotrmurach/strings/compare/v0.1.5...v0.1.6
data/README.md CHANGED
@@ -18,7 +18,7 @@
18
18
  [coverage]: https://coveralls.io/github/piotrmurach/strings?branch=master
19
19
  [inchpages]: http://inch-ci.org/github/piotrmurach/strings
20
20
 
21
- > The `Strings` is a set of useful functions such as fold, truncate, wrap, and many more for transforming strings.
21
+ > A set of useful methods for working with strings such as align, truncate, wrap, and many more.
22
22
 
23
23
  ## Installation
24
24
 
@@ -293,7 +293,7 @@ To wrap text into lines no longer than `wrap_at` argument length, the `wrap` met
293
293
  Given the following text:
294
294
 
295
295
  ```ruby
296
- text "Think not, is my eleventh commandment; and sleep when you can, is my twelfth."
296
+ text = "Think not, is my eleventh commandment; and sleep when you can, is my twelfth."
297
297
  ```
298
298
 
299
299
  Then to wrap the text to given length do:
@@ -377,6 +377,8 @@ using Strings::Extensions
377
377
  | ------------ | ----------- | -------- |
378
378
  | [strings-ansi](https://github.com/piotrmurach/strings-ansi) | Handle ANSI escape codes in strings. | [docs](http://www.rubydoc.info/gems/strings-ansi) |
379
379
  | [strings-case](https://github.com/piotrmurach/strings-case) | Handle case transformations in strings. | [docs](http://www.rubydoc.info/gems/strings-case) |
380
+ | [strings-inflection](https://github.com/piotrmurach/strings-inflection) | Inflects English nouns and verbs. | [docs](http://www.rubydoc.info/gems/strings-inflection) |
381
+ | [strings-numeral](https://github.com/piotrmurach/strings-numeral) | Express numbers as word numerals. | [docs](http://www.rubydoc.info/gems/strings-numeral) |
380
382
 
381
383
  ## Development
382
384
 
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
3
+ require "strings-ansi"
4
4
 
5
- require_relative 'strings/align'
6
- require_relative 'strings/fold'
7
- require_relative 'strings/pad'
8
- require_relative 'strings/truncate'
9
- require_relative 'strings/wrap'
10
- require_relative 'strings/version'
5
+ require_relative "strings/align"
6
+ require_relative "strings/fold"
7
+ require_relative "strings/pad"
8
+ require_relative "strings/truncate"
9
+ require_relative "strings/wrap"
10
+ require_relative "strings/version"
11
11
 
12
12
  module Strings
13
13
  # Align text within the width.
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
4
- require 'unicode/display_width'
3
+ require "strings-ansi"
4
+ require "unicode/display_width"
5
5
 
6
6
  module Strings
7
7
  # Responsible for text alignment
8
8
  module Align
9
- NEWLINE = "\n".freeze
10
- SPACE = " ".freeze
9
+ NEWLINE = "\n"
10
+ SPACE = " "
11
11
  LINE_BREAK = %r{\r\n|\r|\n}.freeze
12
12
 
13
13
  # Aligns text within the width.
@@ -32,12 +32,13 @@ module Strings
32
32
  # Strings::Align(text, 22, direction: :right)
33
33
  # # => " the madness of men"
34
34
  #
35
- # Strings::Align.align(text, 22, direction: :center, fill: '*')
35
+ # Strings::Align.align(text, 22, direction: :center, fill: "*")
36
36
  # # => "***the madness of men***"
37
37
  #
38
38
  # @api public
39
39
  def align(text, width, direction: :left, **options)
40
40
  return text if width.nil?
41
+
41
42
  method = to_alignment(direction)
42
43
  send(method, text, width, **options)
43
44
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../strings'
3
+ require_relative "../strings"
4
4
 
5
5
  module Strings
6
6
  module Extensions
@@ -20,7 +20,7 @@ module Strings
20
20
  def fold(text, separator = LINE_BREAK)
21
21
  text.gsub(/([ ]+)#{separator}/, "\\1")
22
22
  .gsub(/#{separator}(?<space>[ ]+)/, "\\k<space>")
23
- .gsub(/#{separator}/, ' ')
23
+ .gsub(/#{separator}/, " ")
24
24
  end
25
25
  module_function :fold
26
26
  end # Fold
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
4
- require 'unicode/display_width'
3
+ require "strings-ansi"
4
+ require "unicode/display_width"
5
5
 
6
- require_relative 'padder'
6
+ require_relative "padder"
7
7
 
8
8
  module Strings
9
9
  # Responsible for text padding
10
10
  module Pad
11
- NEWLINE = "\n".freeze
12
- SPACE = " ".freeze
13
- LINE_BREAK = %r{\r\n|\r|\n}.freeze
11
+ NEWLINE = "\n"
12
+ SPACE = " "
13
+ LINE_BREAK = %r{\r\n|\r|\n}.freeze
14
14
 
15
15
  # Apply padding to multiline text with ANSI codes
16
16
  #
@@ -79,7 +79,7 @@ module Strings
79
79
  # @api private
80
80
  def max_line_length(text, separator)
81
81
  lines = text.split(separator, -1)
82
- display_width(lines.max_by { |line| display_width(line) } || '')
82
+ display_width(lines.max_by { |line| display_width(line) } || "")
83
83
  end
84
84
  module_function :max_line_length
85
85
 
@@ -1,4 +1,4 @@
1
- # frozen_string_litera: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Strings
4
4
  # A class responsible for parsing padding value
@@ -45,7 +45,7 @@ module Strings
45
45
  elsif value.size == 4
46
46
  value
47
47
  else
48
- raise ParseError, 'Wrong :padding parameter, must be an array'
48
+ raise ParseError, "Wrong :padding parameter, must be an array"
49
49
  end
50
50
  end
51
51
 
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
4
- require 'unicode/display_width'
5
- require 'unicode_utils/each_grapheme'
3
+ require "strings-ansi"
4
+ require "unicode/display_width"
5
+ require "unicode_utils/each_grapheme"
6
6
 
7
7
  module Strings
8
8
  # A module responsible for text truncation
9
9
  module Truncate
10
- DEFAULT_TRAILING = ''.freeze
10
+ DEFAULT_TRAILING = "".freeze
11
11
 
12
12
  DEFAULT_LENGTH = 30
13
13
 
@@ -32,10 +32,10 @@ module Strings
32
32
  # Strings::Truncate.truncate(text, 20)
33
33
  # # => "The sovereignest t…"
34
34
  #
35
- # Strings::Truncate.truncate(text, 20, separator: ' ' )
35
+ # Strings::Truncate.truncate(text, 20, separator: " " )
36
36
  # # => "The sovereignest…"
37
37
  #
38
- # Strings::Truncate.truncate(text, 40, trailing: '... (see more)' )
38
+ # Strings::Truncate.truncate(text, 40, trailing: "... (see more)" )
39
39
  # # => "The sovereignest thing on... (see more)"
40
40
  #
41
41
  # @api public
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strings
4
- VERSION = "0.1.8"
4
+ VERSION = "0.2.0"
5
5
  end # Strings
@@ -1,18 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'strings-ansi'
4
- require 'unicode/display_width'
5
- require 'unicode_utils/each_grapheme'
6
-
7
- require_relative 'fold'
3
+ require "strings-ansi"
4
+ require "unicode/display_width"
5
+ require "unicode_utils/each_grapheme"
8
6
 
9
7
  module Strings
10
8
  module Wrap
11
9
  DEFAULT_WIDTH = 80
12
- NEWLINE = "\n".freeze
13
- SPACE = " ".freeze
10
+ NEWLINE = "\n"
11
+ SPACE = " "
14
12
  LINE_BREAK = %r{\r\n|\r|\n}.freeze
15
- LINE_BREAKS = "\r\n+|\r+|\n+".freeze
13
+ LINE_BREAKS = "\r\n+|\r+|\n+"
16
14
 
17
15
  # Wrap a text into lines no longer than wrap_at length.
18
16
  # Preserves existing lines and existing word boundaries.
@@ -22,31 +20,30 @@ module Strings
22
20
  # # => "Some \nlongish \ntext"
23
21
  #
24
22
  # @api public
25
- def wrap(text, wrap_at = DEFAULT_WIDTH, separator: nil)
23
+ def wrap(text, wrap_at = DEFAULT_WIDTH, separator: NEWLINE)
26
24
  if text.scan(/[[:print:]]/).length < wrap_at.to_i || wrap_at.to_i.zero?
27
25
  return text
28
26
  end
27
+
29
28
  ansi_stack = []
30
- sep = separator || text[LINE_BREAK] || NEWLINE
31
- text.split(%r{#{LINE_BREAKS}}, -1).map do |paragraph|
32
- format_paragraph(paragraph, wrap_at, ansi_stack)
33
- end * sep
29
+ text.lines.map do |line|
30
+ format_line(line, wrap_at, ansi_stack).join(separator)
31
+ end.join
34
32
  end
35
33
  module_function :wrap
36
34
 
37
- # Format paragraph to be maximum of wrap_at length
35
+ # Format line to be maximum of wrap_at length
38
36
  #
39
- # @param [String] paragraph
40
- # the paragraph to format
37
+ # @param [String] text_line
38
+ # the line to format
41
39
  # @param [Integer] wrap_at
42
- # the maximum length to wrap the paragraph
40
+ # the maximum length to wrap the line
43
41
  #
44
42
  # @return [Array[String]]
45
43
  # the wrapped lines
46
44
  #
47
45
  # @api private
48
- def format_paragraph(paragraph, wrap_at, ansi_stack)
49
- cleared_para = Fold.fold(paragraph)
46
+ def format_line(text_line, wrap_at, ansi_stack)
50
47
  lines = []
51
48
  line = []
52
49
  word = []
@@ -55,10 +52,10 @@ module Strings
55
52
  word_length = 0
56
53
  line_length = 0
57
54
  char_length = 0 # visible char length
58
- text_length = display_width(cleared_para)
55
+ text_length = display_width(text_line)
59
56
  total_length = 0
60
57
 
61
- UnicodeUtils.each_grapheme(cleared_para) do |char|
58
+ UnicodeUtils.each_grapheme(text_line) do |char|
62
59
  # we found ansi let's consume
63
60
  if char == Strings::ANSI::CSI || ansi.length > 0
64
61
  ansi << char
@@ -115,7 +112,7 @@ module Strings
115
112
  lines << insert_ansi(word.join, ansi_stack) unless word.empty?
116
113
  lines
117
114
  end
118
- module_function :format_paragraph
115
+ module_function :format_line
119
116
 
120
117
  # Insert ANSI code into string
121
118
  #
@@ -151,7 +148,11 @@ module Strings
151
148
  matched_reset = false
152
149
  new_stack << ansi # keep the ansi
153
150
  next if ansi[1] == length
154
- output.insert(-1, ansi_reset) # add reset at the end
151
+ if output.end_with?(NEWLINE)
152
+ output.insert(-2, ansi_reset)
153
+ else
154
+ output.insert(-1, ansi_reset) # add reset at the end
155
+ end
155
156
  end
156
157
 
157
158
  output.insert(ansi[1], ansi[0])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-24 00:00:00.000000000 Z
11
+ date: 2020-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: strings-ansi
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.1'
26
+ version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: unicode_utils
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
- - !ruby/object:Gem::Dependency
56
- name: bundler
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '1.5'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '1.5'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: rake
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,20 +80,20 @@ dependencies:
94
80
  - - ">="
95
81
  - !ruby/object:Gem::Version
96
82
  version: '3.0'
97
- description: A set of useful functions such as fold, truncate, wrap and more for transoforming
98
- strings.
83
+ description: A set of methods for working with strings such as align, truncate, wrap
84
+ and many more.
99
85
  email:
100
- - me@piotrmurach.com
86
+ - piotr@piotrmurach.com
101
87
  executables: []
102
88
  extensions: []
103
- extra_rdoc_files: []
89
+ extra_rdoc_files:
90
+ - README.md
91
+ - CHANGELOG.md
92
+ - LICENSE.txt
104
93
  files:
105
94
  - CHANGELOG.md
106
95
  - LICENSE.txt
107
96
  - README.md
108
- - Rakefile
109
- - bin/console
110
- - bin/setup
111
97
  - lib/strings.rb
112
98
  - lib/strings/align.rb
113
99
  - lib/strings/extensions.rb
@@ -117,28 +103,6 @@ files:
117
103
  - lib/strings/truncate.rb
118
104
  - lib/strings/version.rb
119
105
  - lib/strings/wrap.rb
120
- - spec/spec_helper.rb
121
- - spec/unit/align/align_left_spec.rb
122
- - spec/unit/align/align_right_spec.rb
123
- - spec/unit/align/align_spec.rb
124
- - spec/unit/align_spec.rb
125
- - spec/unit/ansi_spec.rb
126
- - spec/unit/extensions_spec.rb
127
- - spec/unit/fold/fold_spec.rb
128
- - spec/unit/fold_spec.rb
129
- - spec/unit/pad/pad_spec.rb
130
- - spec/unit/pad_spec.rb
131
- - spec/unit/padder/parse_spec.rb
132
- - spec/unit/sanitize_spec.rb
133
- - spec/unit/truncate/truncate_spec.rb
134
- - spec/unit/truncate_spec.rb
135
- - spec/unit/wrap/insert_ansi_spec.rb
136
- - spec/unit/wrap/wrap_spec.rb
137
- - spec/unit/wrap_spec.rb
138
- - strings.gemspec
139
- - tasks/console.rake
140
- - tasks/coverage.rake
141
- - tasks/spec.rake
142
106
  homepage: https://github.com/piotrmurach/strings
143
107
  licenses:
144
108
  - MIT
@@ -156,15 +120,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
120
  requirements:
157
121
  - - ">="
158
122
  - !ruby/object:Gem::Version
159
- version: '0'
123
+ version: 2.0.0
160
124
  required_rubygems_version: !ruby/object:Gem::Requirement
161
125
  requirements:
162
126
  - - ">="
163
127
  - !ruby/object:Gem::Version
164
128
  version: '0'
165
129
  requirements: []
166
- rubygems_version: 3.0.6
130
+ rubygems_version: 3.1.2
167
131
  signing_key:
168
132
  specification_version: 4
169
- summary: A set of useful functions for transforming strings.
133
+ summary: A set of methods for working with strings.
170
134
  test_files: []