我如何漂亮地打印一个Erlang术语,使其可读?

vsaztqbk  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(169)

让我们假设我有这个Erlang术语:

{{badmatch,{error,enoent}},[{ts_config_http,parse_config,2,[{file,"src/tsung_controller/ts_config_http.erl"},{line,63}]},{lists,foldl,3,[{file,"lists.erl"},{line,1261}]},{ts_config,parse,2,[{file,"src/tsung_controller/ts_config.erl"},{line,437}]},{lists,foldl,3,[{file,"lists.erl"},{line,1261}]},{ts_config,handle_read,3,[{file,"src/tsung_controller/ts_config.erl"},{line,85}]},{ts_config,read,2,[{file,"src/tsung_controller/ts_config.erl"},{line,70}]},{ts_config_server,handle_call,3,[{file,"src/tsung_controller/ts_config_server.erl"},{line,206}]},{gen_server,try_handle_call,4,[{file,"gen_server.erl"},{line,607}]}]}

我如何将这个术语打印到宽度为80?

yvfmudvl

yvfmudvl1#

You can use rp :

1> f(Ds), Ds = [{beside,{beside,{beside,{text,"        something"},{beside,{text,[1,58]},{text,"       very_long"}}},{text,[1,40]}},{beside,{sep,[{beside,{text,[5,98,97,115,101,100]},{float,{text,[1,44]},0,0}},{beside,{text,[2,111,110]},{float,{text,[1,44]},0,0}},{beside,{text,[7,80,114,101,100,79,110,101]},{float,{text,[1,44]},0,0}},{text,"6\"1231231231231231313123123132sfsafsasfasdfasdfasdfsdf\""}],0,false},{float,{text,[1,41]},0,0}}},{float,{text,[7,97,110,100,97,108,115,111]},0,0},{text,[7,80,114,101,100,84,119,111]},{float,{text,[7,97,110,100,97,108,115,111]},0,0},{sep,[{text,[6,65,86,97,108,117,101]},{float,{text,[3,61,58,61]},0,0},{text,"  something"}],4,true},{float,{text,[7,97,110,100,97,108,115,111]},0,0},{above,{beside,{beside,{text,"    something"},{beside,{text,[1,58]},{text,[11,112,111,116,101,110,116,105,97,108,108,121]}}},{text,[1,40]}},{nest,4,{beside,{sep,[{above,{beside,{beside,{text,[4,118,101,114,121]},{beside,{text,[1,58]},{text,[4,108,111,110,103]}}},{text,[1,40]}},{nest,4,{beside,{sep,[{beside,{beside,{beside,{text,[4,116,104,97,116]},{beside,{text,[1,58]},{text,[5,110,101,101,100,115]}}},{text,[1,40]}},{beside,{sep,[{text," indenting"}],0,false},{float,{text,[1,41]},0,0}}}],0,false},{float,{text,[1,41]},0,0}}}}],0,false},{float,{text,[1,41]},0,0}}}}].
[{beside,{beside,{beside,{text,"        something"},
                         {beside,{text,[1,58]},{text,"       very_long"}}},
                 {text,[1,40]}},
         {beside,{sep,[{beside,{text,[5,98,97,115,101,100]},
                               {float,{text,[1,44]},0,0}},
                       {beside,{text,[2,111,110]},{float,{text,[1,44]},0,0}},
                       {beside,{text,[7,80,114,101,100,79,110,101]},
                               {float,{text,[1,44]},0,0}},
                       {text,"6\"1231231231231231313123123132sfsafsasfasdfasdfasdfsdf\""}],
                      0,false},
                 {float,{text,[1,41]},0,0}}},
 {float,{text,[7,97,110,100,97,108,115,111]},0,0},
 {text,[7,80,114,101,100,84,119,111]},
 {float,{text,[7,97,110,100,97,108,115,111]},0,0},
 {sep,[{text,[6,65,86,97,108,117,101]},
       {float,{text,[3,61,58,61]},0,0},
       {text,"  something"}],
      4,true},
 {float,{text,[7,97,110,100,97,108,115,111]},0,0},
 {above,{beside,{beside,{text,"    something"},
                        {beside,{text,[1,58]},{text,"\vpotentially"}}},
                {text,[1,40]}},
        {nest,4,
              {beside,{sep,[{above,{beside,{beside,{text,[4,118,101|...]},
                                                   {beside,{text,[...]},{text,...}}},
                                           {text,[1,40]}},
                                   {nest,4,{beside,{sep,[{...}],0,...},{float,{...},...}}}}],
                           0,false},
                      {float,{text,[1,41]},0,0}}}}]
