1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2013-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19-module(cdv_term_cb).
20
21-export([get_details/2,
22	 detail_pages/0]).
23
24%% Callbacks for cdv_detail_wx
25get_details({Type, {T,Key}}, _) ->
26    [{Key,Term}] = ets:lookup(T,Key),
27    {ok,{"Expanded Term", {Type,[Term, T]}, []}}.
28
29detail_pages() ->
30    [{"Term", fun init_term_page/2}].
31
32init_term_page(ParentWin, {Type, [Term, Tab]}) ->
33    observer_lib:report_progress({ok,"Expanding term"}),
34    observer_lib:report_progress({ok,start_pulse}),
35    Expanded = expand(Term, true),
36    BinSaved = expand(Term, Tab),
37    observer_lib:report_progress({ok,stop_pulse}),
38    Cs = observer_lib:colors(ParentWin),
39    cdv_multi_wx:start_link(
40      ParentWin,
41      [{"Format \~p",cdv_html_wx,{Type, format_term_fun("~p",BinSaved,Tab,Cs)}},
42       {"Format \~tp",cdv_html_wx,{Type,format_term_fun("~tp",BinSaved,Tab,Cs)}},
43       {"Format \~w",cdv_html_wx,{Type,format_term_fun("~w",BinSaved,Tab,Cs)}},
44       {"Format \~tw",cdv_html_wx,{Type,format_term_fun("~tw",BinSaved,Tab,Cs)}},
45       {"Format \~s",cdv_html_wx,{Type,format_term_fun("~s",Expanded,Tab,Cs)}},
46       {"Format \~ts",cdv_html_wx,{Type,format_term_fun("~ts",Expanded,Tab,Cs)}}]).
47
48format_term_fun(Format,Term,Tab,Cs) ->
49    fun() ->
50            observer_lib:report_progress({ok,"Formatting term"}),
51            observer_lib:report_progress({ok,start_pulse}),
52	    try io_lib:format(Format,[Term]) of
53		Str -> {expand, plain_html(Str,Cs), Tab}
54	    catch error:badarg ->
55		    Warning = "This term cannot be formatted with " ++ Format,
56		    observer_html_lib:warning(Warning,Cs)
57            after
58                    observer_lib:report_progress({ok,stop_pulse})
59	    end
60    end.
61
62plain_html(Text,Cs) ->
63    observer_html_lib:plain_page(Text,Cs).
64
65expand(['#CDVBin',Offset,Size,Pos], true) ->
66    {ok,Bin} = crashdump_viewer:expand_binary({Offset,Size,Pos}),
67    Bin;
68expand(Bin, Tab) when is_binary(Bin), not is_boolean(Tab) ->
69    observer_lib:make_obsbin(Bin, Tab);
70expand([H|T], Expand) ->
71    case expand(T, Expand) of
72	ET when is_list(ET) ->
73	    [expand(H, Expand)|ET];
74        ET -> % The tail is an expanded binary - cannot append with |
75	    [expand(H, Expand),ET]
76    end;
77expand(Tuple, Expand) when is_tuple(Tuple) ->
78    list_to_tuple(expand(tuple_to_list(Tuple), Expand));
79expand(Term, _) ->
80    Term.
81