indented_io 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -16
- data/TODO +7 -2
- data/indented_io.gemspec +5 -7
- data/lib/indented_io.rb +3 -2
- data/lib/indented_io/error.rb +3 -3
- data/lib/indented_io/indented_io.rb +1 -1
- data/lib/indented_io/indented_io_interface.rb +2 -2
- data/lib/indented_io/kernel.rb +4 -4
- data/lib/indented_io/version.rb +1 -1
- data/scripts/perf.rb +108 -0
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f678cdfa6edee19dad6a5256367071ceea8d1ca7ebb5dd4556330f260250e0e1
|
4
|
+
data.tar.gz: a2cb615747e62ca14ed25b872ae501c9f6b0dbaadd1a9ec73f3a2dbfcd519f61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 914ca7aef4d7017fc4899c2f1cc19da8a7cf6bacc7c0937bd28033bef7cb2350f3878bd0ac9380301d4e2b0cb7df6aded579085a7a52c0f48206e80dbe6b3a7a
|
7
|
+
data.tar.gz: 5dcf284b39596f7faaff975d2e703125caa157c133646a1aa9a2147bb95b7c4c852de61c3bcdc872208c08d79a7d4acc42a0170d168d66e9a56199082ed5d328
|
data/README.md
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
# IndentedIO
|
2
2
|
|
3
|
-
Print indented text
|
4
|
-
|
5
3
|
`IndentedIO` extends `Kernel`, `IO`, and `StringIO` with an `#indent` method
|
6
4
|
that returns an `IndentedIO` object. The `IndentedIO` object acts as the
|
7
|
-
original object but redefines
|
8
|
-
|
9
|
-
|
5
|
+
original object but redefines `#print`, `#printf`, `#puts`, and `#p` to print
|
6
|
+
their output indented. Indentations are stacked so that each new indentation
|
7
|
+
adds to the previous indendation
|
10
8
|
|
11
9
|
## Usage
|
12
10
|
|
@@ -54,10 +52,10 @@ instead:
|
|
54
52
|
|
55
53
|
```ruby
|
56
54
|
$stdout.puts "Not indented"
|
57
|
-
$stdout.indent(2, "> ")
|
55
|
+
$stdout.indent(2, "> ") do |f|
|
58
56
|
f.indent(string: "* ").puts "Indented three levels"
|
59
57
|
f.indent(-1).puts "Indented one level"
|
60
|
-
|
58
|
+
end
|
61
59
|
|
62
60
|
# Not indented
|
63
61
|
# > > * Indented three levels
|
@@ -74,13 +72,13 @@ indented within that block:
|
|
74
72
|
|
75
73
|
```ruby
|
76
74
|
puts "Not indented"
|
77
|
-
indent
|
75
|
+
indent do
|
78
76
|
puts "Indented one level"
|
79
|
-
indent
|
77
|
+
indent do
|
80
78
|
puts "Indented two levels"
|
81
|
-
|
79
|
+
end
|
82
80
|
puts "Indented one level"
|
83
|
-
|
81
|
+
end
|
84
82
|
puts "Not indented"
|
85
83
|
|
86
84
|
# Not indented
|
@@ -140,7 +138,7 @@ to get a different indentation for a part of the program
|
|
140
138
|
|
141
139
|
In case of errors an `IndentedIO::Error` exception is raised
|
142
140
|
|
143
|
-
##
|
141
|
+
## Adding support for other classes
|
144
142
|
|
145
143
|
You can add support for your own IO objects by including
|
146
144
|
`IndentedIO::IndentedIOInterface` in your class. All that is required is that
|
@@ -155,7 +153,11 @@ class MyIO
|
|
155
153
|
end
|
156
154
|
|
157
155
|
my_io = MyIO.new
|
156
|
+
my_io.puts "Not indented"
|
158
157
|
my_io.indent.puts "It works!"
|
158
|
+
|
159
|
+
# Not indented
|
160
|
+
# It works!
|
159
161
|
```
|
160
162
|
|
161
163
|
## Implementation & performance
|
@@ -165,10 +167,14 @@ my_io.indent.puts "It works!"
|
|
165
167
|
with a block without parameters manipulates `$stdout`, replacing it with an
|
166
168
|
`IndentedIO` object for the duration of the block
|
167
169
|
|
168
|
-
The implementation carries no overhead if it is not used but the core
|
169
|
-
mechanism processes characters one-by-one which is
|
170
|
-
|
171
|
-
|
170
|
+
The implementation carries no overhead if it is not used but the core
|
171
|
+
indentation mechanism processes characters one-by-one which is about 10-15
|
172
|
+
times slower than a handwritten implementation (scripts/perf.rb is a script to
|
173
|
+
check performance). It would be much faster if the inner loop was implemented
|
174
|
+
in C. However, we're talking micro-seconds here: Printing without using
|
175
|
+
IndentedIO range from around 0.25ms to 1ms while using IndentedIO slows it down
|
176
|
+
to between 4 and 12 microseconds, so IndentedIO won't cause a noticeable slow
|
177
|
+
down of your application
|
172
178
|
|
173
179
|
## Installation
|
174
180
|
|
data/TODO
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
TODO
|
3
3
|
o Check if IO object is writable - no good solution ?
|
4
4
|
o better name for @this_indent ? 'string' ?
|
5
|
+
o Change dependency in IndentedIOInterface from #print to #write and bump
|
6
|
+
version to 0.8 as this is the last user-visible change to be made
|
7
|
+
o Reimplement core loop in C
|
8
|
+
o Create BaseIndentedIO to hold bol status and remove it from IndentedIO
|
9
|
+
o Transmogrify instances variables and protected methods in IndentedIO to
|
10
|
+
avoid collision with names from the underlying device
|
11
|
+
o Explain name collision issues in the docs
|
5
12
|
|
6
13
|
+ explain bol
|
7
14
|
+ Allow a symbolic :string argument
|
@@ -15,5 +22,3 @@ TODO
|
|
15
22
|
+ Rename Device -> IndentedIO
|
16
23
|
|
17
24
|
- Make Kernel.indent behave like Kernel ?
|
18
|
-
|
19
|
-
? Create a BaseIndentedIO class ?
|
data/indented_io.gemspec
CHANGED
@@ -11,13 +11,11 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{Print indented text}
|
13
13
|
spec.description = %q{
|
14
|
-
IndentedIO extends Kernel, IO, and StringIO
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
indented. Indentations are stacked so that each new
|
20
|
-
indentation adds to the previous indendation
|
14
|
+
IndentedIO extends Kernel, IO, and StringIO with an
|
15
|
+
#indent method that redefines #print, printf, #puts,
|
16
|
+
and #p to print their output indented. Indentations
|
17
|
+
are stacked so that each new indentation adds to the
|
18
|
+
previous indendation
|
21
19
|
}
|
22
20
|
spec.homepage = "https://github.com/clrgit/indented_io"
|
23
21
|
spec.license = "MIT"
|
data/lib/indented_io.rb
CHANGED
@@ -7,8 +7,9 @@ require 'indented_io/kernel'
|
|
7
7
|
require 'indented_io/io'
|
8
8
|
require 'indented_io/stringio'
|
9
9
|
|
10
|
-
# IndentedIO module
|
11
|
-
|
10
|
+
# IndentedIO module. See {IndentedIO::IndentedIO} for documentation on how to
|
11
|
+
# use this module
|
12
|
+
#
|
12
13
|
module IndentedIO
|
13
14
|
# Returns default indentation. Two spaces is the default but it can be set by
|
14
15
|
# #default_indent
|
data/lib/indented_io/error.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
2
|
module IndentedIO
|
3
|
-
# Error class. To rescue errors
|
3
|
+
# Error class derived from RuntimeError. To rescue errors from IndentedIO do
|
4
4
|
#
|
5
5
|
# begin
|
6
|
-
# do_some_stuff
|
6
|
+
# # do_some_stuff
|
7
7
|
# rescue IndentedIO::Error => ex
|
8
|
-
# handle_error
|
8
|
+
# # handle_error
|
9
9
|
# end
|
10
10
|
#
|
11
11
|
class Error < RuntimeError; end
|
@@ -8,7 +8,7 @@ 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 an object that supports it
|
11
|
+
# call #indent on an object that supports it ({Kernel}, {IO}, or {StringIO})
|
12
12
|
class IndentedIO
|
13
13
|
include IndentedIOInterface
|
14
14
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module IndentedIO
|
2
|
-
# IndentedIO interface that provides the #indent method.
|
3
|
-
# StringIO, and IndentedIO
|
2
|
+
# IndentedIO interface that provides the #indent method. It is used by IO,
|
3
|
+
# StringIO, and IndentedIO but can be included in any class that define a
|
4
4
|
# #print method like this:
|
5
5
|
#
|
6
6
|
# require 'indented_io'
|
data/lib/indented_io/kernel.rb
CHANGED
@@ -6,12 +6,12 @@ module Kernel
|
|
6
6
|
# and argument. In that case it manipulates $stdout to print indented:
|
7
7
|
#
|
8
8
|
# puts "Not indented
|
9
|
-
# indent
|
9
|
+
# indent do
|
10
10
|
# puts "Indented"
|
11
|
-
# indent
|
11
|
+
# indent do
|
12
12
|
# puts "Even more indented"
|
13
|
-
#
|
14
|
-
#
|
13
|
+
# end
|
14
|
+
# end
|
15
15
|
#
|
16
16
|
# # Not indented
|
17
17
|
# # Indented
|
data/lib/indented_io/version.rb
CHANGED
data/scripts/perf.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
exec("bundle exec #$0 '#{ARGV.join("' '")}'") if !ENV.key?('BUNDLE_BIN_PATH')
|
4
|
+
|
5
|
+
RUNS = 1_000_000
|
6
|
+
|
7
|
+
saved_stdout = $stdout
|
8
|
+
$stdout = File.open("/dev/null", "w")
|
9
|
+
|
10
|
+
def timeit(&block)
|
11
|
+
t0 = Time.now
|
12
|
+
RUNS.times {
|
13
|
+
yield
|
14
|
+
}
|
15
|
+
t1 = Time.now
|
16
|
+
(1000 * (t1 - t0)).round(0)
|
17
|
+
end
|
18
|
+
|
19
|
+
def report(title, wo_indent, w_indent)
|
20
|
+
times = (w_indent / wo_indent).round(1)
|
21
|
+
if times < 1.1
|
22
|
+
slower = "same speed"
|
23
|
+
else
|
24
|
+
slower = "#{times} times slower"
|
25
|
+
end
|
26
|
+
|
27
|
+
puts title
|
28
|
+
indent {
|
29
|
+
puts "w/o indented_io: #{wo_indent}ms"
|
30
|
+
puts "w/indented_io : #{w_indent}ms (#{slower})"
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
# No indent
|
35
|
+
#
|
36
|
+
flat_output_wo_indent = timeit { puts "Not indented" }
|
37
|
+
|
38
|
+
require 'indented_io'
|
39
|
+
|
40
|
+
flat_output_w_indent = timeit { puts "Not indented" }
|
41
|
+
|
42
|
+
# Indented one level
|
43
|
+
#
|
44
|
+
static_wo_indent = timeit { puts " Indented" }
|
45
|
+
|
46
|
+
static_w_indent = nil
|
47
|
+
indent {
|
48
|
+
static_w_indent = timeit { puts "Indented" }
|
49
|
+
}
|
50
|
+
|
51
|
+
# Indented by a dynamic string
|
52
|
+
#
|
53
|
+
indent = " "
|
54
|
+
dynamic_wo_indent = timeit {
|
55
|
+
puts "#{indent}Indented"
|
56
|
+
}
|
57
|
+
|
58
|
+
dynamic_w_indent = nil
|
59
|
+
indent(" ") {
|
60
|
+
dynamic_w_indent = timeit { puts "Indented" }
|
61
|
+
}
|
62
|
+
|
63
|
+
# Recursive indentation
|
64
|
+
#
|
65
|
+
def recur_wo_indent(level, indent)
|
66
|
+
puts "#{indent}Indented"
|
67
|
+
if level > 0
|
68
|
+
recur_wo_indent(level - 1, "#{indent} ")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def recur_w_indent(level)
|
73
|
+
puts "Indented"
|
74
|
+
if level > 0
|
75
|
+
indent { recur_w_indent(level - 1) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
recursive_wo_indent = timeit { recur_wo_indent(2, " ") }
|
80
|
+
recursive_w_indent = timeit { recur_w_indent(2) }
|
81
|
+
|
82
|
+
$stdout = saved_stdout
|
83
|
+
|
84
|
+
puts "Performance with #{RUNS} runs"
|
85
|
+
puts
|
86
|
+
|
87
|
+
report "Flat output", flat_output_wo_indent, flat_output_w_indent
|
88
|
+
report "Static indent", static_wo_indent, static_w_indent
|
89
|
+
report "Dynamic indent", dynamic_wo_indent, dynamic_w_indent
|
90
|
+
report "Recursive indent", recursive_wo_indent, recursive_w_indent
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
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.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,13 +52,11 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description: "\n IndentedIO extends Kernel, IO, and StringIO
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
stacked so that each new\n indentation adds to the previous
|
61
|
-
indendation\n "
|
55
|
+
description: "\n IndentedIO extends Kernel, IO, and StringIO
|
56
|
+
with an\n #indent method that redefines #print, printf,
|
57
|
+
#puts,\n and #p to print their output indented. Indentations
|
58
|
+
\n are stacked so that each new indentation adds to the\n
|
59
|
+
\ previous indendation\n "
|
62
60
|
email:
|
63
61
|
- claus.l.rasmussen@gmail.com
|
64
62
|
executables: []
|
@@ -85,6 +83,7 @@ files:
|
|
85
83
|
- lib/indented_io/kernel.rb
|
86
84
|
- lib/indented_io/stringio.rb
|
87
85
|
- lib/indented_io/version.rb
|
86
|
+
- scripts/perf.rb
|
88
87
|
homepage: https://github.com/clrgit/indented_io
|
89
88
|
licenses:
|
90
89
|
- MIT
|