string-text 0.3.0 → 0.5.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: a0aca29ea9d4de27eee11ec5fcbc58a7a0daad9fb2ed31507bb919b1bca3b9e2
4
- data.tar.gz: a01a8670c95fca9090d484a6defd0fed7b02dc81c8175d10450f6dff03be6c3e
3
+ metadata.gz: 9cb6091be55dccdd85e5ed00b7348573a768833a782725e00a0aa6258506c31b
4
+ data.tar.gz: 5faa4885c3a303f1eed4ee21ad30a80b779701df7be15be9fda079ea353d9c3b
5
5
  SHA512:
6
- metadata.gz: 5f9ec6576c3c0ecd16637cc8e7f9e806d08c8478480f87812a362831677cd7abc1287d979d200e010d7b9ff2f9954495e279436482362eaff1c1f703ccb27e2d
7
- data.tar.gz: d441c56ceefd09be12f512c34e72cb15787719a25107e3aef186206eb8d8fa1962884bc9549de6fa16b6bc4e85be151ee5fc5c5c07e92808c2dcbcc04b725548
6
+ metadata.gz: ee4799f3ac56b221c362e00c6627282aa03dcf294af5ce88422e3134c60fa96e5d3d25eb2bbbe09ad5720a7890e205beb7bfb34c91efb22122f4e64eeead656b
7
+ data.tar.gz: 8ebb89255417583f8dc30ec33ca08db50d2ea88a748c186cf95d6bf3df511eb6a6ae84d1502c077277c7585a17f569333b855210b054ba503df7e3d67a7fcebe
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module String::Text
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/string-text.rb CHANGED
@@ -19,18 +19,42 @@ module String::Text
19
19
  # This line will start at column 3
20
20
  # ).align
21
21
  #
22
- def align(column = 1)
23
- column == 1 or raise NotImplementedError
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:
24
+ #
25
+ # innert_text = %(
26
+ # first line
27
+ # second line
28
+ # )
29
+ # puts %(
30
+ # Here is the outer text
31
+ # #{inner_text.align(2, bol: false)}
32
+ # Rest of the outer text
33
+ # ).align
34
+ #
35
+ def align(column = 1, bol: true)
36
+ column > 0 or raise ArgumentError "Illegal column: #{column}"
37
+ initial = " " * (column - 1)
24
38
  lines = self.split(/\n/)
25
39
  lines.pop while !lines.empty? && !(lines.last =~ /^\s*\S/)
26
40
  lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
27
41
  return "" if lines.empty?
42
+
28
43
  indent = lines.map { _1 =~ /^(\s*)/; $1.size }.select { _1 > 0 }.min || 0
29
- lines.map { |line| line[indent..-1]&.rstrip || "" }.join("\n")
44
+ first = true
45
+ lines.map { |line|
46
+ l = line[indent..-1]&.rstrip
47
+ if !bol && first
48
+ first = false
49
+ l || ""
50
+ else
51
+ l ? initial + l : ""
52
+ end
53
+ }.join("\n")
30
54
  end
31
55
 
32
56
  # Like #align but replaces the string
33
- def align!(column = 1) = self.replace align(column)
57
+ def align!(column = 1, bol: true) = self.replace align(column, bol: bol)
34
58
 
35
59
  # Converts a string to a boolean so that "true" becomes true and that
36
60
  # "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.3.0
4
+ version: 0.5.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-22 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: []