@@ -3,20 +3,10 @@ defmodule Pi.Eval do
33
44 alias Pi.Bridge.Info
55 alias Pi.Eval . { Evaluator , ExceptionInfo , Sandbox , Supervisor }
6+ alias Pi.Eval.Output , as: EvalOutput
67 alias Pi.Output
78 alias Pi.Protocol.Tool.Eval , as: EvalPayload
89 alias Pi.Protocol.Tool.OutputPart
9- alias Pi.Protocol.UI.Block
10- alias Pi.Protocol.UI.Display
11-
12- @ inspect_opts [ charlists: :as_lists , limit: 50 , pretty: true ]
13- @ preview_inspect_opts [
14- charlists: :as_lists ,
15- limit: 20 ,
16- pretty: false ,
17- printable_limit: 200 ,
18- width: 1_000_000
19- ]
2010
2111 def sandbox ( code , opts \\ [ ] ) when is_binary ( code ) , do: Sandbox . eval ( code , opts )
2212
@@ -138,7 +128,7 @@ defmodule Pi.Eval do
138128
139129 defp eval_with_captured_io ( code , mode ) do
140130 { { success? , result } , io } =
141- capture_io ( fn ->
131+ EvalOutput . capture_io ( fn ->
142132 try do
143133 { result , _bindings } = Code . eval_string ( code , [ arguments: [ ] ] , env ( ) )
144134 { true , result }
@@ -159,39 +149,50 @@ defmodule Pi.Eval do
159149 end
160150 end
161151
162- defp error_text ( % { text: text } ) when is_binary ( text ) , do: text
163- defp error_text ( text ) when is_binary ( text ) , do: text
164-
165- defp error_exception ( % { exception: exception } ) when is_map ( exception ) , do: exception
166- defp error_exception ( _ ) , do: nil
167-
168152 defp format_eval_result ( result , success? , io ) do
169153 case { result , success? , io } do
170- { :"do not show this result in output" , true , io } -> { :ok , io }
171- { result , false , "" } -> { :error , error_text ( result ) }
172- { result , false , io } -> { :error , "IO:\n \n #{ io } \n \n Error:\n \n #{ error_text ( result ) } " }
173- { result , true , "" } -> { :ok , inspect ( result , @ inspect_opts ) }
174- { result , true , io } -> { :ok , "IO:\n \n #{ io } \n \n Result:\n \n #{ inspect ( result , @ inspect_opts ) } " }
154+ { :"do not show this result in output" , true , io } ->
155+ { :ok , io }
156+
157+ { result , false , "" } ->
158+ { :error , EvalOutput . error_text ( result ) }
159+
160+ { result , false , io } ->
161+ { :error , "IO:\n \n #{ io } \n \n Error:\n \n #{ EvalOutput . error_text ( result ) } " }
162+
163+ { result , true , "" } ->
164+ { :ok , EvalOutput . inspect_value ( result ) }
165+
166+ { result , true , io } ->
167+ { :ok , "IO:\n \n #{ io } \n \n Result:\n \n #{ EvalOutput . inspect_value ( result ) } " }
175168 end
176169 end
177170
178171 defp structured_eval_result ( :"do not show this result in output" , true , io , { :ok , text } ) do
179172 parts = if io == "" , do: [ ] , else: [ OutputPart . text ( io ) ]
180- { :ok , % EvalPayload { io: io , result: nil , text: text , parts: parts , display: display ( parts ) } }
173+
174+ { :ok ,
175+ % EvalPayload {
176+ io: io ,
177+ result: nil ,
178+ text: text ,
179+ parts: parts ,
180+ display: EvalOutput . display ( parts )
181+ } }
181182 end
182183
183184 defp structured_eval_result ( result , true , io , { :ok , text } ) do
184185 explicit_text = Output . text_for ( result )
185- inspected = explicit_text || inspect ( result , @ inspect_opts )
186- preview = inspect ( result , @ preview_inspect_opts )
186+ inspected = explicit_text || EvalOutput . inspect_value ( result )
187+ preview = EvalOutput . preview ( result )
187188
188189 value_parts =
189190 Output . parts_for ( result ) ||
190191 [ OutputPart . inspect ( inspected , language: :elixir , title: preview ) ]
191192
192193 parts =
193194 [ ]
194- |> maybe_io_part ( io )
195+ |> EvalOutput . maybe_io_part ( io )
195196 |> Kernel . ++ ( value_parts )
196197
197198 { :ok ,
@@ -200,41 +201,27 @@ defmodule Pi.Eval do
200201 result: inspected ,
201202 text: explicit_text || text ,
202203 parts: parts ,
203- display: display ( parts )
204+ display: EvalOutput . display ( parts )
204205 } }
205206 end
206207
207208 defp structured_eval_result ( result , false , io , { :error , text } ) do
208209 parts =
209210 [ ]
210- |> maybe_io_part ( io )
211+ |> EvalOutput . maybe_io_part ( io )
211212 |> Kernel . ++ ( [ OutputPart . error ( text ) ] )
212213
213214 { :error ,
214215 % EvalPayload {
215216 io: io ,
216217 error: text ,
217- exception: error_exception ( result ) ,
218+ exception: EvalOutput . error_exception ( result ) ,
218219 text: text ,
219220 parts: parts ,
220- display: display ( parts )
221+ display: EvalOutput . display ( parts )
221222 } }
222223 end
223224
224- defp maybe_io_part ( parts , "" ) , do: parts
225- defp maybe_io_part ( parts , io ) , do: parts ++ [ OutputPart . text ( io ) ]
226-
227- defp display ( parts ) do
228- % Display { blocks: Enum . map ( parts , & part_block / 1 ) }
229- end
230-
231- defp part_block ( % OutputPart { kind: kind , body: body , language: language } ) do
232- % Block { type: block_type ( kind ) , text: body , language: language }
233- end
234-
235- defp block_type ( :code ) , do: :source
236- defp block_type ( kind ) , do: kind
237-
238225 defp normalize_names! ( name ) when is_atom ( name ) , do: [ name ]
239226
240227 defp normalize_names! ( name ) when is_binary ( name ) , do: [ String . to_existing_atom ( name ) ]
@@ -251,24 +238,6 @@ defmodule Pi.Eval do
251238 __ENV__
252239 end
253240
254- defp capture_io ( fun ) do
255- { :ok , pid } = StringIO . open ( "" )
256- original = Application . get_env ( :elixir , :ansi_enabled )
257- Application . put_env ( :elixir , :ansi_enabled , false )
258- original_gl = Process . group_leader ( )
259- Process . group_leader ( self ( ) , pid )
260-
261- try do
262- result = fun . ( )
263- { _ , content } = StringIO . contents ( pid )
264- { result , content }
265- after
266- Process . group_leader ( self ( ) , original_gl )
267- StringIO . close ( pid )
268- Application . put_env ( :elixir , :ansi_enabled , original )
269- end
270- end
271-
272241 defp endpoints do
273242 for { app , _ , _ } <- Application . started_applications ( ) ,
274243 mod <- ( Application . get_env ( app , :phoenix_endpoint ) || [ ] ) |> List . wrap ( ) do
0 commit comments