@@ -49,8 +49,11 @@ get_help(Command) ->
4949% %% Spawning and gen_server implementation
5050% %%===================================================================
5151
52+ -spec start_link () ->
53+ {ok , pid ()} | {error , {already_started , pid ()}} | {error , term ()}.
5254start_link () ->
53- gen_server :start_link ({local , ? SERVER }, ? MODULE , [], []).
55+ gen_server :start_link ({local , ? SERVER }, ? MODULE , [],
56+ [{hibernate_after , timer :seconds (160 )}]).
5457
5558-spec init (term ()) -> {ok , state ()}.
5659init (_ ) ->
@@ -65,9 +68,10 @@ handle_call({run, Command, Args}, From, State = #state{}) ->
6568 [] ->
6669 {reply , {error , command_not_found }, State };
6770 [{_ , ArgsDef , Fun , _Description }] ->
68- CommandPid = run_command (Command , Args , ArgsDef , From , Fun ),
71+ CommandPid = spawn_run_command (Command , Args , ArgsDef , From , Fun ),
6972 RunningCommands = State # state .running_commands ,
70- {noreply , State # state {running_commands = [{CommandPid , From } | RunningCommands ]}}
73+ {noreply ,
74+ State # state {running_commands = [{CommandPid , From } | RunningCommands ]}}
7175 end ;
7276handle_call ({get_help , Command }, _From , State = # state {}) ->
7377 Help = do_get_help (Command ),
@@ -84,37 +88,39 @@ handle_cast(_Request, State = #state{}) ->
8488-spec handle_info (Info :: term (), State :: term ()) ->
8589 {noreply , NewState :: term ()}.
8690handle_info ({'EXIT' , Pid , normal }, # state {running_commands = Rc } = State ) ->
87- {noreply , State # state {running_commands = lists :delete (Pid , Rc )}};
91+ {noreply , State # state {running_commands = proplists :delete (Pid , Rc )}};
8892handle_info ({'EXIT' , Pid , Reason }, # state {running_commands = Rc } = State ) ->
8993 case lists :keyfind (Pid , 1 , Rc ) of
9094 false ->
9195 ok ;
9296 {_ , From } ->
9397 gen_server :reply (From , {error , Reason })
9498 end ,
95- {noreply , State # state {running_commands = lists :delete (Pid , Rc )}}.
99+ {noreply , State # state {running_commands = proplists :delete (Pid , Rc )}}.
96100
97101% %%===================================================================
98102% %% Internal functions
99103% %%===================================================================
100-
101- run_command (Command , Args , ArgsDef , From , Fun ) ->
104+ spawn_run_command (Command , Args , ArgsDef , From , Fun ) ->
102105 spawn_link (fun () ->
103- Result =
104- case convert (ArgsDef , Args ) of
105- {ok , NewArgs } ->
106- case is_not_in_arg_def_but_set (ArgsDef , Args , " help" ) of
107- true ->
108- get_help (Command );
109- _ ->
110- MissingArguments = get_missing_arguments (ArgsDef , NewArgs ),
111- evaluate_argument_check (MissingArguments , fun () -> execute_fun (Fun , NewArgs ) end )
112- end ;
113- Else ->
114- Else
115- end ,
116- gen_server :reply (From , Result )
117- end ).
106+ CommandResult = run_command (Command , Args , ArgsDef , Fun ),
107+ gen_server :reply (From , CommandResult )
108+ end ).
109+
110+ run_command (Command , Args , ArgsDef , Fun ) ->
111+ case convert (ArgsDef , Args ) of
112+ {ok , NewArgs } ->
113+ case is_not_in_arg_def_but_set (ArgsDef , Args , " help" ) of
114+ true ->
115+ get_help (Command );
116+ _ ->
117+ MissingArguments = get_missing_arguments (ArgsDef , NewArgs ),
118+ evaluate_argument_check (MissingArguments ,
119+ fun () -> execute_fun (Fun , NewArgs ) end )
120+ end ;
121+ Else ->
122+ Else
123+ end .
118124
119125execute_fun (Fun , NewArgs ) ->
120126 try
@@ -262,22 +268,22 @@ format_command({Commands, Desc, Args}) ->
262268 [format_arg (Arg ) || Arg <- Args ]];
263269format_command ({Commands , Desc }) ->
264270 CommandStr = string :pad (string :join (Commands , " " ), 32 ),
265- cli_console_formatter :text (" ~s ~s " , [CommandStr , Desc ]).
271+ cli_console_formatter :text (" ~ts ~ts " , [CommandStr , Desc ]).
266272
267273format_arg (# argument {name = Name , optional = Optional ,
268274 default = Default , description = Desc }) ->
269275
270276 CommandStr = string :pad (Name , 30 ),
271277 ArgData = get_default_string (Default , get_optional_string (Optional , " " )),
272- cli_console_formatter :text (" -~s ~s ~n~s~s " ,
278+ cli_console_formatter :text (" -~ts ~ts ~n~ts~ts " ,
273279 [CommandStr , Desc , lists :duplicate (32 , " " ), ArgData ]).
274280
275281get_default_string (undefined , Acc ) ->
276282 Acc ;
277283get_default_string (Default , Acc ) when is_integer (Default ) ->
278- Acc ++ " Default value: " ++ io_lib :format (" ~s " , [integer_to_list (Default )]);
284+ Acc ++ " Default value: " ++ io_lib :format (" ~ts " , [integer_to_list (Default )]);
279285get_default_string (Default , Acc ) ->
280- Acc ++ " Default value: " ++ io_lib :format (" ~s " , [Default ]).
286+ Acc ++ " Default value: " ++ io_lib :format (" ~ts " , [Default ]).
281287
282288get_optional_string (true , Acc ) ->
283289 Acc ;
0 commit comments