indented_io 0.8.5 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b381c1136988e46a6a79848db05b1563a6704619775b4945e879c1efeb0dfc7a
4
- data.tar.gz: 7296f2367c4eecb9037bf9c2f4492ee21ff3d8520c841ad95f9c06527f9cf8f8
3
+ metadata.gz: b429449b90ca605dbcf4f07abaee53a8f2eece7a3131024649c0c2b55775f962
4
+ data.tar.gz: eccb5276e5d1356e58809d674a27dc64651bb885c03000e1ac3150b50f989206
5
5
  SHA512:
6
- metadata.gz: 216242b00c20a64f0a6f4394f7587bb4d663864814bfcae5ab96b76401ae9498eb0fbc9211b2aa7addbe2c2321397ad8b8cf70329eea60f8b8434346302198f3
7
- data.tar.gz: d0dcd905b87f2c024137d3a862c1ae8ad9b345e38cec918a0e8d1a1b2a678edc66196d112d6608d98fbb94f462aae61c37d698b13fd533dc5f4125750ea2ddab
6
+ metadata.gz: 93fc65e925cad892a1f8cb716ee5064d4a23112930e14ed184072108550e5470834fc97e400011465de1e48a645dbeee3f65883f14e2242fa6a57913bdba51d5
7
+ data.tar.gz: 40161650518478d5ad566ceade2274b9900dd651f89bff44246cbd066bdd96d3a5d8cb9da5258d046d324deb1d06feaa608528d46815cce2aa0ed278bdbeeb68
data/TODO CHANGED
@@ -1,11 +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
6
+ o Fix that #indent is defined on _all_ objects!
3
7
  o Find a solution for the inclusion of StringIO and Tempfile (the last is
4
8
  required by ruby 2.7 - see also note in lib/indented_io/tempfile.rb
5
9
  o Check if IO object is writable - no good solution ?
6
10
  o better name for @this_indent ? 'string' ?
7
11
  o Reimplement core loop in C
8
- 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
9
13
  o Transmogrify instances variables and protected methods in IndentedIO to
10
14
  avoid collision with names from the underlying device
11
15
  o Explain name collision issues in the docs
data/indented_io.gemspec CHANGED
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
37
  spec.require_paths = ["lib"]
38
38
 
39
- spec.add_development_dependency "bundler", "~> 1.16"
39
+ spec.add_development_dependency "bundler", "~> 2.2.10"
40
40
  spec.add_development_dependency "rake", ">= 12.3.3"
41
41
  spec.add_development_dependency "rspec", "~> 3.0"
42
42
  end
@@ -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
@@ -33,9 +33,21 @@ module IndentedIO
33
33
  # If +level+ is negative, #indent will outdent text instead
34
34
  #
35
35
  def indent(levels = 1, string_ = ::IndentedIO.default_indent, string: string_, bol: nil, &block)
36
- block.nil? || block.arity == 1 or raise ::IndentedIO::Error.new "Wrong number of parameters"
37
- obj = ::IndentedIO::IndentedIO.send(:new, self, levels, string, bol)
38
- block_given? ? yield(obj) : obj
36
+ block.nil? || block.arity == 1 or raise ::IndentedIO::Error.new "Wrong number of block parameters"
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
@@ -0,0 +1,14 @@
1
+ require 'indented_io/indented_io_interface'
2
+
3
+ require "tempfile"
4
+
5
+ # Includes the IndentedIOInterface that define the #indent method
6
+ #
7
+ # Note that Tempfile used to be a (kind of) IO but that changed in ruby-2.7.
8
+ # The problem is that we now have to require both StringIO and Tempfile and
9
+ # possible other classes to inject the #indent method instead of just doing it
10
+ # once in the IO class itself. TODO: Find a better solution - probably by
11
+ # implementing a check in Kernel
12
+ class Tempfile
13
+ include IndentedIO::IndentedIOInterface
14
+ end
@@ -1,4 +1,4 @@
1
1
  module IndentedIO
2
2
  # Version number
3
- VERSION = '0.8.5'
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.5
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-04-15 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
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: 2.2.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: 2.2.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +82,7 @@ files:
82
82
  - lib/indented_io/io.rb
83
83
  - lib/indented_io/kernel.rb
84
84
  - lib/indented_io/stringio.rb
85
+ - lib/indented_io/tempfile.rb
85
86
  - lib/indented_io/version.rb
86
87
  - scripts/perf.rb
87
88
  homepage: https://github.com/clrgit/indented_io