2> rp(Ds).
[{beside,{beside,{beside,{text,"        something"},
                         {beside,{text,[1,58]},{text,"       very_long"}}},
                 {text,[1,40]}},
         {beside,{sep,[{beside,{text,[5,98,97,115,101,100]},
                               {float,{text,[1,44]},0,0}},
                       {beside,{text,[2,111,110]},{float,{text,[1,44]},0,0}},
                       {beside,{text,[7,80,114,101,100,79,110,101]},
                               {float,{text,[1,44]},0,0}},
                       {text,"6\"1231231231231231313123123132sfsafsasfasdfasdfasdfsdf\""}],
                      0,false},
                 {float,{text,[1,41]},0,0}}},
 {float,{text,[7,97,110,100,97,108,115,111]},0,0},
 {text,[7,80,114,101,100,84,119,111]},
 {float,{text,[7,97,110,100,97,108,115,111]},0,0},
 {sep,[{text,[6,65,86,97,108,117,101]},
       {float,{text,[3,61,58,61]},0,0},
       {text,"  something"}],
      4,true},
 {float,{text,[7,97,110,100,97,108,115,111]},0,0},
 {above,{beside,{beside,{text,"    something"},
                        {beside,{text,[1,58]},{text,"\vpotentially"}}},
                {text,[1,40]}},
        {nest,4,
              {beside,{sep,[{above,{beside,{beside,{text,[4,118,101,114,
                                                          121]},
                                                   {beside,{text,[1,58]},{text,[4,108,111,110,103]}}},
                                           {text,[1,40]}},
                                   {nest,4,
                                         {beside,{sep,[{beside,{beside,{beside,{text,[4,116,104,97,
                                                                                      116]},
                                                                               {beside,{text,[1,58]},{text,[5,110,101,101,100,115]}}},
                                                                       {text,[1,40]}},
                                                               {beside,{sep,[{text," indenting"}],0,false},
                                                                       {float,{text,[1,41]},0,0}}}],
                                                      0,false},
                                                 {float,{text,[1,41]},0,0}}}}],
                           0,false},
                      {float,{text,[1,41]},0,0}}}}]
ok
3>

Even more, if you combine it with rr/1 you can get nicer results…

3> rr(prettypr).
[above,beside,c_above_nest,c_beside,c_best_nest,
 c_best_nest_or,c_fit,c_float_above_nest,c_float_beside,
 c_sep_nest,c_text_beside,fit,float,nest,sep,text,union]
