How could I send an
EOT inside an input in
iex> IO.read :all
Usually we do it by pressing ctrl+D on other shells, but that’s something else in Erlang.
If there isn’t a way, how could I stop the input in a way that I could access the data already entered?
You can use Ctrl + G to enter Job control mode, there you can interrupt the running job (e.g. with
i 1) and connect to it back.
iex(5)> IO.read :all
User switch command
--> i 1
--> c 1
But this way I do not get the input.
I hope there’s a way of transmitting
I’m unable to find any info on how to send EOF manually, so I’d guess that the next best thing is to use custom string to terminate the input.
defmodule A do
def read(acc \\ "") do
case IO.read(:stdio, :line) do
"eof\n" -> acc
line -> read(acc <> line)
"1 line\n2 line\n"
You can also just use
IO.gets, if you’re only interested in the first line.
Another way to write it:
IO.stream(:stdio, :line) |> Stream.take_while(&(&1 != "EOF\n")) |> Enum.reduce("", &(&2 <> &1))