string-text 0.6.0 → 0.6.2

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: dc708ad8db9f529e578b50865ace1ac3fdeb1f8ec756039e9db10dcbefb45801
4
- data.tar.gz: eedbc4c94d154a5dd6c8bed18f954409a2c89165356625aec07f68883b6ac56e
3
+ metadata.gz: 5595a21634c9c54723c5e357e923e9c87c312a98005bb959dc98d8b69ad58480
4
+ data.tar.gz: 8da08b627f45ab3f25dbac74915c3a844b45270c5cfd3b742eb95b0287de0a50
5
5
  SHA512:
6
- metadata.gz: c91f32af2d66cbb39793bf7c05209bee216dbcc5836e8e76ca734a0e9d44c76aa9e14b03df60fdcb23f17f6246b0fd5f05718177a902a94c7ef2f6be3f206f38
7
- data.tar.gz: 48f6a28ebb4cd562874d7890adad0d0df28f419085fe1a1b2ba90844ddea977cfe25e3d7be2f6d8a3c627cf8cd8207379d69c43fbe1316877e4bbd00457cf46a
6
+ metadata.gz: ee0fdb300976264a9dd98dc25f1a73d9cd5f5258e1d272c327fd1bb51fe66d9c79d913192f3ac18a541972d5ba8469f2db62b6cea486babef8400a62b831800c
7
+ data.tar.gz: 64092e8dd171c15f40b202fd6849aed0b85ef1fa973f4257410a374bb0339ff29d546f54eb3b5021bd2aa223f0caa8f813e2f1d0926c1035d7d5afed3adf042b
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module String::Text
4
- VERSION = "0.6.0"
4
+ VERSION = "0.6.2"
5
5
  end
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. Lines starting
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,8 +58,15 @@ module String::Text
41
58
  lines.shift while !lines.empty? && !(lines.first =~ /^\s*\S/)
42
59
  return "" if lines.empty?
43
60
 
44
- # Find minimal indent. Ignores lines with indent 0
45
- indents = lines.map { _1 =~ /^(\s*)/; $1.size }
61
+ # Only align to given column if first line is not indented
62
+ if String::Text.indentation(lines.first) == 0
63
+ return lines.map { |line| ' ' * (column-1) + line }.join("\n")
64
+ end
65
+
66
+ # Identation level of each line
67
+ indents = lines.map { String::Text.indentation(_1) }
68
+
69
+ # Find minimal indent, ignoring unindented lines
46
70
  indent = indents.select { _1 > 0 }.min || 0
47
71
 
48
72
  first = true
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.6.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen