Skip to content

Commit 63e6f24

Browse files
committed
Add IO.stream/0 and IO.binstream/0 as convenience functions
1 parent aa25920 commit 63e6f24

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

lib/elixir/lib/io.ex

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,16 @@ defmodule IO do
508508
:io.get_line(map_dev(device), to_chardata(prompt))
509509
end
510510

511+
@doc """
512+
Returns a line-based `IO.Stream` on `:stdio`.
513+
514+
This is equivalent to:
515+
516+
IO.stream(:stdio, :line)
517+
518+
"""
519+
def stream, do: stream(:stdio, :line)
520+
511521
@doc """
512522
Converts the IO `device` into an `IO.Stream`.
513523
@@ -533,12 +543,22 @@ defmodule IO do
533543
534544
"""
535545
@spec stream(device, :line | pos_integer) :: Enumerable.t()
536-
def stream(device, line_or_codepoints)
546+
def stream(device \\ :stdio, line_or_codepoints)
537547
when line_or_codepoints == :line
538548
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
539549
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
540550
end
541551

552+
@doc """
553+
Returns a raw, line-based `IO.Stream` on `:stdio`. The operation is Unicode unsafe.
554+
555+
This is equivalent to:
556+
557+
IO.binstream(:stdio, :line)
558+
559+
"""
560+
def binstream, do: binstream(:stdio, :line)
561+
542562
@doc """
543563
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
544564
@@ -547,18 +567,16 @@ defmodule IO do
547567
and write.
548568
549569
The `device` is iterated by the given number of bytes or line by line if
550-
`:line` is given.
551-
This reads from the IO device as a raw binary.
570+
`:line` is given. This reads from the IO device as a raw binary.
552571
553572
Note that an IO stream has side effects and every time
554573
you go over the stream you may get different results.
555574
556575
Finally, do not use this function on IO devices in Unicode
557576
mode as it will return the wrong result.
558-
559577
"""
560578
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
561-
def binstream(device, line_or_bytes)
579+
def binstream(device \\ :stdio, line_or_bytes)
562580
when line_or_bytes == :line
563581
when is_integer(line_or_bytes) and line_or_bytes > 0 do
564582
IO.Stream.__build__(map_dev(device), true, line_or_bytes)

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ defmodule Kernel.SpecialForms do
14161416
The `IO` module provides streams, that are both `Enumerable` and
14171417
`Collectable`, here is an upcase echo server using comprehensions:
14181418
1419-
for line <- IO.stream(:stdio, :line), into: IO.stream(:stdio, :line) do
1419+
for line <- IO.stream(), into: IO.stream() do
14201420
String.upcase(line)
14211421
end
14221422

lib/elixir/lib/system.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,12 @@ defmodule System do
897897
iex> System.shell("echo hello")
898898
{"hello\n", 0}
899899
900+
If you want to stream the devices to IO as they come:
901+
902+
iex> System.shell("echo hello", into: IO.stream())
903+
hello
904+
{%IO.Stream{}, 0}
905+
900906
## Options
901907
902908
It accepts the same options as `cmd/3`, except for `arg0`.
@@ -963,7 +969,9 @@ defmodule System do
963969
iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
964970
{"hello\n", 0}
965971
966-
iex> System.cmd("echo", ["hello"], into: IO.stream(:stdio, :line))
972+
If you want to stream the devices to IO as they come:
973+
974+
iex> System.cmd("echo", ["hello"], into: IO.stream())
967975
hello
968976
{%IO.Stream{}, 0}
969977

lib/elixir/test/elixir/io_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,9 @@ defmodule IOTest do
210210
assert capture_io(fn -> IO.inspect(1, label: "foo") end) == "foo: 1\n"
211211
assert capture_io(fn -> IO.inspect(1, label: :foo) end) == "foo: 1\n"
212212
end
213+
214+
test "stream" do
215+
assert IO.stream() == IO.stream(:stdio, :line)
216+
assert IO.binstream() == IO.binstream(:stdio, :line)
217+
end
213218
end

lib/mix/lib/mix/scm/git.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ defmodule Mix.SCM.Git do
280280

281281
defp default_into() do
282282
case Mix.shell() do
283-
Mix.Shell.IO -> IO.stream(:stdio, :line)
283+
Mix.Shell.IO -> IO.stream()
284284
_ -> ""
285285
end
286286
end

lib/mix/lib/mix/tasks/format.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ defmodule Mix.Tasks.Format do
406406
defp stdin_or_wildcard(path), do: path |> Path.expand() |> Path.wildcard(match_dot: true)
407407

408408
defp read_file(:stdin) do
409-
{IO.stream(:stdio, :line) |> Enum.to_list() |> IO.iodata_to_binary(), file: "stdin"}
409+
{IO.stream() |> Enum.to_list() |> IO.iodata_to_binary(), file: "stdin"}
410410
end
411411

412412
defp read_file(file) do

0 commit comments

Comments
 (0)