indented_io 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/indented_io/error.rb +8 -1
- data/lib/indented_io/indented_io.rb +11 -13
- data/lib/indented_io/indented_io_interface.rb +26 -15
- data/lib/indented_io/io.rb +1 -1
- data/lib/indented_io/kernel.rb +16 -1
- data/lib/indented_io/stringio.rb +1 -1
- data/lib/indented_io/version.rb +1 -1
- data/lib/indented_io.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae77258c328dc98d5333e7954d3412556a1628a9ae2797885342f3efeff9c84a
|
4
|
+
data.tar.gz: 167ea53c4fd36d27a544c304ce72632c452641f3c2978a1c1cc15e1b844e58e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 843c6ca1105dca45e8ea3b53c47933db4e650451b29294855a80a7a84186804d7565f49309fdf2fcbdea30c43d605caea2bf1a5b65c0c6fc90501b896ee8bf23
|
7
|
+
data.tar.gz: 248f51b62caab9925452779161b2e910eb06aaa62b9c1e886d75bb040b1feb613d7cc9605ddad5aa0c2d2ab7fda5e92dbf0b95b398c278bd287098a351234f73
|
data/README.md
CHANGED
@@ -20,13 +20,13 @@ indent(2, "* ").puts "Indented two levels"
|
|
20
20
|
|
21
21
|
outputs
|
22
22
|
|
23
|
-
```
|
23
|
+
```
|
24
24
|
Not indented
|
25
25
|
Indented one level
|
26
26
|
* * Indented two levels
|
27
27
|
```
|
28
28
|
|
29
|
-
####
|
29
|
+
#### `Kernel#indent`, `IO#indent`, and `StringIO#indent`
|
30
30
|
|
31
31
|
`#indent` without a block returns an `IndentedIO` object that acts as the
|
32
32
|
receiver but redefine `#print`, `#printf`, `#puts`, and `#p` to print indented
|
@@ -201,7 +201,7 @@ git commits and tags, and push the `.gem` file to
|
|
201
201
|
## Contributing
|
202
202
|
|
203
203
|
Bug reports and pull requests are welcome on GitHub at
|
204
|
-
https://github.com/
|
204
|
+
https://github.com/clrgit/indented_io.
|
205
205
|
|
206
206
|
## License
|
207
207
|
|
data/lib/indented_io/error.rb
CHANGED
@@ -8,26 +8,18 @@ module IndentedIO
|
|
8
8
|
# levels rise or fall IndentedIO objects are moved on and off the stack
|
9
9
|
#
|
10
10
|
# Note that #new is private. The only way to create a IndentedIO object is to
|
11
|
-
# call #indent on
|
11
|
+
# call #indent on an object that supports it
|
12
12
|
class IndentedIO
|
13
13
|
include IndentedIOInterface
|
14
14
|
|
15
|
-
#
|
15
|
+
# @!visibility private
|
16
16
|
alias :interface_indent :indent
|
17
|
-
# :startdoc:
|
18
17
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# See IndentedIO::IndentedIOInterface#indent for documentation
|
18
|
+
# (see IndentedIO::IndentedIOInterface#indent)
|
22
19
|
def indent(levels=1, string_ = self.this_indent, string: string_, bol: nil, &block)
|
23
20
|
interface_indent(levels, string, bol: bol, &block)
|
24
21
|
end
|
25
22
|
|
26
|
-
# Current level
|
27
|
-
def level
|
28
|
-
@level ||= @levels + (parent.is_a?(::IndentedIO::IndentedIO) ? parent.level : 0)
|
29
|
-
end
|
30
|
-
|
31
23
|
# Indent and print args to the underlying device. #print has the same semantic
|
32
24
|
# as Kernel#print
|
33
25
|
def print(*args)
|
@@ -74,14 +66,14 @@ module IndentedIO
|
|
74
66
|
args.size == 1 ? args.first : args
|
75
67
|
end
|
76
68
|
|
77
|
-
# :stopdoc:
|
78
|
-
|
79
69
|
# Make IndentedIO behave like the underlying @device
|
70
|
+
# @!visibility private
|
80
71
|
def respond_to?(method)
|
81
72
|
[:indent, :level, :print, :puts, :p].include?(method) || device.respond_to?(method)
|
82
73
|
end
|
83
74
|
|
84
75
|
# Make IndentedIO behave like the underlying @device
|
76
|
+
# @!visibility private
|
85
77
|
def method_missing(method, *args)
|
86
78
|
device.send(method, *args)
|
87
79
|
end
|
@@ -119,6 +111,11 @@ module IndentedIO
|
|
119
111
|
@base.instance_variable_set(:@bol, bol) # @bol only exists in the #base object
|
120
112
|
end
|
121
113
|
|
114
|
+
# Current level
|
115
|
+
def level
|
116
|
+
@level ||= @levels + (parent.is_a?(::IndentedIO::IndentedIO) ? parent.level : 0)
|
117
|
+
end
|
118
|
+
|
122
119
|
# Hide new
|
123
120
|
private_class_method :new
|
124
121
|
|
@@ -151,6 +148,7 @@ module IndentedIO
|
|
151
148
|
end
|
152
149
|
|
153
150
|
public
|
151
|
+
# @!visibility private
|
154
152
|
def dump
|
155
153
|
$stderr.puts "#{self.class} [#{self.object_id}]"
|
156
154
|
$stderr.puts " device: #{device.class} [#{device.object_id}]"
|
@@ -1,25 +1,36 @@
|
|
1
1
|
module IndentedIO
|
2
2
|
# IndentedIO interface that provides the #indent method. Used by IO,
|
3
3
|
# StringIO, and IndentedIO. It can be included in any class that define a
|
4
|
-
# #print method
|
4
|
+
# #print method like this:
|
5
|
+
#
|
6
|
+
# require 'indented_io'
|
7
|
+
# class MyIO
|
8
|
+
# include IndentedIO::IndentedIOInterface
|
9
|
+
# def print(*args) ... end
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# my_io = MyIO.new
|
13
|
+
# my_io.print "Not indented\n"
|
14
|
+
# my_io.indent.puts "It works!"
|
15
|
+
#
|
16
|
+
# # Not indented
|
17
|
+
# # It works!
|
18
|
+
#
|
5
19
|
module IndentedIOInterface
|
6
20
|
# Returns a IndentedIO object that can be used for printing. The IO object
|
7
|
-
# will pass-through all method
|
8
|
-
#
|
21
|
+
# will pass-through all method to the underlying device except #print,
|
22
|
+
# #printf, #puts, and #p
|
9
23
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
24
|
+
# +level+ is the number of leves to indent and +string+ is the string used
|
25
|
+
# for indentation. The indentation string can also be given as the keyword
|
26
|
+
# parameter +:string+. Default is the indent string of the outer level or
|
27
|
+
# {::IndentedIO.default_indent} if this is the first level. +:bol+ control the
|
28
|
+
# beginning-of-line status: If true, #indent will begin writing with an
|
29
|
+
# indentation string as if it was at the beginning of the line. If false,
|
30
|
+
# it will only indent after the next newline. Default is true
|
31
|
+
#
|
32
|
+
# If +level+ is negative, #indent will outdent text instead
|
15
33
|
#
|
16
|
-
# +levels+:: Number of indentation levels. Default is one level
|
17
|
-
# +string+:: The indentation string. Default is the indent string of the
|
18
|
-
# outer level or ::IndentedIO.default_indent if this is the
|
19
|
-
# first level
|
20
|
-
# +bol+:: Beginning of line. If true, #indent will begin writing with an
|
21
|
-
# indentation string. If false, it will only indent after the next
|
22
|
-
# newline. Default true
|
23
34
|
def indent(levels = 1, string_ = ::IndentedIO.default_indent, string: string_, bol: nil, &block)
|
24
35
|
block.nil? || block.arity == 1 or raise ::IndentedIO::Error.new "Wrong number of parameters"
|
25
36
|
obj = ::IndentedIO::IndentedIO.send(:new, self, levels, string, bol)
|
data/lib/indented_io/io.rb
CHANGED
data/lib/indented_io/kernel.rb
CHANGED
@@ -1,7 +1,22 @@
|
|
1
1
|
require 'indented_io/indented_io_interface'
|
2
2
|
|
3
|
-
# :nodoc:
|
4
3
|
module Kernel
|
4
|
+
# Like {IndentedIO::IndentedIOInterface#indent} except the underlying device is
|
5
|
+
# not the receiver (Kernel) but $stdout. Kernel#indent also allows a block without
|
6
|
+
# and argument. In that case it manipulates $stdout to print indented:
|
7
|
+
#
|
8
|
+
# puts "Not indented
|
9
|
+
# indent {
|
10
|
+
# puts "Indented"
|
11
|
+
# indent {
|
12
|
+
# puts "Even more indented"
|
13
|
+
# }
|
14
|
+
# }
|
15
|
+
#
|
16
|
+
# # Not indented
|
17
|
+
# # Indented
|
18
|
+
# # Even more indented
|
19
|
+
#
|
5
20
|
def indent(levels = 1, string_ = IndentedIO.default_indent, string: string_, bol: nil, &block)
|
6
21
|
block.nil? || block.arity <= 1 or raise IndentedIO::Error.new "Wrong number of parameters"
|
7
22
|
obj = IndentedIO::IndentedIO.send(:new, $stdout, levels, string, bol)
|
data/lib/indented_io/stringio.rb
CHANGED
data/lib/indented_io/version.rb
CHANGED
data/lib/indented_io.rb
CHANGED
@@ -10,11 +10,11 @@ require 'indented_io/stringio'
|
|
10
10
|
# IndentedIO module
|
11
11
|
|
12
12
|
module IndentedIO
|
13
|
-
# Returns default indentation.
|
14
|
-
#
|
13
|
+
# Returns default indentation. Two spaces is the default but it can be set by
|
14
|
+
# #default_indent
|
15
15
|
def self.default_indent() @DEFAULT_INDENT end
|
16
16
|
|
17
|
-
#
|
17
|
+
# Sets default indentation
|
18
18
|
def self.default_indent=(indent) @DEFAULT_INDENT = indent end
|
19
19
|
|
20
20
|
private
|