string-text 0.7.0 → 0.8.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 +6 -4
- 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: 5a74a791dce540de25e15aee87d46ffad13b6b681dd900a5abf64c3b18106169
|
|
4
|
+
data.tar.gz: 775033ab62489346e7334c9935ae0555e170c94ff4c94418039b4e0624abe86e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 26f2d2c4d7085fedc90010cab5f1c96b0694b3124c53205e9fcc022a07aa893734aecdb3a5a3330d95282cdb8b8a51990cc0e42e1f9f0c3ad3fae43b8a09e94d
|
|
7
|
+
data.tar.gz: 95827c43612d432f7dd33d594a2201d754f8f3420bdc683e0be923bc72b9637fa9b3b894fde8958729cc827b0a2e2e8a0178d91a74533585f21932cc3a6f95a6
|
data/lib/string-text/version.rb
CHANGED
data/lib/string-text.rb
CHANGED
|
@@ -5,8 +5,6 @@ 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
|
-
|
|
10
8
|
refine String do
|
|
11
9
|
# Indent or outdent a block of text to the given column (default 1). It
|
|
12
10
|
# uses the indent of the least indented non-empty line as the indent of the
|
|
@@ -65,14 +63,15 @@ module String::Text
|
|
|
65
63
|
line = lines.find { |line| line =~ /\S/ }
|
|
66
64
|
end
|
|
67
65
|
return "" if lines.empty?
|
|
66
|
+
return lines.map { "" }.join("\n") if line.nil?
|
|
68
67
|
|
|
69
68
|
# Only align to given column if first line is not indented
|
|
70
|
-
if
|
|
69
|
+
if line.indentation == 0
|
|
71
70
|
return lines.map { |line| ' ' * (column-1) + line }.join("\n")
|
|
72
71
|
end
|
|
73
72
|
|
|
74
73
|
# Identation level of each line
|
|
75
|
-
indents = lines.map
|
|
74
|
+
indents = lines.map(&:indentation)
|
|
76
75
|
|
|
77
76
|
# Find minimal indent, ignoring unindented lines
|
|
78
77
|
indent = indents.select { _1 > 0 }.min || 0
|
|
@@ -98,6 +97,9 @@ module String::Text
|
|
|
98
97
|
# Like #align but replaces the string
|
|
99
98
|
def align!(column = 1, bol: true) = self.replace align(column, bol: bol)
|
|
100
99
|
|
|
100
|
+
# Indent of line
|
|
101
|
+
def indentation = (self[/\A *\S/]&.size || 1) - 1
|
|
102
|
+
|
|
101
103
|
# Converts a string to a boolean so that "true" becomes true and that
|
|
102
104
|
# "false" and the empty string becomes false. Any other string is an error
|
|
103
105
|
#
|