Created
February 21, 2018 19:57
-
-
Save pragdave/e11c9ed58fdee8b0e59916af17175f93 to your computer and use it in GitHub Desktop.
Code Extract: diet :"r" command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp command("r", args, state) do | |
args = if String.starts_with?(args, ":") && String.contains?(args, " ") do | |
String.replace(args, " ", ", ", global: false) | |
else | |
args | |
end | |
args = if String.contains?(args, ",") && !String.starts_with?(args, "{") do | |
"{ #{args} }" | |
else | |
args | |
end | |
try do | |
{arg_val, _} = Code.eval_string(args) | |
{ result, stepper } = Diet.Stepper.run(stepper(state), arg_val) | |
IO.puts "\nResult: #{inspect result, pretty: true}" | |
put_in(state.steppers[state.current], stepper) | |
|> interact | |
rescue | |
e in [ CompileError, SyntaxError ] -> | |
IO.puts "error: #{e.description}\n" | |
interact(state) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment