string-text 0.5.0 → 0.6.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 +4 -4
- data/lib/string-text/version.rb +1 -1
- data/lib/string-text.rb +27 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc708ad8db9f529e578b50865ace1ac3fdeb1f8ec756039e9db10dcbefb45801
|
4
|
+
data.tar.gz: eedbc4c94d154a5dd6c8bed18f954409a2c89165356625aec07f68883b6ac56e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c91f32af2d66cbb39793bf7c05209bee216dbcc5836e8e76ca734a0e9d44c76aa9e14b03df60fdcb23f17f6246b0fd5f05718177a902a94c7ef2f6be3f206f38
|
7
|
+
data.tar.gz: 48f6a28ebb4cd562874d7890adad0d0df28f419085fe1a1b2ba90844ddea977cfe25e3d7be2f6d8a3c627cf8cd8207379d69c43fbe1316877e4bbd00457cf46a
|
data/lib/string-text/version.rb
CHANGED
data/lib/string-text.rb
CHANGED
@@ -8,8 +8,9 @@ module String::Text
|
|
8
8
|
refine String do
|
9
9
|
# Indent or outdent a block of text to the given column (default 1). It
|
10
10
|
# uses the indent of the least indented non-empty line as the indent of the
|
11
|
-
#
|
12
|
-
#
|
11
|
+
# block that is then aligned as a whole to the given column. Lines starting
|
12
|
+
# at column one are realigned to the indent of the previous line. Initial
|
13
|
+
# and final empty lines are ignored.
|
13
14
|
#
|
14
15
|
# #align is often handy when you call methods with a %(...) argument
|
15
16
|
# and don't want weird indentation in your output
|
@@ -19,36 +20,45 @@ module String::Text
|
|
19
20
|
# This line will start at column 3
|
20
21
|
# ).align
|
21
22
|
#
|
22
|
-
#
|
23
|
-
#
|
23
|
+
# Because unindented lines are realigned to the previous line's indent,
|
24
|
+
# lists can be indented like this
|
24
25
|
#
|
25
|
-
#
|
26
|
-
# first line
|
27
|
-
# second line
|
28
|
-
# )
|
26
|
+
# words = %w(hello world)
|
29
27
|
# puts %(
|
30
|
-
#
|
31
|
-
# #{
|
32
|
-
# Rest of the outer text
|
28
|
+
# Array elements on separate lines and starting at column 3
|
29
|
+
# #{words.join("\n")}
|
33
30
|
# ).align
|
34
31
|
#
|
32
|
+
# If :bol is false then the first line won't be indented or outdented
|
33
|
+
#
|
35
34
|
def align(column = 1, bol: true)
|
36
35
|
column > 0 or raise ArgumentError "Illegal column: #{column}"
|
37
36
|
initial = " " * (column - 1)
|
38
|
-
|
37
|
+
|
38
|
+
# Remove initial and final empty lines
|
39
|
+
lines = self.split(/\n/).map &:rstrip
|
39
40
|
lines.pop while !lines.empty? && !(lines.last =~ /^\s*\S/)
|
40
41
|
lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
|
41
42
|
return "" if lines.empty?
|
42
43
|
|
43
|
-
|
44
|
+
# Find minimal indent. Ignores lines with indent 0
|
45
|
+
indents = lines.map { _1 =~ /^(\s*)/; $1.size }
|
46
|
+
indent = indents.select { _1 > 0 }.min || 0
|
47
|
+
|
44
48
|
first = true
|
45
|
-
lines.map { |line|
|
46
|
-
l = line[indent..-1]&.rstrip
|
49
|
+
lines.map.with_index { |line, i|
|
47
50
|
if !bol && first
|
48
51
|
first = false
|
49
|
-
|
52
|
+
line[indents[0]..-1]
|
50
53
|
else
|
51
|
-
|
54
|
+
if line.empty?
|
55
|
+
""
|
56
|
+
elsif indents[i] == 0 && i > 0 # Unindented lines
|
57
|
+
indents[i] = indents[i-1] # use previous line's indent
|
58
|
+
' ' * (indents[i] - indent) + line
|
59
|
+
else # Regular line
|
60
|
+
initial + line[indent..-1]
|
61
|
+
end
|
52
62
|
end
|
53
63
|
}.join("\n")
|
54
64
|
end
|