string-text 0.6.0 → 0.6.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/lib/string-text/version.rb +1 -1
- data/lib/string-text.rb +25 -5
- 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: 5f6c8858a8db9e8092584a772dfaf14f37ea992f5281a88419f42873f6ee2557
|
4
|
+
data.tar.gz: 1e0e8b6db1a0b3184f1141dd38891fc2c628b28f53edfedd7013c84657bed6ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2f147ba4df73785325b7f7f316cfc21419b2cd79cb4cf7daa3c58a2f909c5ada95d99c566b5d7759b50d518491b96d1389e2c5fde0e78b584932f6fab2c917b
|
7
|
+
data.tar.gz: dab721a833b6afb441ec26ea881fed1975d65fd33a27843c9dbf236b6d1d2114ecccce1d1b848d01f0e03325352ed0b8819cd3b5612c72f2a4ca8a2a0619e8e2
|
data/lib/string-text/version.rb
CHANGED
data/lib/string-text.rb
CHANGED
@@ -5,13 +5,17 @@ require_relative "string-text/version"
|
|
5
5
|
module String::Text
|
6
6
|
class Error < StandardError; end
|
7
7
|
|
8
|
+
def self.indentation(s) s =~ /^(\s*)/; $1.size end
|
9
|
+
|
8
10
|
refine String do
|
9
11
|
# Indent or outdent a block of text to the given column (default 1). It
|
10
12
|
# uses the indent of the least indented non-empty line as the indent of the
|
11
|
-
# block that is then aligned as a whole to the given column
|
13
|
+
# block that is then aligned as a whole to the given column; lines starting
|
12
14
|
# at column one are realigned to the indent of the previous line. Initial
|
13
15
|
# and final empty lines are ignored.
|
14
16
|
#
|
17
|
+
# FIXME: make the special rule for lines starting at column one an option
|
18
|
+
#
|
15
19
|
# #align is often handy when you call methods with a %(...) argument
|
16
20
|
# and don't want weird indentation in your output
|
17
21
|
#
|
@@ -20,15 +24,28 @@ module String::Text
|
|
20
24
|
# This line will start at column 3
|
21
25
|
# ).align
|
22
26
|
#
|
27
|
+
# This line will start at column 1
|
28
|
+
# This line will start at column 3
|
29
|
+
#
|
23
30
|
# Because unindented lines are realigned to the previous line's indent,
|
24
31
|
# lists can be indented like this
|
25
32
|
#
|
26
33
|
# words = %w(hello world)
|
27
34
|
# puts %(
|
28
|
-
# Array elements on separate lines and starting at column 3
|
35
|
+
# Array elements on separate lines and starting at column 3:
|
29
36
|
# #{words.join("\n")}
|
30
37
|
# ).align
|
31
38
|
#
|
39
|
+
# Array elements on separate lines and starting at column 3:
|
40
|
+
# hello
|
41
|
+
# world
|
42
|
+
#
|
43
|
+
# The trick is that the words expression makes the string expand to
|
44
|
+
#
|
45
|
+
# Array elements on separate lines and starting at column 3:
|
46
|
+
# hello
|
47
|
+
# world
|
48
|
+
#
|
32
49
|
# If :bol is false then the first line won't be indented or outdented
|
33
50
|
#
|
34
51
|
def align(column = 1, bol: true)
|
@@ -41,9 +58,12 @@ module String::Text
|
|
41
58
|
lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
|
42
59
|
return "" if lines.empty?
|
43
60
|
|
44
|
-
#
|
45
|
-
indents = lines.map { _1
|
46
|
-
|
61
|
+
# Identation level of each line
|
62
|
+
indents = lines.map { String::Text.indentation(_1) }
|
63
|
+
|
64
|
+
# Find minimal indent. If the first line is not indented the minimal
|
65
|
+
# indent is 0, otherwise the smallest non-zero indent used
|
66
|
+
indent = String::Text.indentation(lines.first) == 0 ? 0 : indents.select { _1 > 0 }.min || 0
|
47
67
|
|
48
68
|
first = true
|
49
69
|
lines.map.with_index { |line, i|
|