indented_io 0.8.7 → 0.8.8

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: 16a05ee639c3d89bf4e32a1cf77ccc9bf686106f0701761c4615847ba693e1dc
4
- data.tar.gz: 5dac44faa9ac00089cfa3aeb196e43e160222faa6c625986525c66a8c7bf5132
3
+ metadata.gz: b429449b90ca605dbcf4f07abaee53a8f2eece7a3131024649c0c2b55775f962
4
+ data.tar.gz: eccb5276e5d1356e58809d674a27dc64651bb885c03000e1ac3150b50f989206
5
5
  SHA512:
6
- metadata.gz: 16cd031d80fb46ec616b0d8ae437df88c448adfed411a2fa9f0071728fd1d3e96fe3469e8aadb7580e913213d57d1830276c9c2c967fd5f2254424e67d192101
7
- data.tar.gz: d2f7fe0230a81e5309996f69c1d0d2da8898096fe584f43e3174694f060256a1efe5ff0396599753d0cc8e35bc833ad456bc2423a9a19f053d0ecbddb0dea1f7
6
+ metadata.gz: 93fc65e925cad892a1f8cb716ee5064d4a23112930e14ed184072108550e5470834fc97e400011465de1e48a645dbeee3f65883f14e2242fa6a57913bdba51d5
7
+ data.tar.gz: 40161650518478d5ad566ceade2274b9900dd651f89bff44246cbd066bdd96d3a5d8cb9da5258d046d324deb1d06feaa608528d46815cce2aa0ed278bdbeeb68
data/TODO CHANGED
@@ -1,12 +1,15 @@
1
1
 
2
2
  TODO
3
+ o Fix "goes wrong" in spec/indented_io_spec.rb by making
4
+ IndentedIOInterface#indent create a IndentedIO object for each level. This
5
+ may require blocks to pop-off multiple levels
3
6
  o Fix that #indent is defined on _all_ objects!
