Skip to content

Commit

Permalink
Merge pull request #13 from silbermm/fix/charlist-returning-from-read
Browse files Browse the repository at this point in the history
Properly handle chardata return from IO.read
  • Loading branch information
silbermm authored Jul 28, 2023
2 parents a8fb46f + 3c29669 commit 4c71e31
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.15.2-otp-26
erlang 26.0.1
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.1] - 2023-07-17
### Fixed
- Properly handle the charlist return from `IO.read/2`

## [0.9.0] - 2023-07-17
### Changed
- when a command is passed in that is not recognized, print help to the screen
Expand Down
2 changes: 1 addition & 1 deletion lib/prompt/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Prompt.Example.FallbackCommand do
use Prompt.Command

@impl true
def process(cmd) do
def process(_cmd) do
display("fallback command")
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/prompt/io/choice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ defmodule Prompt.IO.Choice do
case read(:stdio, :line) do
:eof -> :error
{:error, _reason} -> :error
answer -> _evaluate_choice(answer, choice)
answer when is_binary(answer) -> _evaluate_choice(answer, choice)
answer when is_list(answer) -> _evaluate_choice(IO.chardata_to_string(answer), choice)
end
end

Expand Down
9 changes: 8 additions & 1 deletion lib/prompt/io/confirm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,19 @@ defmodule Prompt.IO.Confirm do
{:error, _reason} ->
:error

answer ->
answer when is_binary(answer) ->
if confirm.mask_line do
Prompt.Position.mask_line(1)
end

evaluate_confirm(%{confirm | answer: answer})

answer when is_list(answer) ->
if confirm.mask_line do
Prompt.Position.mask_line(1)
end

evaluate_confirm(%{confirm | answer: IO.chardata_to_string(answer)})
end
end

Expand Down
9 changes: 8 additions & 1 deletion lib/prompt/io/password.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ defmodule Prompt.IO.Password do
write(IO.ANSI.reset())
:error

answer ->
answer when is_binary(answer) ->
write(IO.ANSI.reset())
String.trim(answer)

answer when is_list(answer) ->
write(IO.ANSI.reset())

answer
|> IO.chardata_to_string()
|> String.trim()
end
end

Expand Down
32 changes: 21 additions & 11 deletions lib/prompt/io/select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,29 @@ defmodule Prompt.IO.Select do
{:error, reason} ->
%Select{select | error: reason}

answer ->
answer when is_binary(answer) ->
answer
|> String.trim()
|> evaluate_choice_answer(select)
|> case do
%Select{error: err} = s when not is_nil(err) ->
s
|> show_select_error()
|> evaluate()

%Select{answer: answer} ->
answer
end
|> do_evaluate(select)

answer when is_list(answer) ->
answer
|> IO.chardata_to_string()
|> String.trim()
|> do_evaluate(select)
end
end

defp do_evaluate(answer, select) do
evaluate_choice_answer(answer, select)
|> case do
%Select{error: err} = s when not is_nil(err) ->
s
|> show_select_error()
|> evaluate()

%Select{answer: answer} ->
answer
end
end

Expand Down
26 changes: 18 additions & 8 deletions lib/prompt/io/text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ defmodule Prompt.IO.Text do
{:error, _reason} ->
:error

answer ->
answer = String.trim(answer)

case {determine_min(answer, txt), determine_max(answer, txt)} do
{false, _} -> :error_min
{true, false} -> :error_max
{true, true} -> answer
end
answer when is_binary(answer) ->
answer
|> String.trim()
|> do_evaluate(txt)

answer when is_list(answer) ->
answer
|> IO.chardata_to_string()
|> String.trim()
|> do_evaluate(txt)
end
end

defp do_evaluate(answer, txt) do
case {determine_min(answer, txt), determine_max(answer, txt)} do
{false, _} -> :error_min
{true, false} -> :error_max
{true, true} -> answer
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Prompt.MixProject do
[
app: :prompt,
description: "A terminal toolkit and a set of helpers for building console applications.",
version: "0.9.0",
version: "0.9.1",
elixir: "~> 1.10",
package: package(),
source_url: "https://github.com/silbermm/prompt",
Expand Down

0 comments on commit 4c71e31

Please sign in to comment.