Skip to content

Commit ddfa10e

Browse files
committed
a pipe in front seems to help with ^D behaviour
1 parent 3010c50 commit ddfa10e

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ end
7878

7979
See Examples for more, like daisy-chaining, or updating a digest.
8080

81-
## Note
82-
83-
If testing with cooked interactive input at the console, note that linefeed's demo naturally reads in binary mode, so `^D` may not be instant EOF.
84-
8581
## License
8682

8783
MIT license. Copyright (c) 2026 inopinatus

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ file 'tmp/Examples.md' => ['tmp', *FileList['examples/*.rb']] do |f|
2121
# Examples
2222
2323
Contents of
24-
{inopinatus/linefeed/examples/}[https://github.com/inopinatus/linefeed/tree/main/examples],
24+
{linefeed:examples/}[https://github.com/inopinatus/linefeed/tree/main/examples],
2525
concatenated here for easy consumption.
2626
2727
HEAD

examples/demo.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
def run
1313
recipients = Demo.setup_examples
14+
input = Demo.input_pipe(ARGF)
1415
maxlen = 8192
1516
chunk = ''.b
1617

17-
while $stdin.read(maxlen, chunk)
18+
while input.read(maxlen, chunk) && !chunk.empty?
1819
recipients.each do |r|
1920
r << chunk
2021
end

examples/demo_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ def <<(obj)=puts "#{@prefix}: #{obj.inspect}"
88
def close()=puts "#{@prefix} closed."
99
end
1010

11+
# decouple from tty if demo run interactively
12+
def self.input_pipe(source)
13+
return source unless $stdin.tty?
14+
15+
reader, writer = IO.pipe
16+
Thread.new do
17+
IO.copy_stream(source, writer)
18+
ensure
19+
writer.close unless writer.closed?
20+
end
21+
reader
22+
end
23+
1124
# Example registry
1225
@example_classes = []
1326
class << self

0 commit comments

Comments
 (0)