strings 0.1.5 → 0.2.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/CHANGELOG.md +40 -0
- data/README.md +17 -12
- data/lib/strings.rb +9 -9
- data/lib/strings/align.rb +23 -16
- data/lib/strings/extensions.rb +15 -11
- data/lib/strings/fold.rb +2 -2
- data/lib/strings/pad.rb +13 -11
- data/lib/strings/padder.rb +4 -4
- data/lib/strings/truncate.rb +9 -8
- data/lib/strings/version.rb +1 -1
- data/lib/strings/wrap.rb +63 -53
- metadata +28 -52
- data/Rakefile +0 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/spec/spec_helper.rb +0 -37
- data/spec/unit/align/align_left_spec.rb +0 -62
- data/spec/unit/align/align_right_spec.rb +0 -62
- data/spec/unit/align/align_spec.rb +0 -77
- data/spec/unit/align_spec.rb +0 -18
- data/spec/unit/ansi_spec.rb +0 -7
- data/spec/unit/extensions_spec.rb +0 -51
- data/spec/unit/fold/fold_spec.rb +0 -28
- data/spec/unit/fold_spec.rb +0 -7
- data/spec/unit/pad/pad_spec.rb +0 -63
- data/spec/unit/pad_spec.rb +0 -12
- data/spec/unit/padder/parse_spec.rb +0 -35
- data/spec/unit/sanitize_spec.rb +0 -7
- data/spec/unit/truncate/truncate_spec.rb +0 -70
- data/spec/unit/truncate_spec.rb +0 -8
- data/spec/unit/wrap/wrap_spec.rb +0 -155
- data/spec/unit/wrap_spec.rb +0 -15
- data/strings.gemspec +0 -28
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 763d67f8c555aab9f45587b8596a66f75d751b965fcf2782b1920bf5d5b97f6b
|
4
|
+
data.tar.gz: a894e46873337664f6a35863a6bffdbaa1603d1f9ddcacf448de9841226a43e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eddc0eb59fcf78d5d5c9d84246821cbf9559387d6c94f090bea855ec4928bb8648b464832c97b3c442b3a51748d41a51fc304fc4d5a38440d3889b4924e5ad65
|
7
|
+
data.tar.gz: 711fe7e35fa2c893a5e94f98e47674a16fc5e814c67d941101bead1e03d076ac6bf38e8e17a579c3a3353a3b30518214537fea040b45d7a149cd597d6a31134b
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,40 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.2.1] - 2021-03-09
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change to relax unicode-display_width version constraint to allow 2.0 release
|
7
|
+
|
8
|
+
## [v0.2.0] - 2020-08-11
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
* Change String#wrap to preserve newline character breaks
|
12
|
+
* Change gemspec to remove test artefacts and bundler dev dependency
|
13
|
+
* Change gemspec to require Ruby >= 2.0.0
|
14
|
+
|
15
|
+
## [v0.1.8] - 2019-11-24
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
* Fix Ruby 2.7 warnings by Ryan Davis(@zenspider)
|
19
|
+
|
20
|
+
## [v0.1.7] - 2019-11-14
|
21
|
+
|
22
|
+
### Added
|
23
|
+
* Add metadata to gemspec
|
24
|
+
|
25
|
+
### Fixed
|
26
|
+
* Fix Truncate#truncate to accept length of 1 by Katelyn Schiesser(@slowbro)
|
27
|
+
|
28
|
+
## [v0.1.6] - 2019-08-28
|
29
|
+
|
30
|
+
### Changed
|
31
|
+
* Change Wrap#wrap, Align#align & Pad#pad to handle different line endings
|
32
|
+
* Change Pad#pad to pad empty lines
|
33
|
+
|
34
|
+
### Fixed
|
35
|
+
* Fix Wrap#wrap to handle adjacent ANSI codes
|
36
|
+
* Fix Wrap#insert_ansi to handle nested ANSI codes
|
37
|
+
|
3
38
|
## [v0.1.5] - 2019-03-29
|
4
39
|
|
5
40
|
### Changed
|
@@ -30,6 +65,11 @@
|
|
30
65
|
|
31
66
|
* Initial implementation and release
|
32
67
|
|
68
|
+
[v0.2.1]: https://github.com/piotrmurach/strings/compare/v0.2.0...v0.2.1
|
69
|
+
[v0.2.0]: https://github.com/piotrmurach/strings/compare/v0.1.8...v0.2.0
|
70
|
+
[v0.1.8]: https://github.com/piotrmurach/strings/compare/v0.1.7...v0.1.8
|
71
|
+
[v0.1.7]: https://github.com/piotrmurach/strings/compare/v0.1.6...v0.1.7
|
72
|
+
[v0.1.6]: https://github.com/piotrmurach/strings/compare/v0.1.5...v0.1.6
|
33
73
|
[v0.1.5]: https://github.com/piotrmurach/strings/compare/v0.1.4...v0.1.5
|
34
74
|
[v0.1.4]: https://github.com/piotrmurach/strings/compare/v0.1.3...v0.1.4
|
35
75
|
[v0.1.3]: https://github.com/piotrmurach/strings/compare/v0.1.2...v0.1.3
|
data/README.md
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
<div align="center">
|
2
|
-
<img width="225" src="https://
|
2
|
+
<img width="225" src="https://github.com/piotrmurach/strings/blob/master/assets/strings_logo.png" alt="strings logo" />
|
3
3
|
</div>
|
4
4
|
|
5
5
|
# Strings
|
6
6
|
|
7
7
|
[][gem]
|
8
|
-
[][gh_actions_ci]
|
9
9
|
[][appveyor]
|
10
10
|
[][codeclimate]
|
11
11
|
[][coverage]
|
12
12
|
[][inchpages]
|
13
13
|
|
14
14
|
[gem]: http://badge.fury.io/rb/strings
|
15
|
-
[
|
15
|
+
[gh_actions_ci]: https://github.com/piotrmurach/strings/actions?query=workflow%3ACI
|
16
16
|
[appveyor]: https://ci.appveyor.com/project/piotrmurach/strings
|
17
17
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/strings/maintainability
|
18
18
|
[coverage]: https://coveralls.io/github/piotrmurach/strings?branch=master
|
19
19
|
[inchpages]: http://inch-ci.org/github/piotrmurach/strings
|
20
20
|
|
21
|
-
>
|
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
|
|
@@ -43,6 +43,7 @@ Or install it yourself as:
|
|
43
43
|
* Supports multibyte character encodings such as UTF-8, EUC-JP
|
44
44
|
* Handles languages without white-spaces between words (like Chinese and Japanese)
|
45
45
|
* Supports ANSI escape codes
|
46
|
+
* Flexible by nature, split into [components](#4-components)
|
46
47
|
|
47
48
|
## Contents
|
48
49
|
|
@@ -56,7 +57,7 @@ Or install it yourself as:
|
|
56
57
|
* [2.6 truncate](#26-truncate)
|
57
58
|
* [2.7 wrap](#27-wrap)
|
58
59
|
* [3. Extending String class](#3-extending-string-class)
|
59
|
-
* [4.
|
60
|
+
* [4. Components](#4-components)
|
60
61
|
|
61
62
|
## 1. Usage
|
62
63
|
|
@@ -260,7 +261,7 @@ Strings::Truncate.truncate(text, 20) # => "for there is no fol…"
|
|
260
261
|
If you want to split words on their boundaries use `:separator` option:
|
261
262
|
|
262
263
|
```ruby
|
263
|
-
Strings.truncate(text, 20) # => "for there is no…"
|
264
|
+
Strings.truncate(text, 20, separator: ' ') # => "for there is no…"
|
264
265
|
```
|
265
266
|
|
266
267
|
Use `:trailing` option (by default `…`) to provide omission characters:
|
@@ -292,7 +293,7 @@ To wrap text into lines no longer than `wrap_at` argument length, the `wrap` met
|
|
292
293
|
Given the following text:
|
293
294
|
|
294
295
|
```ruby
|
295
|
-
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."
|
296
297
|
```
|
297
298
|
|
298
299
|
Then to wrap the text to given length do:
|
@@ -368,13 +369,17 @@ require 'strings/extensions'
|
|
368
369
|
using Strings::Extensions
|
369
370
|
```
|
370
371
|
|
371
|
-
## 4.
|
372
|
+
## 4. Components
|
372
373
|
|
373
|
-
**Strings** aims to be flexible and allow you to choose only the
|
374
|
+
**Strings** aims to be flexible and allow you to choose only the components that you need. Currently you can choose from:
|
374
375
|
|
375
|
-
|
|
376
|
+
| Component | Description | API docs |
|
376
377
|
| ------------ | ----------- | -------- |
|
377
|
-
| [strings-ansi](https://github.com/piotrmurach/strings-ansi) | Handle ANSI escape codes in strings. | [docs](
|
378
|
+
| [strings-ansi](https://github.com/piotrmurach/strings-ansi) | Handle ANSI escape codes in strings. | [docs](https://www.rubydoc.info/gems/strings-ansi) |
|
379
|
+
| [strings-case](https://github.com/piotrmurach/strings-case) | Handle case transformations in strings. | [docs](https://www.rubydoc.info/gems/strings-case) |
|
380
|
+
| [strings-inflection](https://github.com/piotrmurach/strings-inflection) | Inflects English nouns and verbs. | [docs](https://www.rubydoc.info/gems/strings-inflection) |
|
381
|
+
| [strings-numeral](https://github.com/piotrmurach/strings-numeral) | Express numbers as word numerals. | [docs](https://www.rubydoc.info/gems/strings-numeral) |
|
382
|
+
| [strings-truncation](https://github.com/piotrmurach/strings-truncation) | Truncate strings with fullwidth characters and ANSI codes. | [docs](https://www.rubydoc.info/gems/strings-truncation) |
|
378
383
|
|
379
384
|
## Development
|
380
385
|
|
@@ -386,7 +391,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
386
391
|
|
387
392
|
Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/strings. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
388
393
|
|
389
|
-
1. Fork it ( https://github.com/piotrmurach/
|
394
|
+
1. Fork it ( https://github.com/piotrmurach/strings/fork )
|
390
395
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
391
396
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
392
397
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/strings.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "strings-ansi"
|
4
4
|
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
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.
|
@@ -15,8 +15,8 @@ module Strings
|
|
15
15
|
# @see Strings::Align#align
|
16
16
|
#
|
17
17
|
# @api public
|
18
|
-
def align(*args)
|
19
|
-
Align.align(*args)
|
18
|
+
def align(*args, **kws)
|
19
|
+
Align.align(*args, **kws)
|
20
20
|
end
|
21
21
|
module_function :align
|
22
22
|
|
data/lib/strings/align.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
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"
|
10
|
-
|
11
|
-
|
9
|
+
NEWLINE = "\n"
|
10
|
+
SPACE = " "
|
11
|
+
LINE_BREAK = %r{\r\n|\r|\n}.freeze
|
12
12
|
|
13
13
|
# Aligns text within the width.
|
14
14
|
#
|
@@ -23,23 +23,24 @@ module Strings
|
|
23
23
|
# @example
|
24
24
|
# text = "the madness of men"
|
25
25
|
#
|
26
|
-
# Strings::Align.align(22, :left)
|
26
|
+
# Strings::Align.align(text, 22, direction: :left)
|
27
27
|
# # => "the madness of men "
|
28
28
|
#
|
29
|
-
# Strings::Align.align(22, :center)
|
29
|
+
# Strings::Align.align(text, 22, direction: :center)
|
30
30
|
# # => " the madness of men "
|
31
31
|
#
|
32
|
-
# Strings::Align(22, :right)
|
32
|
+
# Strings::Align(text, 22, direction: :right)
|
33
33
|
# # => " the madness of men"
|
34
34
|
#
|
35
|
-
# Strings::Align.align(22, :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
|
-
send(method, text, width, options)
|
43
|
+
send(method, text, width, **options)
|
43
44
|
end
|
44
45
|
module_function :align
|
45
46
|
|
@@ -62,9 +63,11 @@ module Strings
|
|
62
63
|
# @return [String]
|
63
64
|
#
|
64
65
|
# @api public
|
65
|
-
def align_left(text, width, fill: SPACE, separator:
|
66
|
+
def align_left(text, width, fill: SPACE, separator: nil)
|
66
67
|
return if width.nil?
|
67
|
-
|
68
|
+
sep = separator || text[LINE_BREAK] || NEWLINE
|
69
|
+
|
70
|
+
each_line(text, sep) do |line|
|
68
71
|
width_diff = width - display_width(line)
|
69
72
|
if width_diff > 0
|
70
73
|
line + fill * width_diff
|
@@ -80,9 +83,11 @@ module Strings
|
|
80
83
|
# @return [String]
|
81
84
|
#
|
82
85
|
# @api public
|
83
|
-
def align_center(text, width, fill: SPACE, separator:
|
86
|
+
def align_center(text, width, fill: SPACE, separator: nil)
|
84
87
|
return text if width.nil?
|
85
|
-
|
88
|
+
sep = separator || text[LINE_BREAK] || NEWLINE
|
89
|
+
|
90
|
+
each_line(text, sep) do |line|
|
86
91
|
width_diff = width - display_width(line)
|
87
92
|
if width_diff > 0
|
88
93
|
right_count = (width_diff.to_f / 2).ceil
|
@@ -100,9 +105,11 @@ module Strings
|
|
100
105
|
# @return [String]
|
101
106
|
#
|
102
107
|
# @api public
|
103
|
-
def align_right(text, width, fill: SPACE, separator:
|
108
|
+
def align_right(text, width, fill: SPACE, separator: nil)
|
104
109
|
return text if width.nil?
|
105
|
-
|
110
|
+
sep = separator || text[LINE_BREAK] || NEWLINE
|
111
|
+
|
112
|
+
each_line(text, sep) do |line|
|
106
113
|
width_diff = width - display_width(line)
|
107
114
|
if width_diff > 0
|
108
115
|
fill * width_diff + line
|
data/lib/strings/extensions.rb
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "../strings"
|
4
4
|
|
5
5
|
module Strings
|
6
6
|
module Extensions
|
7
7
|
refine String do
|
8
|
-
def align(*args)
|
9
|
-
Align.align(self, *args)
|
8
|
+
def align(*args, **kws)
|
9
|
+
Align.align(self, *args, **kws)
|
10
10
|
end
|
11
11
|
|
12
|
-
def align_left(*args)
|
13
|
-
Align.align_left(self, *args)
|
12
|
+
def align_left(*args, **kws)
|
13
|
+
Align.align_left(self, *args, **kws)
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
Align.
|
16
|
+
def align_center(*args, **kws)
|
17
|
+
Align.align_center(self, *args, **kws)
|
18
|
+
end
|
19
|
+
|
20
|
+
def align_right(*args, **kws)
|
21
|
+
Align.align_right(self, *args, **kws)
|
18
22
|
end
|
19
23
|
|
20
24
|
def ansi?
|
@@ -25,8 +29,8 @@ module Strings
|
|
25
29
|
Fold.fold(self, *args)
|
26
30
|
end
|
27
31
|
|
28
|
-
def pad(*args)
|
29
|
-
Pad.pad(self, *args)
|
32
|
+
def pad(*args, **kws)
|
33
|
+
Pad.pad(self, *args, **kws)
|
30
34
|
end
|
31
35
|
|
32
36
|
def sanitize
|
@@ -37,8 +41,8 @@ module Strings
|
|
37
41
|
Truncate.truncate(self, *args)
|
38
42
|
end
|
39
43
|
|
40
|
-
def wrap(*args)
|
41
|
-
Wrap.wrap(self, *args)
|
44
|
+
def wrap(*args, **kws)
|
45
|
+
Wrap.wrap(self, *args, **kws)
|
42
46
|
end
|
43
47
|
end
|
44
48
|
end # Extensions
|
data/lib/strings/fold.rb
CHANGED
@@ -7,7 +7,7 @@ module Strings
|
|
7
7
|
# Fold a multiline text into a single line string
|
8
8
|
#
|
9
9
|
# @example
|
10
|
-
# fold("\tfoo \r\n\n bar") # => "foo bar"
|
10
|
+
# fold("\tfoo \r\n\n bar") # => " foo bar"
|
11
11
|
#
|
12
12
|
# @param [String] text
|
13
13
|
#
|
@@ -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
|
data/lib/strings/pad.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "strings-ansi"
|
4
|
+
require "unicode/display_width"
|
5
5
|
|
6
|
-
require_relative
|
6
|
+
require_relative "padder"
|
7
7
|
|
8
8
|
module Strings
|
9
9
|
# Responsible for text padding
|
10
10
|
module Pad
|
11
|
-
NEWLINE = "\n"
|
12
|
-
|
13
|
-
|
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
|
#
|
@@ -31,10 +31,11 @@ module Strings
|
|
31
31
|
# @return [String]
|
32
32
|
#
|
33
33
|
# @api private
|
34
|
-
def pad(text, padding, fill: SPACE, separator:
|
34
|
+
def pad(text, padding, fill: SPACE, separator: nil)
|
35
35
|
padding = Strings::Padder.parse(padding)
|
36
36
|
text_copy = text.dup
|
37
|
-
|
37
|
+
sep = separator || text[LINE_BREAK] || NEWLINE
|
38
|
+
line_width = max_line_length(text, sep)
|
38
39
|
output = []
|
39
40
|
|
40
41
|
filler_line = fill * line_width
|
@@ -43,7 +44,8 @@ module Strings
|
|
43
44
|
output << pad_around(filler_line, padding, fill: fill)
|
44
45
|
end
|
45
46
|
|
46
|
-
text_copy.split(
|
47
|
+
text_copy.split(sep).each do |line|
|
48
|
+
line = line.empty? ? filler_line : line
|
47
49
|
output << pad_around(line, padding, fill: fill)
|
48
50
|
end
|
49
51
|
|
@@ -51,7 +53,7 @@ module Strings
|
|
51
53
|
output << pad_around(filler_line, padding, fill: fill)
|
52
54
|
end
|
53
55
|
|
54
|
-
output.join(
|
56
|
+
output.join(sep)
|
55
57
|
end
|
56
58
|
module_function :pad
|
57
59
|
|
@@ -77,7 +79,7 @@ module Strings
|
|
77
79
|
# @api private
|
78
80
|
def max_line_length(text, separator)
|
79
81
|
lines = text.split(separator, -1)
|
80
|
-
display_width(lines.max_by { |line| display_width(line) } ||
|
82
|
+
display_width(lines.max_by { |line| display_width(line) } || "")
|
81
83
|
end
|
82
84
|
module_function :max_line_length
|
83
85
|
|
data/lib/strings/padder.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
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,
|
48
|
+
raise ParseError, "Wrong :padding parameter, must be an array"
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -72,7 +72,7 @@ module Strings
|
|
72
72
|
|
73
73
|
# Set top padding
|
74
74
|
#
|
75
|
-
# @param [Integer]
|
75
|
+
# @param [Integer] value
|
76
76
|
#
|
77
77
|
# @return [nil]
|
78
78
|
#
|
@@ -92,7 +92,7 @@ module Strings
|
|
92
92
|
|
93
93
|
# Set right padding
|
94
94
|
#
|
95
|
-
# @param [Integer]
|
95
|
+
# @param [Integer] value
|
96
96
|
#
|
97
97
|
# @api public
|
98
98
|
def right=(value)
|