indented_io 0.8.0 → 0.8.5
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/.ruby-version +1 -1
- data/README.md +30 -30
- data/TODO +3 -1
- data/indented_io.gemspec +1 -1
- data/lib/indented_io.rb +1 -0
- data/lib/indented_io/indented_io.rb +11 -17
- data/lib/indented_io/indented_io_interface.rb +2 -1
- data/lib/indented_io/kernel.rb +1 -1
- data/lib/indented_io/stringio.rb +1 -0
- data/lib/indented_io/version.rb +1 -1
- data/scripts/perf.rb +11 -2
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b381c1136988e46a6a79848db05b1563a6704619775b4945e879c1efeb0dfc7a
|
4
|
+
data.tar.gz: 7296f2367c4eecb9037bf9c2f4492ee21ff3d8520c841ad95f9c06527f9cf8f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 216242b00c20a64f0a6f4394f7587bb4d663864814bfcae5ab96b76401ae9498eb0fbc9211b2aa7addbe2c2321397ad8b8cf70329eea60f8b8434346302198f3
|
7
|
+
data.tar.gz: d0dcd905b87f2c024137d3a862c1ae8ad9b345e38cec918a0e8d1a1b2a678edc66196d112d6608d98fbb94f462aae61c37d698b13fd533dc5f4125750ea2ddab
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.7.1
|
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 ">> "
|
@@ -149,12 +149,12 @@ the class define a `#write` method with the same semantics as `IO#write`
|
|
149
149
|
require 'indented_io'
|
150
150
|
class MyIO
|
151
151
|
include IndentedIO::IndentedIOInterface
|
152
|
-
def
|
152
|
+
def write(*args) ... end
|
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
@@ -1,5 +1,7 @@
|
|
1
1
|
|
2
2
|
TODO
|
3
|
+
o Find a solution for the inclusion of StringIO and Tempfile (the last is
|
4
|
+
required by ruby 2.7 - see also note in lib/indented_io/tempfile.rb
|
3
5
|
o Check if IO object is writable - no good solution ?
|
4
6
|
o better name for @this_indent ? 'string' ?
|
5
7
|
o Reimplement core loop in C
|
@@ -7,8 +9,8 @@ TODO
|
|
7
9
|
o Transmogrify instances variables and protected methods in IndentedIO to
|
8
10
|
avoid collision with names from the underlying device
|
9
11
|
o Explain name collision issues in the docs
|
10
|
-
o Remove IndentedIO#dump
|
11
12
|
|
13
|
+
+ Remove IndentedIO#dump
|
12
14
|
+ Change dependency in IndentedIOInterface from #print to #write and bump
|
13
15
|
version to 0.8 as this is the last user-visible change to be made
|
14
16
|
+ Oops. #print reimplemention doesn't do this:
|
data/indented_io.gemspec
CHANGED
@@ -37,6 +37,6 @@ Gem::Specification.new do |spec|
|
|
37
37
|
spec.require_paths = ["lib"]
|
38
38
|
|
39
39
|
spec.add_development_dependency "bundler", "~> 1.16"
|
40
|
-
spec.add_development_dependency "rake", "
|
40
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
41
41
|
spec.add_development_dependency "rspec", "~> 3.0"
|
42
42
|
end
|
data/lib/indented_io.rb
CHANGED
@@ -6,6 +6,7 @@ require 'indented_io/indented_io_interface'
|
|
6
6
|
require 'indented_io/kernel'
|
7
7
|
require 'indented_io/io'
|
8
8
|
require 'indented_io/stringio'
|
9
|
+
require 'indented_io/tempfile'
|
9
10
|
|
10
11
|
# IndentedIO module. See {IndentedIO::IndentedIO} for documentation on how to
|
11
12
|
# use this module
|
@@ -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
|
@@ -59,8 +53,8 @@ module IndentedIO
|
|
59
53
|
end
|
60
54
|
|
61
55
|
# Indent and print args to the underlying device. #p has the same semantic
|
62
|
-
# as Kernel#p. Please note that #p is
|
63
|
-
#
|
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
|
64
58
|
def p(*args)
|
65
59
|
if bol
|
66
60
|
args.each { |arg| write(arg.inspect, "\n") }
|
@@ -74,8 +68,8 @@ module IndentedIO
|
|
74
68
|
|
75
69
|
# Make IndentedIO behave like the underlying @device
|
76
70
|
# @!visibility private
|
77
|
-
def respond_to?(method)
|
78
|
-
[:indent, :level, :print, :puts, :p].include?(method) || device.respond_to?(method)
|
71
|
+
def respond_to?(method, include_all = false)
|
72
|
+
[:indent, :level, :print, :printf, :puts, :p].include?(method) || device.respond_to?(method)
|
79
73
|
end
|
80
74
|
|
81
75
|
# Make IndentedIO behave like the underlying @device
|
@@ -91,8 +85,8 @@ module IndentedIO
|
|
91
85
|
attr_reader :device
|
92
86
|
|
93
87
|
# First IndentedIO object on the stack. Equal to self if self is the first
|
94
|
-
# indentation object. #base is used to keep track of #bol for
|
95
|
-
# stack of IndentedIO objects
|
88
|
+
# indentation object. The #base object is used to keep track of #bol for
|
89
|
+
# the whole stack of IndentedIO objects
|
96
90
|
attr_reader :base
|
97
91
|
|
98
92
|
# Parent IndentedIO or IO object
|
@@ -4,6 +4,7 @@ module IndentedIO
|
|
4
4
|
# #write method like this:
|
5
5
|
#
|
6
6
|
# require 'indented_io'
|
7
|
+
#
|
7
8
|
# class MyIO
|
8
9
|
# include IndentedIO::IndentedIOInterface
|
9
10
|
# def write(*args) ... end
|
@@ -18,7 +19,7 @@ module IndentedIO
|
|
18
19
|
#
|
19
20
|
module IndentedIOInterface
|
20
21
|
# Returns a IndentedIO object that can be used for printing. The IO object
|
21
|
-
# will pass-through all
|
22
|
+
# will pass-through all methods to the underlying device except #print,
|
22
23
|
# #printf, #puts, and #p
|
23
24
|
#
|
24
25
|
# +level+ is the number of leves to indent and +string+ is the string used
|
data/lib/indented_io/kernel.rb
CHANGED
@@ -3,7 +3,7 @@ require 'indented_io/indented_io_interface'
|
|
3
3
|
module Kernel
|
4
4
|
# Like {IndentedIO::IndentedIOInterface#indent} except the underlying device is
|
5
5
|
# not the receiver (Kernel) but $stdout. Kernel#indent also allows a block without
|
6
|
-
#
|
6
|
+
# an argument. In that case it manipulates $stdout to print indented:
|
7
7
|
#
|
8
8
|
# puts "Not indented
|
9
9
|
# indent do
|
data/lib/indented_io/stringio.rb
CHANGED
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 12.3.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,8 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
|
-
|
108
|
-
rubygems_version: 2.7.3
|
107
|
+
rubygems_version: 3.1.4
|
109
108
|
signing_key:
|
110
109
|
specification_version: 4
|
111
110
|
summary: Print indented text
|