indented_io 0.8.6 → 0.9.0

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: cfc1de7e7ffda4d1fa37cd2979367b91b105cc43bc83a18d67816d14c3abd334
4
- data.tar.gz: b54b5d64a8af0ca25cb333a2aa94599fba21be53063bd8fe1953d48bb8e4e3c1
3
+ metadata.gz: ab6498e0cafc5162dbe8f021e5abe01d1782b103581ee6f506ad419e8d945ad7
4
+ data.tar.gz: 986de84134945946db538335c1a850805a022676b9f67b7bfdcbc888a0d2f680
5
5
  SHA512:
6
- metadata.gz: 84b4b34e808dd75d2fbf8d6c2e581448352d0c4d1c0f4bcf363b43e4b3b550a7619d80418fde9a5e8968e97b8c973b73ec9bb391b25c7967454061c9b39e824a
7
- data.tar.gz: f9041fd50c2f791c95b896e34b61b22050d792c938e83af9b89d48c81fb3e49b56d60b012d32c7cbdbc70a4e43d6e50ad651b7a12e13777da7178ab59cd01895
6
+ metadata.gz: 3b5e24b28007050283cd1c2f2903bc19524d2abaa4f9c120d7b8c48c827a5e8ac0a4a2406258743553bfa673dd298c0937cc75ea5c0b2f162bfc33ffeedd9154
7
+ data.tar.gz: 660976e3894125018aaa7968a331144670d2e071e852f48702f4de4f5fc4bd5ea97df95fc6b2df91bc38ec5f77b880dea33576363a1733ed79f66a67a02099fa
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.7.1
1
+ ruby-3.1.2
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in indented_io.gemspec
6
6
  gemspec
7
+
8
+ gem "rake", "~>12.0"
9
+ gem "rspec", "~>3.0"
data/TODO CHANGED
@@ -1,11 +1,27 @@
1
1
 
2
2
  TODO
3
+ o Make the folowing wotk
4
+ # Shorthand for
5
+ # puts "Header"
6
+ # indent { puts content }
7
+ #
8
+ puts "Header", indent, content
9
+
10
+ # Shorthand for
11
+ # puts "Header"
12
+ # indent { recurse }
13
+ puts "Header", indent { recurse }
14
+
15
+ o Fix "goes wrong" in spec/indented_io_spec.rb by making
16
+ IndentedIOInterface#indent create a IndentedIO object for each level. This
17
+ may require blocks to pop-off multiple levels
18
+ o Fix that #indent is defined on _all_ objects!
3
19
  o Find a solution for the inclusion of StringIO and Tempfile (the last is
4
20
  required by ruby 2.7 - see also note in lib/indented_io/tempfile.rb
5
21
  o Check if IO object is writable - no good solution ?
6
22
  o better name for @this_indent ? 'string' ?
7
23
  o Reimplement core loop in C
8
- o Create BaseIndentedIO to hold bol status and remove it from IndentedIO
24
+ o Create InitialIndentedIO to hold bol status and remove it from IndentedIO
9
25
  o Transmogrify instances variables and protected methods in IndentedIO to
10
26
  avoid collision with names from the underlying device
11
27
  o Explain name collision issues in the docs
data/indented_io.gemspec CHANGED
@@ -1,7 +1,4 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "indented_io/version"
1
+ require_relative 'lib/indented_io/version'
5
2
 
6
3
  Gem::Specification.new do |spec|
7
4
  spec.name = "indented_io"
@@ -20,23 +17,12 @@ Gem::Specification.new do |spec|
20
17
  spec.homepage = "https://github.com/clrgit/indented_io"
21
18
  spec.license = "MIT"
22
19
 
23
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
24
- # to allow pushing to a single host or delete this section to allow pushing to any host.
25
- if spec.respond_to?(:metadata)
26
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
27
- else
28
- raise "RubyGems 2.0 or newer is required to protect against " \
29
- "public gem pushes."
30
- end
31
-
32
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
33
- f.match(%r{^(test|spec|features)/})
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
24
  end
35
25
  spec.bindir = "exe"
36
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
27
  spec.require_paths = ["lib"]
38
-
39
- spec.add_development_dependency "bundler", "~> 1.16"
40
- spec.add_development_dependency "rake", ">= 12.3.3"
41
- spec.add_development_dependency "rspec", "~> 3.0"
42
28
  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
@@ -1,4 +1,4 @@
1
1
  module IndentedIO
2
2
  # Version number
3
- VERSION = '0.8.6'
3
+ VERSION = '0.9.0'
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,57 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indented_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.6
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-23 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.16'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.16'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 12.3.3
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 12.3.3
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '3.0'
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
+ dependencies: []
55
13
  description: "\n IndentedIO extends Kernel, IO, and StringIO
56
14
  with an\n #indent method that redefines #print, printf,
57
15
  #puts,\n and #p to print their output indented. Indentations
@@ -88,9 +46,8 @@ files:
88
46
  homepage: https://github.com/clrgit/indented_io
89
47
  licenses:
90
48
  - MIT
91
- metadata:
92
- allowed_push_host: https://rubygems.org
93
- post_install_message:
49
+ metadata: {}
50
+ post_install_message:
94
51
  rdoc_options: []
95
52
  require_paths:
96
53
  - lib
@@ -105,8 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
62
  - !ruby/object:Gem::Version
106
63
  version: '0'
107
64
  requirements: []
108
- rubygems_version: 3.1.4
109
- signing_key:
65
+ rubygems_version: 3.3.18
66
+ signing_key:
110
67
  specification_version: 4
111
68
  summary: Print indented text
112
69
  test_files: []