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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9cb6091be55dccdd85e5ed00b7348573a768833a782725e00a0aa6258506c31b
4
- data.tar.gz: 5faa4885c3a303f1eed4ee21ad30a80b779701df7be15be9fda079ea353d9c3b
3
+ metadata.gz: dc708ad8db9f529e578b50865ace1ac3fdeb1f8ec756039e9db10dcbefb45801
4
+ data.tar.gz: eedbc4c94d154a5dd6c8bed18f954409a2c89165356625aec07f68883b6ac56e
5
5
  SHA512:
6
- metadata.gz: ee4799f3ac56b221c362e00c6627282aa03dcf294af5ce88422e3134c60fa96e5d3d25eb2bbbe09ad5720a7890e205beb7bfb34c91efb22122f4e64eeead656b
7
- data.tar.gz: 8ebb89255417583f8dc30ec33ca08db50d2ea88a748c186cf95d6bf3df511eb6a6ae84d1502c077277c7585a17f569333b855210b054ba503df7e3d67a7fcebe
6
+ metadata.gz: c91f32af2d66cbb39793bf7c05209bee216dbcc5836e8e76ca734a0e9d44c76aa9e14b03df60fdcb23f17f6246b0fd5f05718177a902a94c7ef2f6be3f206f38
7
+ data.tar.gz: 48f6a28ebb4cd562874d7890adad0d0df28f419085fe1a1b2ba90844ddea977cfe25e3d7be2f6d8a3c627cf8cd8207379d69c43fbe1316877e4bbd00457cf46a
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module String::Text
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
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
- # whole block that is then aligned as a whole (including internal indents)
12
- # to the given column. Initial and final empty lines are ignored
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
- # If :bol is false then the first line won't be indented or outdented. It
23
- # is used when including a block of text in another block:
23
+ # Because unindented lines are realigned to the previous line's indent,
24
+ # lists can be indented like this
24
25
  #
25
- # innert_text = %(
26
- # first line
27
- # second line
28
- # )
26
+ # words = %w(hello world)
29
27
  # puts %(
30
- # Here is the outer text
31
- # #{inner_text.align(2, bol: false)}
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
- lines = self.split(/\n/)
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
- indent = lines.map { _1 =~ /^(\s*)/; $1.size }.select { _1 > 0 }.min || 0
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
- l || ""
52
+ line[indents[0]..-1]
50
53
  else
51
- l ? initial + l : ""
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string-text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen