How could I send an EOF
/EOT
inside an input in iex
?
Say inside
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
1111
User switch command
--> j
1* {erlang,apply,[#Fun<Elixir.IEx.CLI.1.121285560>,[]]}
--> i 1
--> c 1
{:error, :interrupted}
iex(6)>
Thanks!
But this way I do not get the input.
I hope there’s a way of transmitting EOT
or EOF
.
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)
end
end
end
iex(11)> A.read
1 line
2 line
eof
"1 line\n2 line\n"
iex(12)>
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))