1010 terminate /2 ,
1111 code_change /3 ]).
1212
13+ -record (state , {
14+ setupterm_done = false :: boolean (),
15+ isatty = #{
16+ stdin => false ,
17+ stdout => false ,
18+ stderr => false
19+ } :: #{stdin => boolean (),
20+ stdout => boolean (),
21+ stderr => boolean ()},
22+ caps = #{} :: map ()
23+ }).
24+
1325init (_ ) ->
14- {ok , undefined }.
26+ State = setup_term (# state {}),
27+ {ok , State }.
1528
1629handle_call (_Request , State ) ->
1730 {ok , ok , State }.
1831
32+ handle_event (
33+ {info_table , #{keys := Keys , rows := Rows0 , callbacks := CBs }},
34+ State )
35+ when length (Keys ) > 0 ->
36+ UseColors = use_colors (stdout , State ),
37+ UseLines = use_lines (stdout , State ),
38+ Title = case UseColors of
39+ true -> #{title => true };
40+ false -> #{}
41+ end ,
42+ Bold = case UseColors of
43+ true -> #{fg => green };
44+ false -> #{}
45+ end ,
46+ Border = case UseLines of
47+ true -> #{border_drawing => ansi };
48+ false -> #{border_drawing => ascii }
49+ end ,
50+ CB1 = maps :get (info_key_to_col_title , CBs , fun (S ) -> S end ),
51+ CB2 = maps :get (info_value_to_cell_content , CBs , fun (S ) -> S end ),
52+ TableHeader = # row {cells = lists :map (
53+ CB1 ,
54+ Keys ),
55+ props = Title },
56+ Rows = lists :map (
57+ fun (Entry ) when length (Entry ) =:= length (Keys ) ->
58+ # row {
59+ cells = [
60+ % % TODO: Format using callbacks provided by
61+ % % the event emitter.
62+ # paragraph {content = CB2 (hd (Entry )),
63+ props = Bold }
64+ | lists :map (CB2 , tl (Entry ))
65+ ]
66+ }
67+ end , Rows0 ),
68+ stdout_formatter :display (
69+ # table {rows = [TableHeader | Rows ],
70+ props = Border #{cell_padding => {0 , 1 }}}),
71+ {ok , State };
1972handle_event (Event , State ) ->
2073 io :format (" ~p~n " , [Event ]),
2174 {ok , State }.
@@ -28,3 +81,45 @@ terminate(_Arg, _State) ->
2881
2982code_change (_OldVsn , State , _Extra ) ->
3083 {ok , State }.
84+
85+ setup_term (# state {setupterm_done = true } = State ) ->
86+ State ;
87+ setup_term (State ) ->
88+ StdinIsTty = terminfo :isatty (stdin ),
89+ StdoutIsTty = terminfo :isatty (stdout ),
90+ StderrIsTty = terminfo :isatty (stderr ),
91+
92+ Isatty = #{
93+ stdin => StdinIsTty ,
94+ stdout => StdoutIsTty ,
95+ stderr => StderrIsTty
96+ },
97+
98+ Caps = #{
99+ stdout => query_term_caps (stdout , StdoutIsTty ),
100+ stderr => query_term_caps (stderr , StderrIsTty )
101+ },
102+
103+ State # state {
104+ setupterm_done = true ,
105+ isatty = Isatty ,
106+ caps = Caps
107+ }.
108+
109+ query_term_caps (FdName , true ) ->
110+ terminfo :setupterm (FdName ),
111+ terminfo :query_all_caps ();
112+ query_term_caps (_FdName , false ) ->
113+ #{}.
114+
115+ use_colors (FdName , # state {isatty = Isatty , caps = Caps }) ->
116+ case maps :get (FdName , Isatty , false ) of
117+ true ->
118+ FdCaps = maps :get (FdName , Caps , #{}),
119+ maps :get (colors , FdCaps , 1 ) > 1 ;
120+ false ->
121+ false
122+ end .
123+
124+ use_lines (FdName , # state {isatty = Isatty }) ->
125+ maps :get (FdName , Isatty , false ).
0 commit comments