4> rp(Ds).
[#beside{
     d1 =
         #beside{
             d1 =
                 #beside{
                     d1 = #text{s = "        something"},
                     d2 =
                         #beside{
                             d1 = #text{s = [1,58]},
                             d2 = #text{s = "       very_long"}}},
             d2 = #text{s = [1,40]}},
     d2 =
         #beside{
             d1 =
                 #sep{
                     ds =
                         [#beside{
                              d1 = #text{s = [5,98,97,115,101,100]},
                              d2 = #float{d = #text{s = [1,44]},h = 0,v = 0}},
                          #beside{
                              d1 = #text{s = [2,111,110]},
                              d2 = #float{d = #text{s = [1,44]},h = 0,v = 0}},
                          #beside{
                              d1 = #text{s = [7,80,114,101,100,79,110,101]},
                              d2 = #float{d = #text{s = [1,44]},h = 0,v = 0}},
                          #text{
                              s = "6\"1231231231231231313123123132sfsafsasfasdfasdfasdfsdf\""}],
                     i = 0,p = false},
             d2 = #float{d = #text{s = [1,41]},h = 0,v = 0}}},
 #float{
     d = #text{s = [7,97,110,100,97,108,115,111]},
     h = 0,v = 0},
 #text{s = [7,80,114,101,100,84,119,111]},
 #float{
     d = #text{s = [7,97,110,100,97,108,115,111]},
     h = 0,v = 0},
 #sep{
     ds =
         [#text{s = [6,65,86,97,108,117,101]},
          #float{d = #text{s = [3,61,58,61]},h = 0,v = 0},
          #text{s = "  something"}],
     i = 4,p = true},
 #float{
     d = #text{s = [7,97,110,100,97,108,115,111]},
     h = 0,v = 0},
 #above{
     d1 =
         #beside{
             d1 =
                 #beside{
                     d1 = #text{s = "    something"},
                     d2 =
                         #beside{
                             d1 = #text{s = [1,58]},
                             d2 = #text{s = "\vpotentially"}}},
             d2 = #text{s = [1,40]}},
     d2 =
         #nest{
             n = 4,
             d = #beside{
                     d1 =
                         #sep{
                             ds =
                                 [#above{
                                      d1 =
                                          #beside{
                                              d1 =
                                                  #beside{
                                                      d1 = #text{s = [4,118,101,114,121]},
                                                      d2 =
                                                          #beside{
                                                              d1 = #text{s = [1,58]},
                                                              d2 = #text{s = [4,108,111,110,103]}}},
                                              d2 = #text{s = [1,40]}},
                                      d2 =
                                          #nest{
                                              n = 4,
                                              d = #beside{
                                                      d1 =
                                                          #sep{
                                                              ds =
                                                                  [#beside{
                                                                       d1 =
                                                                           #beside{
                                                                               d1 =
                                                                                   #beside{
                                                                                       d1 = #text{s = [4,116,104,97,116]},
                                                                                       d2 =
                                                                                           #beside{
                                                                                               d1 = #text{s = [1,58]},
                                                                                               d2 = #text{s = [5,110,101,101,100,115]}}},
                                                                               d2 = #text{s = [1,40]}},
                                                                       d2 =
                                                                           #beside{
                                                                               d1 = #sep{ds = [#text{s = " indenting"}],i = 0,p = false},
                                                                               d2 = #float{d = #text{s = [1,41]},h = 0,v = 0}}}],
                                                              i = 0,p = false},
                                                      d2 = #float{d = #text{s = [1,41]},h = 0,v = 0}}}}],
                             i = 0,p = false},
                     d2 = #float{d = #text{s = [1,41]},h = 0,v = 0}}}}]
ok
5>
gmol1639

gmol16392#

我会将术语保存为Term,然后使用format/1打印它。

1> Term = {{badmatch,{error,enoent}},[{ts_config_http,parse_config,2,[{file,..}
2> io:format("~80p~n", [Term]).
{{badmatch,{error,enoent}},
 [{ts_config_http,parse_config,2,
                  [{file,"src/tsung_controller/ts_config_http.erl"},
                   {line,63}]},
  {lists,foldl,3,[{file,"lists.erl"},{line,1261}]},
  {ts_config,parse,2,[{file,"src/tsung_controller/ts_config.erl"},{line,437}]},
  {lists,foldl,3,[{file,"lists.erl"},{line,1261}]},
  {ts_config,handle_read,3,
             [{file,"src/tsung_controller/ts_config.erl"},{line,85}]},
  {ts_config,read,2,[{file,"src/tsung_controller/ts_config.erl"},{line,70}]},
  {ts_config_server,handle_call,3,
                    [{file,"src/tsung_controller/ts_config_server.erl"},
                     {line,206}]},
  {gen_server,try_handle_call,4,[{file,"gen_server.erl"},{line,607}]}]}

相关问题