string-text 0.4.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: bbebbdbd77112e3f5322cd1bc2e1296c06cc0e4106dbb2b59cfa71e41e869f93
4
- data.tar.gz: 883ad7d37349a4a0b7851006d19292e46c4a9628a63daba9011fd8283b782bde
3
+ metadata.gz: dc708ad8db9f529e578b50865ace1ac3fdeb1f8ec756039e9db10dcbefb45801
4
+ data.tar.gz: eedbc4c94d154a5dd6c8bed18f954409a2c89165356625aec07f68883b6ac56e
5
5
  SHA512:
6
- metadata.gz: 37879cfee303951695df37ba4a110a3020a3d8b769fb0e1ed48607aea1912d8c1ef4085bed452b881422e2a687e2e46a050f33bb8adc577a3eaaeafdbd1b8f45
7
- data.tar.gz: f32f7e8b5146b90f763e8a68301be62d37354f640b6882364334ba64d741f56ee2eac35cb78a37e8b319cf2d679c94ec90fa0bb0a16d1d4ed3381556f4eb27d2
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.4.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,22 +20,51 @@ module String::Text
19
20
  # This line will start at column 3
20
21
  # ).align
21
22
  #
22
- def align(column = 1)
23
+ # Because unindented lines are realigned to the previous line's indent,
24
+ # lists can be indented like this
25
+ #
26
+ # words = %w(hello world)
27
+ # puts %(
28
+ # Array elements on separate lines and starting at column 3
29
+ # #{words.join("\n")}
30
+ # ).align
31
+ #
32
+ # If :bol is false then the first line won't be indented or outdented
33
+ #
34
+ def align(column = 1, bol: true)
23
35
  column > 0 or raise ArgumentError "Illegal column: #{column}"
24
36
  initial = " " * (column - 1)
25
- lines = self.split(/\n/)
37
+
38
+ # Remove initial and final empty lines
39
+ lines = self.split(/\n/).map &:rstrip
26
40
  lines.pop while !lines.empty? && !(lines.last =~ /^\s*\S/)
27
41
  lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
28
42
  return "" if lines.empty?
29
- indent = lines.map { _1 =~ /^(\s*)/; $1.size }.select { _1 > 0 }.min || 0
30
- lines.map { |line|
31
- l = line[indent..-1]&.rstrip
32
- l ? initial + l : ""
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
+
48
+ first = true
49
+ lines.map.with_index { |line, i|
50
+ if !bol && first
51
+ first = false
52
+ line[indents[0]..-1]
53
+ else
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
62
+ end
33
63
  }.join("\n")
34
64
  end
35
65
 
36
66
  # Like #align but replaces the string
37
- def align!(column = 1) = self.replace align(column)
67
+ def align!(column = 1, bol: true) = self.replace align(column, bol: bol)
38
68
 
39
69
  # Converts a string to a boolean so that "true" becomes true and that
40
70
  # "false" and the empty string becomes false. Any other string is an error
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string-text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-09-29 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Gem string-text
14
13
  email:
@@ -30,7 +29,6 @@ homepage: http://www.nowhere.com/
30
29
  licenses: []
31
30
  metadata:
32
31
  homepage_uri: http://www.nowhere.com/
33
- post_install_message:
34
32
  rdoc_options: []
35
33
  require_paths:
36
34
  - lib
@@ -45,8 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
43
  - !ruby/object:Gem::Version
46
44
  version: '0'
47
45
  requirements: []
48
- rubygems_version: 3.3.7
49
- signing_key:
46
+ rubygems_version: 3.6.9
50
47
  specification_version: 4
51
48
  summary: Text formatting methods
52
49
  test_files: []