init commit
This commit is contained in:
+40
-13
@@ -38,7 +38,8 @@ defmodule Mix.Tasks.Clje.Repl do
|
||||
end
|
||||
|
||||
defp loop(state) do
|
||||
prompt = "clje:#{state.counter}> "
|
||||
ns = CljElixir.REPL.current_ns(state)
|
||||
prompt = "#{ns}:#{state.counter}> "
|
||||
|
||||
case read_input(prompt) do
|
||||
:eof ->
|
||||
@@ -80,10 +81,28 @@ defmodule Mix.Tasks.Clje.Repl do
|
||||
end
|
||||
|
||||
defp read_input(prompt) do
|
||||
case IO.gets(prompt) do
|
||||
:eof -> :eof
|
||||
{:error, _} -> :eof
|
||||
data -> data
|
||||
IO.write(prompt)
|
||||
read_line()
|
||||
end
|
||||
|
||||
# Read a line character-by-character, treating both \r and \n as line terminators.
|
||||
# This avoids IO.gets hanging when the terminal sends \r without \n.
|
||||
defp read_line, do: read_line([])
|
||||
|
||||
defp read_line(acc) do
|
||||
case IO.getn("", 1) do
|
||||
:eof ->
|
||||
if acc == [], do: :eof, else: acc |> Enum.reverse() |> IO.iodata_to_binary()
|
||||
|
||||
{:error, _} ->
|
||||
if acc == [], do: :eof, else: acc |> Enum.reverse() |> IO.iodata_to_binary()
|
||||
|
||||
<<c>> when c in [?\r, ?\n] ->
|
||||
IO.write("\n")
|
||||
acc |> Enum.reverse() |> IO.iodata_to_binary()
|
||||
|
||||
char ->
|
||||
read_line([char | acc])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -96,16 +115,24 @@ defmodule Mix.Tasks.Clje.Repl do
|
||||
end
|
||||
|
||||
defp read_continuation(acc) do
|
||||
case IO.gets(" ") do
|
||||
:eof -> acc
|
||||
{:error, _} -> acc
|
||||
line ->
|
||||
new_acc = acc <> "\n" <> String.trim_trailing(line, "\n")
|
||||
IO.write(" ")
|
||||
|
||||
if CljElixir.REPL.balanced?(new_acc) do
|
||||
new_acc
|
||||
case read_line() do
|
||||
:eof -> acc
|
||||
line ->
|
||||
trimmed = String.trim(line)
|
||||
|
||||
if trimmed == "" do
|
||||
# Empty Enter in continuation mode: submit what we have
|
||||
acc
|
||||
else
|
||||
read_continuation(new_acc)
|
||||
new_acc = acc <> "\n" <> trimmed
|
||||
|
||||
if CljElixir.REPL.balanced?(new_acc) do
|
||||
new_acc
|
||||
else
|
||||
read_continuation(new_acc)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user