Skip to content

Commit 04e5777

Browse files
committed
WIP
1 parent dc6ddd2 commit 04e5777

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

deps/rabbit/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ endef
128128
LOCAL_DEPS = sasl os_mon inets compiler public_key crypto ssl syntax_tools xmerl
129129

130130
BUILD_DEPS = rabbitmq_cli
131-
DEPS = ranch rabbit_common amqp10_common rabbitmq_prelaunch ra sysmon_handler stdout_formatter recon redbug observer_cli osiris syslog systemd seshat khepri khepri_mnesia_migration cuttlefish gen_batch_server
131+
DEPS = ranch rabbit_common amqp10_common rabbitmq_prelaunch ra sysmon_handler stdout_formatter recon redbug observer_cli osiris syslog systemd seshat khepri khepri_mnesia_migration cuttlefish gen_batch_server host_triple terminfo
132132
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers meck proper amqp_client rabbitmq_amqp_client rabbitmq_amqp1_0
133133

134134
PLT_APPS += mnesia runtime_tools
135135

136+
dep_host_triple = hex 0.1.0
137+
dep_terminfo = git https://github.com/rabbitmq/terminfo.git master
136138
dep_syslog = git https://github.com/schlagert/syslog 4.0.0
137139

138140
define usage_xml_to_erl

deps/rabbit/src/rabbit_cli_cmd_list_exchanges.erl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
-export([cli/0]).
44

5+
-include("rabbit.hrl").
6+
57
cli() ->
68
#{commands => #{
79
"exchange" => #{
@@ -32,7 +34,27 @@ list_exchanges(Args) ->
3234
rabbit_exchange,
3335
info_all,
3436
[VHost, InfoKeys]),
35-
rabbit_cli_output:sync_notify(Ret).
37+
case is_list(Ret) of
38+
true ->
39+
rabbit_cli_output:notify(
40+
{info_table, #{keys => InfoKeys,
41+
rows => Ret,
42+
callbacks => #{
43+
info_key_to_col_title =>
44+
fun(S) -> string:titlecase(atom_to_list(S)) end,
45+
info_value_to_cell_content =>
46+
fun
47+
({name, #resource{name = <<>>}}) ->
48+
"<empty>";
49+
({name, #resource{name = Name}}) ->
50+
Name;
51+
({_, T}) ->
52+
T
53+
end
54+
}}});
55+
false ->
56+
rabbit_cli_output:notify(Ret)
57+
end.
3658

3759
get_nodename(#{node := Nodename}) ->
3860
Nodename;

deps/rabbit/src/rabbit_cli_output_console.erl

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,65 @@
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+
1325
init(_) ->
14-
{ok, undefined}.
26+
State = setup_term(#state{}),
27+
{ok, State}.
1528

1629
handle_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};
1972
handle_event(Event, State) ->
2073
io:format("~p~n", [Event]),
2174
{ok, State}.
@@ -28,3 +81,45 @@ terminate(_Arg, _State) ->
2881

2982
code_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

Comments
 (0)