indented_io 0.8.0 → 0.8.1
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 +4 -4
- data/README.md +29 -29
- data/TODO +1 -1
- data/lib/indented_io/indented_io.rb +5 -11
- data/lib/indented_io/version.rb +1 -1
- data/scripts/perf.rb +11 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c36c671ed19295dbc8816818028ce7d561f572c8aacc340c5d84236ce19a1e1e
|
4
|
+
data.tar.gz: 87b38c79f9ccce609c0a165438f62b2dd5194e1cb00df4b98623856f134019a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b04768c215fa7fd6baf00c6f99915dd43bcaa36db6bb3f8c140f0d43f2cd8b721c246f6cdb1e380e0f8bf718dbcc26961dff665a1163a1eccf5a0395113b9178
|
7
|
+
data.tar.gz: 8d689f7775d01772ece81d43cc8100d8e7e484c406e61ac84a84b0204a4708cd7436477d8af12c1c3ed3a28b8acfb6938ec24df3f7b9c8b41e9628a6ebe7396b
|
data/README.md
CHANGED
@@ -11,9 +11,9 @@ adds to the previous indendation
|
|
11
11
|
```ruby
|
12
12
|
require 'indented_io'
|
13
13
|
|
14
|
-
puts
|
15
|
-
indent { puts
|
16
|
-
indent(2,
|
14
|
+
puts 'Not indented'
|
15
|
+
indent { puts 'Indented one level' }
|
16
|
+
indent(2, '* ').puts 'Indented two levels'
|
17
17
|
```
|
18
18
|
|
19
19
|
outputs
|
@@ -32,9 +32,9 @@ If given a block, the block will be called with the IndentedIO object as
|
|
32
32
|
argument:
|
33
33
|
|
34
34
|
```ruby
|
35
|
-
$stdout.puts
|
36
|
-
$stdout.indent.puts
|
37
|
-
$stdout.indent { |f| f.puts
|
35
|
+
$stdout.puts 'Not indented'
|
36
|
+
$stdout.indent.puts 'Indented'
|
37
|
+
$stdout.indent { |f| f.puts 'Indented' }
|
38
38
|
|
39
39
|
# Not indented
|
40
40
|
# Indented
|
@@ -51,10 +51,10 @@ symbolic argument `:string`. If level is negative, the text will be outdented
|
|
51
51
|
instead:
|
52
52
|
|
53
53
|
```ruby
|
54
|
-
$stdout.puts
|
55
|
-
$stdout.indent(2,
|
56
|
-
f.indent(string:
|
57
|
-
f.indent(-1).puts
|
54
|
+
$stdout.puts 'Not indented'
|
55
|
+
$stdout.indent(2, '> ') do |f|
|
56
|
+
f.indent(string: '* ').puts 'Indented three levels'
|
57
|
+
f.indent(-1).puts 'Indented one level'
|
58
58
|
end
|
59
59
|
|
60
60
|
# Not indented
|
@@ -71,15 +71,15 @@ that `Kernel#print`, `Kernel#printf`, `Kernel#puts`, and `Kernel#p` will output
|
|
71
71
|
indented within that block:
|
72
72
|
|
73
73
|
```ruby
|
74
|
-
puts
|
74
|
+
puts 'Not indented'
|
75
75
|
indent do
|
76
|
-
puts
|
76
|
+
puts 'Indented one level'
|
77
77
|
indent do
|
78
|
-
puts
|
78
|
+
puts 'Indented two levels'
|
79
79
|
end
|
80
|
-
puts
|
80
|
+
puts 'Indented one level'
|
81
81
|
end
|
82
|
-
puts
|
82
|
+
puts 'Not indented'
|
83
83
|
|
84
84
|
# Not indented
|
85
85
|
# Indented one level
|
@@ -95,8 +95,8 @@ def legacy(phrase)
|
|
95
95
|
puts phrase
|
96
96
|
end
|
97
97
|
|
98
|
-
legacy(
|
99
|
-
indent { legacy(
|
98
|
+
legacy('Not indented')
|
99
|
+
indent { legacy('Indented' }
|
100
100
|
|
101
101
|
# Not indented
|
102
102
|
# Indented
|
@@ -111,8 +111,8 @@ that specify if the output device is at the beginning of a line and that printin
|
|
111
111
|
should start with an indentation string:
|
112
112
|
|
113
113
|
```ruby
|
114
|
-
indent(1, bol: true).puts
|
115
|
-
indent(1, bol: false).puts
|
114
|
+
indent(1, bol: true).puts 'Indented'
|
115
|
+
indent(1, bol: false).puts 'Not indented\nIndented'
|
116
116
|
|
117
117
|
# Indented
|
118
118
|
# Not indented
|
@@ -124,7 +124,7 @@ indent(1, bol: false).puts "Not indented\nIndented"
|
|
124
124
|
The default indentation string is defined in `IndentedIO`:
|
125
125
|
|
126
126
|
```ruby
|
127
|
-
IndentedIO.default_indent =
|
127
|
+
IndentedIO.default_indent = '>> '
|
128
128
|
indent.puts "Indented by #{IndentedIO.default_indent.inspect}"
|
129
129
|
|
130
130
|
# >> Indented by ">> "
|
@@ -153,8 +153,8 @@ class MyIO
|
|
153
153
|
end
|
154
154
|
|
155
155
|
my_io = MyIO.new
|
156
|
-
my_io.puts
|
157
|
-
my_io.indent.puts
|
156
|
+
my_io.puts 'Not indented'
|
157
|
+
my_io.indent.puts 'It works!'
|
158
158
|
|
159
159
|
# Not indented
|
160
160
|
# It works!
|
@@ -168,13 +168,13 @@ with a block without parameters manipulates `$stdout`, replacing it with an
|
|
168
168
|
`IndentedIO` object for the duration of the block
|
169
169
|
|
170
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
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
171
|
+
indentation mechanism processes characters one-by-one which is about 7-8 times
|
172
|
+
slower than a handwritten implementation (scripts/perf.rb is a script to check
|
173
|
+
performance). It would be much faster if the inner loop was implemented in C.
|
174
|
+
However, we're talking micro-seconds here: Printing without using IndentedIO
|
175
|
+
range from around 0.25us to 1us while using IndentedIO slows it down to between
|
176
|
+
2us and 8us, so IndentedIO won't cause a noticeable slow down of your
|
177
|
+
application unless you do a lot of output
|
178
178
|
|
179
179
|
## Installation
|
180
180
|
|
data/TODO
CHANGED
@@ -7,8 +7,8 @@ TODO
|
|
7
7
|
o Transmogrify instances variables and protected methods in IndentedIO to
|
8
8
|
avoid collision with names from the underlying device
|
9
9
|
o Explain name collision issues in the docs
|
10
|
-
o Remove IndentedIO#dump
|
11
10
|
|
11
|
+
+ Remove IndentedIO#dump
|
12
12
|
+ Change dependency in IndentedIOInterface from #print to #write and bump
|
13
13
|
version to 0.8 as this is the last user-visible change to be made
|
14
14
|
+ Oops. #print reimplemention doesn't do this:
|
@@ -23,17 +23,11 @@ module IndentedIO
|
|
23
23
|
# Indent and print args to the underlying device. #write has the same semantic
|
24
24
|
# as IO#write
|
25
25
|
def write(*args)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
n_bytes += @device.write(@combined_indent)
|
32
|
-
self.bol = false
|
33
|
-
end
|
34
|
-
n_bytes += @device.write(c)
|
35
|
-
}
|
36
|
-
n_bytes
|
26
|
+
str = args.join
|
27
|
+
return if str.empty?
|
28
|
+
s = (bol && str[0] != "\n" ? @combined_indent : "") + str.gsub(/\n([^\n])/m, "\n#{@combined_indent}\\1")
|
29
|
+
self.bol = (s[-1] == "\n")
|
30
|
+
@device.write(s)
|
37
31
|
end
|
38
32
|
|
39
33
|
# Indent and print args to the underlying device. #print has the same semantic
|
data/lib/indented_io/version.rb
CHANGED
data/scripts/perf.rb
CHANGED
@@ -7,15 +7,24 @@ RUNS = 1_000_000
|
|
7
7
|
saved_stdout = $stdout
|
8
8
|
$stdout = File.open("/dev/null", "w")
|
9
9
|
|
10
|
-
|
10
|
+
$base = 0.0
|
11
|
+
|
12
|
+
def timeit_impl(&block)
|
11
13
|
t0 = Time.now
|
12
14
|
RUNS.times {
|
13
15
|
yield
|
14
16
|
}
|
15
17
|
t1 = Time.now
|
16
|
-
|
18
|
+
t1 - t0 - $base
|
17
19
|
end
|
18
20
|
|
21
|
+
$base = timeit_impl {}
|
22
|
+
|
23
|
+
def timeit(&block)
|
24
|
+
(1000 * timeit_impl(&block)).round(0)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
19
28
|
def report(title, wo_indent, w_indent)
|
20
29
|
times = (w_indent / wo_indent).round(1)
|
21
30
|
if times < 1.1
|
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.
|
4
|
+
version: 0.8.1
|
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-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|