4
7
  o Find a solution for the inclusion of StringIO and Tempfile (the last is
5
8
  required by ruby 2.7 - see also note in lib/indented_io/tempfile.rb
6
9
  o Check if IO object is writable - no good solution ?
7
10
  o better name for @this_indent ? 'string' ?
8
11
  o Reimplement core loop in C
9
- o Create BaseIndentedIO to hold bol status and remove it from IndentedIO
12
+ o Create InitialIndentedIO to hold bol status and remove it from IndentedIO
10
13
  o Transmogrify instances variables and protected methods in IndentedIO to
11
14
  avoid collision with names from the underlying device
12
15
  o Explain name collision issues in the docs
@@ -1,11 +1,12 @@
1
1
  require 'indented_io/indented_io_interface'
2
2
 
3
3
  module IndentedIO
4
- # An IO device that writes indented text
4
+ # An IO device that writes indented text by reimplementing #write, #print,
5
+ # #printf, #puts, and #p
5
6
  #
6
7
  # IndentedIO objects forms a chain that acts as a stack. The lowest element
7
- # in the stack is always a "pure" IO object (eg. $stdout) and as indentation
8
- # levels rise or fall IndentedIO objects are moved on and off the stack
8
+ # in the stack is always a "pure" IO object (eg. $stdout). IndentedIO object
9
+ # are than moved on and off the stack as indentation levels rise or fall
9
10
  #
10
11
  # Note that #new is private. The only way to create a IndentedIO object is to
11
12
  # call #indent on an object that supports it ({Kernel}, {IO}, or {StringIO})
@@ -16,8 +17,8 @@ module IndentedIO
16
17
  alias :interface_indent :indent
17
18
 
18
19
  # (see IndentedIO::IndentedIOInterface#indent)
19
- def indent(levels=1, string_ = self.this_indent, string: string_, bol: nil, &block)
20
- interface_indent(levels, string, bol: bol, &block)
20
+ def indent(depth=1, string_ = self.this_indent, string: string_, bol: nil, &block)
21
+ interface_indent(depth, string, bol: bol, &block)
21
22
  end
22
23
 
23
24
  # Indent and print args to the underlying device. #write has the same semantic
@@ -53,8 +54,8 @@ module IndentedIO
53
54
  end
54
55
 
55
56
  # Indent and print args to the underlying device. #p has the same semantic
56
- # as Kernel#p. Please note that #p is only defined on Kernel in the Ruby core
57
- # library but can be used on any IndentedIO object
57
+ # as Kernel#p. Please note that even though #p is only defined on Kernel
58
+ # but can be used on any IndentedIO object so you can say '$stderr.p value'
58
59
  def p(*args)
59
60
  if bol
60
61
  args.each { |arg| write(arg.inspect, "\n") }
@@ -66,10 +67,12 @@ module IndentedIO
66
67
  args.size == 1 ? args.first : args
67
68
  end
68
69
 
69
- # Make IndentedIO behave like the underlying @device
70
+ # Make IndentedIO behave like the underlying @device by only searching for
71
+ # methods in the device
72
+ #
70
73
  # @!visibility private
71
74
  def respond_to?(method, include_all = false)
72
- [:indent, :level, :print, :printf, :puts, :p].include?(method) || device.respond_to?(method)
75
+ [:indent, :depth, :tab, :p].include?(method) || device.respond_to?(method, include_all)
73
76
  end
74
77
 
75
78
  # Make IndentedIO behave like the underlying @device
@@ -78,22 +81,27 @@ module IndentedIO
78
81
  device.send(method, *args)
79
82
  end
80
83
 
84
+ # Depth of this object. #depth is always bigger than zero for IndentedIO
85
+ # objects
86
+ attr_reader :depth
87
+
88
+ # Tabulator position for this object. The is the same as the combined
89
+ # length of the indentation levels
90
+ attr_reader :tab
91
+
81
92
  protected
93
+ # Parent object. This can be an IndentedIO, IO, or StringIO object
94
+ attr_reader :parent
95
+
82
96
  # Reference to the pure IO object at the bottom of the stack. It is used to
83
97
  # write directly to the IO object without having to recurse down the IndentedIO
84
98
  # stack
85
99
  attr_reader :device
86
100
 
87
101
  # First IndentedIO object on the stack. Equal to self if self is the first
88
- # indentation object. The #base object is used to keep track of #bol for
89
- # the whole stack of IndentedIO objects
90
- attr_reader :base
91
-
92
- # Parent IndentedIO or IO object
93
- attr_reader :parent
94
-
95
- # Number of indent levels
96
- attr_reader :levels
102
+ # indentation object. The #initial object is used to store a stack-global
103
+ # bol flag
104
+ attr_reader :initial
97
105
 
98
106
  # Indent string for this device
99
107
  attr_reader :this_indent
@@ -101,28 +109,29 @@ module IndentedIO
101
109
  # The combined indent strings of previous levels plus this device's indent string
102
110
  attr_reader :combined_indent
103
111
 
104
- # True iff at Beginning-Of-Line
112
+ # True iff at Beginning-Of-Line. Note that #bol is global for the whole
113
+ # indentation stack
105
114
  def bol()
106
- @base == self ? @bol : @base.bol # @bol only exists in the #base object
115
+ @initial == self ? @bol : @initial.bol # @bol only exists in the #base object
107
116
  end
108
117
 
109
- # Set Beginning-Of-Line to true or false
118
+ # Set Beginning-Of-Line to true or false. Note that #bol is global for the
119
+ # whole indentation stack
110
120
  def bol=(bol)
111
- @base.instance_variable_set(:@bol, bol) # @bol only exists in the #base object
121
+ @initial.instance_variable_set(:@bol, bol) # @bol only exists in the #initial object
112
122
  end
113
123
 
114
- # Current level
115
- def level
116
- @level ||= @levels + (parent.is_a?(::IndentedIO::IndentedIO) ? parent.level : 0)
117
- end
124
+ # Number of indent levels for this IndentedIO object
125
+ attr_reader :levels
118
126
 
119
127
  # Hide new
120
128
  private_class_method :new
121
129
 
130
+ # TODO: Move multi-level functionality to ::IndentedIO.indent
122
131
  def initialize(parent, levels, this_indent, bol)
123
132
  if levels < 0
124
- parent.is_a?(::IndentedIO::IndentedIO) or raise IndentedIO::Error.new "Negative levels argument"
125
- parent.level + levels >= 0 or raise IndentedIO::Error.new "levels out of range"
133
+ parent.is_a?(::IndentedIO::IndentedIO) or raise ::IndentedIO::Error.new "Negative levels argument"
134
+ parent.levels + levels >= 0 or raise ::IndentedIO::Error.new "levels out of range"
126
135
  sibling = parent
127
136
  while parent.is_a?(::IndentedIO::IndentedIO) && levels < 0
128
137
  levels += parent.levels
@@ -133,15 +142,17 @@ module IndentedIO
133
142
  end
134
143
  @parent = parent
135
144
  @levels = levels
145
+ @depth = @parent.depth + levels
146
+ @tab = @parent.tab + this_indent.size * @levels
136
147
  @this_indent = this_indent
137
148
  if @parent.is_a?(::IndentedIO::IndentedIO)
138
149
  @device = @parent.device
139
- @base = @parent.base
150
+ @initial = @parent.initial
140
151
  @combined_indent = @parent.combined_indent + @this_indent * @levels
141
152
  self.bol = bol if !bol.nil?
142
153
  else
143
154
  @device = parent
144
- @base = self
155
+ @initial = self
145
156
  @combined_indent = @this_indent * @levels
146
157
  self.bol = (bol.nil? ? true : bol)
147
158
  end
@@ -34,8 +34,20 @@ module IndentedIO
34
34
  #
35
35
  def indent(levels = 1, string_ = ::IndentedIO.default_indent, string: string_, bol: nil, &block)
36
36
  block.nil? || block.arity == 1 or raise ::IndentedIO::Error.new "Wrong number of block parameters"
37
- obj = ::IndentedIO::IndentedIO.send(:new, self, levels, string, bol)
38
- block_given? ? yield(obj) : obj
37
+ @indented_io_object = ::IndentedIO::IndentedIO.send(:new, self, levels, string, bol)
38
+ block_given? ? yield(@indented_io_object) : @indented_io_object
39
39
  end
40
+
41
+ # :call-seq:
42
+ # indent(levels, string = ::IndentedIO.default:_indent, bol: nil, &block)
43
+ # indent(string = ::IndentedIO.default:_indent, bol: nil, &block)
44
+
45
+ # Return the accumulated number of indentation levels. An unindented device
46
+ # has depth equal to 0
47
+ def depth() 0 end
48
+
49
+ # Return current tabulator position. This is the same as the combined
50
+ # length of the indentations. An unindented device has depth equal to 0
51
+ def tab() 0 end
40
52
  end
41
53
  end
@@ -1,4 +1,4 @@
1
1
  module IndentedIO
2
2
  # Version number
3
- VERSION = '0.8.7'
3
+ VERSION = '0.8.8'
4
4
  end
data/lib/indented_io.rb CHANGED
@@ -13,7 +13,7 @@ require 'indented_io/tempfile'
13
13
  #
14
14
  module IndentedIO
15
15
  # Returns default indentation. Two spaces is the default but it can be set by
16
- # #default_indent
16
+ # #default_indent=
17
17
  def self.default_indent() @DEFAULT_INDENT end
18
18
 
19
19
  # Sets default indentation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indented_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.7
4
+ version: 0.8.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-18 00:00:00.000000000 Z
11
+ date: 2022-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler