strings 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +4 -2
- data/lib/strings.rb +7 -7
- data/lib/strings/align.rb +6 -5
- data/lib/strings/extensions.rb +1 -1
- data/lib/strings/fold.rb +1 -1
- data/lib/strings/pad.rb +7 -7
- data/lib/strings/padder.rb +2 -2
- data/lib/strings/truncate.rb +6 -6
- data/lib/strings/version.rb +1 -1
- data/lib/strings/wrap.rb +24 -23
- metadata +15 -51
- data/Rakefile +0 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/spec/spec_helper.rb +0 -39
- data/spec/unit/align/align_left_spec.rb +0 -71
- data/spec/unit/align/align_right_spec.rb +0 -71
- data/spec/unit/align/align_spec.rb +0 -95
- data/spec/unit/align_spec.rb +0 -18
- data/spec/unit/ansi_spec.rb +0 -7
- data/spec/unit/extensions_spec.rb +0 -77
- 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 -74
- 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 -74
- data/spec/unit/truncate_spec.rb +0 -8
- data/spec/unit/wrap/insert_ansi_spec.rb +0 -65
- data/spec/unit/wrap/wrap_spec.rb +0 -207
- data/spec/unit/wrap_spec.rb +0 -15
- data/strings.gemspec +0 -34
- data/tasks/console.rake +0 -9
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
data/Rakefile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "strings"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
if ENV['COVERAGE'] || ENV['TRAVIS']
|
4
|
-
require 'simplecov'
|
5
|
-
require 'coveralls'
|
6
|
-
|
7
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
8
|
-
SimpleCov::Formatter::HTMLFormatter,
|
9
|
-
Coveralls::SimpleCov::Formatter
|
10
|
-
])
|
11
|
-
|
12
|
-
SimpleCov.start do
|
13
|
-
command_name 'spec'
|
14
|
-
add_filter 'spec'
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
require "bundler/setup"
|
19
|
-
require "strings"
|
20
|
-
|
21
|
-
module Helpers
|
22
|
-
def unindent(text)
|
23
|
-
text.gsub(/^[ \t]*/, '').chomp
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
RSpec.configure do |config|
|
28
|
-
config.include(Helpers)
|
29
|
-
|
30
|
-
# Enable flags like --only-failures and --next-failure
|
31
|
-
config.example_status_persistence_file_path = ".rspec_status"
|
32
|
-
|
33
|
-
# Disable RSpec exposing methods globally on `Module` and `main`
|
34
|
-
config.disable_monkey_patching!
|
35
|
-
|
36
|
-
config.expect_with :rspec do |c|
|
37
|
-
c.syntax = :expect
|
38
|
-
end
|
39
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Align, '#align_left' do
|
4
|
-
it "aligns line to left" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect(Strings::Align.align_left(text, 22)).to eq("the madness of men ")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "fills empty" do
|
10
|
-
expect(Strings::Align.align_left('', 22)).to eq(" ")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "left justifies string with unicode characters" do
|
14
|
-
text = "こんにちは"
|
15
|
-
expect(Strings::Align.align(text, 20, direction: :left)).to eq("こんにちは ")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "left justifies string with ansi codes" do
|
19
|
-
text = "\e[32mthe madness of men\e[0m"
|
20
|
-
expect(Strings::Align.align(text, 22, direction: :left)).to eq("\e[32mthe madness of men\e[0m ")
|
21
|
-
end
|
22
|
-
|
23
|
-
it "aligns multiline text to left" do
|
24
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
25
|
-
expect(Strings::Align.align_left(text, 40)).to eq([
|
26
|
-
"for there is no folly of the beast \n",
|
27
|
-
"of the earth which \n",
|
28
|
-
"is not infinitely \n",
|
29
|
-
"outdone by the madness of men "
|
30
|
-
].join)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "left justifies multiline utf text" do
|
34
|
-
text = "ラドクリフ\n、マラソン五輪\n代表に1万m出\n場にも含み"
|
35
|
-
expect(Strings::Align.align_left(text, 20)).to eq([
|
36
|
-
"ラドクリフ \n",
|
37
|
-
"、マラソン五輪 \n",
|
38
|
-
"代表に1万m出 \n",
|
39
|
-
"場にも含み "
|
40
|
-
].join)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "left justifies ansi text" do
|
44
|
-
text = "for \e[35mthere\e[0m is no folly of the beast\nof the \e[33mearth\e0m which\nis \e[34mnot infinitely\e[0m\n\e[33moutdone\e[0m by the madness of men"
|
45
|
-
expect(Strings::Align.align_left(text, 40)).to eq([
|
46
|
-
"for \e[35mthere\e[0m is no folly of the beast \n",
|
47
|
-
"of the \e[33mearth\e0m which \n",
|
48
|
-
"is \e[34mnot infinitely\e[0m \n",
|
49
|
-
"\e[33moutdone\e[0m by the madness of men "
|
50
|
-
].join)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "left justifies multiline text with fill of '*'" do
|
54
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
55
|
-
expect(Strings::Align.align_left(text, 40, fill: '*')).to eq([
|
56
|
-
"for there is no folly of the beast******\n",
|
57
|
-
"of the earth which**********************\n",
|
58
|
-
"is not infinitely***********************\n",
|
59
|
-
"outdone by the madness of men***********"
|
60
|
-
].join)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "handles \r\n line separator" do
|
64
|
-
text = "Closes #360\r\n\r\nCloses !217"
|
65
|
-
expect(Strings::Align.align_left(text, 27)).to eq([
|
66
|
-
"Closes #360 ",
|
67
|
-
" ",
|
68
|
-
"Closes !217 "
|
69
|
-
].join("\r\n"))
|
70
|
-
end
|
71
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Align, '#align_right' do
|
4
|
-
it "aligns line to right" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect(Strings::Align.align_right(text, 22)).to eq(" the madness of men")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "fills empty" do
|
10
|
-
expect(Strings::Align.align_right('', 22)).to eq(" ")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "aligns string to the right with unicode characters" do
|
14
|
-
text = "こんにちは"
|
15
|
-
expect(Strings::Align.align(text, 20, direction: :right)).to eq(" こんにちは")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "aligns string to the right with ansi codees" do
|
19
|
-
text = "\e[32mthe madness of men\e[0m"
|
20
|
-
expect(Strings::Align.align(text, 22, direction: :right)).to eq(" \e[32mthe madness of men\e[0m")
|
21
|
-
end
|
22
|
-
|
23
|
-
it "aligns multiline text to the right" do
|
24
|
-
text = "for there is no folly of the beast\n of the earth which\n is not infinitely\n outdone by the madness of men"
|
25
|
-
expect(Strings::Align.align_right(text, 40)).to eq([
|
26
|
-
" for there is no folly of the beast\n",
|
27
|
-
" of the earth which\n",
|
28
|
-
" is not infinitely\n",
|
29
|
-
" outdone by the madness of men"
|
30
|
-
].join)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "aligns multiline text to right with unicode characters" do
|
34
|
-
text = "ラドクリフ\n、マラソン五輪\n代表に1万m出\n場にも含み"
|
35
|
-
expect(Strings::Align.align_right(text, 20)).to eq([
|
36
|
-
" ラドクリフ\n",
|
37
|
-
" 、マラソン五輪\n",
|
38
|
-
" 代表に1万m出\n",
|
39
|
-
" 場にも含み"
|
40
|
-
].join)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "right justfies ansi text" do
|
44
|
-
text = "for \e[35mthere\e[0m is no folly of the beast\nof the \e[33mearth\e0m which\nis \e[34mnot infinitely\e[0m\n\e[33moutdone\e[0m by the madness of men"
|
45
|
-
expect(Strings::Align.align_right(text, 40)).to eq([
|
46
|
-
" for \e[35mthere\e[0m is no folly of the beast\n",
|
47
|
-
" of the \e[33mearth\e0m which\n",
|
48
|
-
" is \e[34mnot infinitely\e[0m\n",
|
49
|
-
" \e[33moutdone\e[0m by the madness of men"
|
50
|
-
].join)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "right justifies multiline text with fill of '*'" do
|
54
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
55
|
-
expect(Strings::Align.align_right(text, 40, fill: '*')).to eq([
|
56
|
-
"******for there is no folly of the beast\n",
|
57
|
-
"**********************of the earth which\n",
|
58
|
-
"***********************is not infinitely\n",
|
59
|
-
"***********outdone by the madness of men"
|
60
|
-
].join)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "handles \r\n line separator" do
|
64
|
-
text = "Closes #360\r\n\r\nCloses !217"
|
65
|
-
expect(Strings::Align.align_right(text, 27)).to eq([
|
66
|
-
" Closes #360",
|
67
|
-
" ",
|
68
|
-
" Closes !217"
|
69
|
-
].join("\r\n"))
|
70
|
-
end
|
71
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Align, '#align' do
|
4
|
-
it "doesn't align unrecognized direction" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect {
|
7
|
-
Strings::Align.align(text, 22, direction: :unknown)
|
8
|
-
}.to raise_error(ArgumentError, /Unknown alignment/)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "fills empty" do
|
12
|
-
expect(Strings::Align.align('', 22, direction: :center)).to eq(" ")
|
13
|
-
end
|
14
|
-
|
15
|
-
it "centers line" do
|
16
|
-
text = "the madness of men"
|
17
|
-
expect(Strings::Align.align_center(text, 22)).to eq(" the madness of men ")
|
18
|
-
end
|
19
|
-
|
20
|
-
it "centers unicode characters" do
|
21
|
-
text = "こんにちは"
|
22
|
-
expect(Strings::Align.align_center(text, 20)).to eq(" こんにちは ")
|
23
|
-
end
|
24
|
-
|
25
|
-
it "centers string with ansi codes" do
|
26
|
-
text = "\e[32mthe madness of men\e[0m"
|
27
|
-
expect(Strings::Align.align_center(text, 22)).to eq(" \e[32mthe madness of men\e[0m ")
|
28
|
-
end
|
29
|
-
|
30
|
-
it "centers multiline text" do
|
31
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
32
|
-
expect(Strings::Align.align_center(text, 40)).to eq([
|
33
|
-
" for there is no folly of the beast \n",
|
34
|
-
" of the earth which \n",
|
35
|
-
" is not infinitely \n",
|
36
|
-
" outdone by the madness of men "
|
37
|
-
].join)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "centers multiline text with exact width" do
|
41
|
-
text = "the madness \nof men"
|
42
|
-
expect(Strings::Align.align_center(text, 12)).to eq([
|
43
|
-
"the madness \n",
|
44
|
-
" of men "
|
45
|
-
].join)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "centers multiline unicode text" do
|
49
|
-
text = "ラドクリフ\n、マラソン五輪\n代表に1万m出\n場にも含み"
|
50
|
-
expect(Strings::Align.align_center(text, 20)).to eq([
|
51
|
-
" ラドクリフ \n",
|
52
|
-
" 、マラソン五輪 \n",
|
53
|
-
" 代表に1万m出 \n",
|
54
|
-
" 場にも含み "
|
55
|
-
].join)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "centers text with ansi codes" do
|
59
|
-
text = "for \e[35mthere\e[0m is no folly of the beast\nof the \e[33mearth\e0m which\nis \e[34mnot infinitely\e[0m\n\e[33moutdone\e[0m by the madness of men"
|
60
|
-
expect(Strings::Align.align_center(text, 40)).to eq([
|
61
|
-
" for \e[35mthere\e[0m is no folly of the beast \n",
|
62
|
-
" of the \e[33mearth\e0m which \n",
|
63
|
-
" is \e[34mnot infinitely\e[0m \n",
|
64
|
-
" \e[33moutdone\e[0m by the madness of men "
|
65
|
-
].join)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "centers multiline text with fill character '*'" do
|
69
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
70
|
-
expect(Strings::Align.align(text, 40, direction: :center, fill: '*')).to eq([
|
71
|
-
"***for there is no folly of the beast***\n",
|
72
|
-
"***********of the earth which***********\n",
|
73
|
-
"***********is not infinitely************\n",
|
74
|
-
"*****outdone by the madness of men******"
|
75
|
-
].join)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "handles \r\n line separator" do
|
79
|
-
text = "Closes #360\r\n\r\nCloses !217"
|
80
|
-
expect(Strings::Align.align(text, 27)).to eq([
|
81
|
-
"Closes #360 ",
|
82
|
-
" ",
|
83
|
-
"Closes !217 "
|
84
|
-
].join("\r\n"))
|
85
|
-
end
|
86
|
-
|
87
|
-
it "handles \r\n line separator and centers" do
|
88
|
-
text = "Closes #360\r\n\r\nCloses !217"
|
89
|
-
expect(Strings::Align.align_center(text, 27)).to eq([
|
90
|
-
" Closes #360 ",
|
91
|
-
" ",
|
92
|
-
" Closes !217 "
|
93
|
-
].join("\r\n"))
|
94
|
-
end
|
95
|
-
end
|
data/spec/unit/align_spec.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings, '#align' do
|
4
|
-
it "aligns text" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect(Strings.align(text, 22, direction: :center)).to eq(" the madness of men ")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "aligns text to left" do
|
10
|
-
text = "the madness of men"
|
11
|
-
expect(Strings.align_left(text, 22)).to eq("the madness of men ")
|
12
|
-
end
|
13
|
-
|
14
|
-
it "aligns text to right" do
|
15
|
-
text = "the madness of men"
|
16
|
-
expect(Strings.align_right(text, 22)).to eq(" the madness of men")
|
17
|
-
end
|
18
|
-
end
|
data/spec/unit/ansi_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'strings/extensions'
|
4
|
-
|
5
|
-
using Strings::Extensions
|
6
|
-
|
7
|
-
RSpec.describe Strings::Extensions do
|
8
|
-
let(:text) { "the madness of men" }
|
9
|
-
|
10
|
-
it "aligns a line in the center" do
|
11
|
-
expect(text.align(22, direction: :center)).to eq(" the madness of men ")
|
12
|
-
end
|
13
|
-
|
14
|
-
it "aligns a line to the left" do
|
15
|
-
expect(text.align_left(22)).to eq("the madness of men ")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "aligns a line to the right" do
|
19
|
-
expect(text.align_right(22)).to eq(" the madness of men")
|
20
|
-
end
|
21
|
-
|
22
|
-
it "checks for ansi" do
|
23
|
-
expect("\e[33mfoo\e[0m".ansi?).to eq(true)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "removes ansi codes" do
|
27
|
-
expect("\e[33mfoo\e[0m".sanitize).to eq('foo')
|
28
|
-
end
|
29
|
-
|
30
|
-
it "folds a line" do
|
31
|
-
expect("foo\n\nbar\n".fold).to eq('foo bar ')
|
32
|
-
end
|
33
|
-
|
34
|
-
it "pads a line" do
|
35
|
-
expect(text.pad(1)).to eq([
|
36
|
-
" " * (text.size + 2),
|
37
|
-
" #{text} ",
|
38
|
-
" " * (text.size + 2),
|
39
|
-
].join("\n"))
|
40
|
-
end
|
41
|
-
|
42
|
-
it "pads a line" do
|
43
|
-
expect(text.pad(1)).to eq([
|
44
|
-
" " * (text.size + 2),
|
45
|
-
" #{text} ",
|
46
|
-
" " * (text.size + 2),
|
47
|
-
].join("\n"))
|
48
|
-
end
|
49
|
-
|
50
|
-
it "pads a line with custom fill" do
|
51
|
-
expect(text.pad(1, fill: "*")).to eq([
|
52
|
-
"*" * (text.size + 2),
|
53
|
-
"*#{text}*",
|
54
|
-
"*" * (text.size + 2),
|
55
|
-
].join("\n"))
|
56
|
-
end
|
57
|
-
|
58
|
-
it "truncates a line" do
|
59
|
-
text = "the madness of men"
|
60
|
-
expect(text.truncate(10)).to eq("the madn…")
|
61
|
-
end
|
62
|
-
|
63
|
-
it "truncates a line with a custom trailing" do
|
64
|
-
text = "the madness of men"
|
65
|
-
expect(text.truncate(10, trailing: "...")).to eq("the ma...")
|
66
|
-
end
|
67
|
-
|
68
|
-
it "truncates into words with a custom trailing" do
|
69
|
-
text = "the madness of men"
|
70
|
-
expect(text.truncate(16, trailing: "...", separator: " ")).to eq("the madness...")
|
71
|
-
end
|
72
|
-
|
73
|
-
it "wraps a line" do
|
74
|
-
expect('foobar1'.wrap(3)).to eq("foo\nbar\n1")
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
data/spec/unit/fold/fold_spec.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# unicode: utf-8
|
2
|
-
# frozen_string_literals: true
|
3
|
-
|
4
|
-
RSpec.describe Strings, '#fold' do
|
5
|
-
{
|
6
|
-
" \n" => ' ',
|
7
|
-
"\n " => ' ',
|
8
|
-
"\n" => ' ',
|
9
|
-
"\n\n\n" => ' ',
|
10
|
-
" \n " => ' ',
|
11
|
-
" \n \n \n" => ' '
|
12
|
-
}.each do |actual, expected|
|
13
|
-
it "removes newline '#{actual.gsub(/\n/, '\\n')}' to '#{expected}'" do
|
14
|
-
expect(Strings::Fold.fold(actual)).to eq(expected)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
{
|
19
|
-
" \r\n" => ' ',
|
20
|
-
"\r\n " => ' ',
|
21
|
-
"\r\n" => ' ',
|
22
|
-
" \r\n " => ' ',
|
23
|
-
}.each do |actual, expected|
|
24
|
-
it "squashes '#{actual.gsub(/\r\n/, '\\r\\n')}' to '#{expected}'" do
|
25
|
-
expect(Strings::Fold.fold(actual, "\r\n")).to eq(expected)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|