Current section
Files
Jump to
Current section
Files
lib/jacob_cli/commands/help.ex
defmodule JacobCli.Commands.Help do
use Jacob.Command
alias Jacob.Ui.Colors, as: C
@name "help"
@desc "Show some help"
@moduledoc """
Return help informations about the current cli.
"""
def run(_) do
IO.write("""
#{Jacob.console_name() |> String.capitalize()} - Jacob #{Jacob.version()}
#{C.y("Available commands:")}
""")
Jacob.Command.Server.all() |> remove_hidden_commands() |> print_commands
IO.write("""
#{C.y("Need more help ?")}
Well, I guess you'll have to read the source ¯\\_(ツ)_/¯.
""")
end
defp print_commands(commands) do
commands
|> prepare_commands_for_print()
|> build_list
|> IO.write()
end
defp prepare_commands_for_print([]), do: []
defp prepare_commands_for_print([{_, command} | rest]) do
[{command.name, command.desc} | prepare_commands_for_print(rest)]
end
defp build_list(commands) do
build_list(commands, find_max(commands))
end
defp build_list([], _), do: ""
defp build_list([{name, desc} | rest], max) do
name = name |> String.pad_trailing(max) |> C.g()
" #{name} #{desc}\n" <> build_list(rest, max)
end
defp find_max(commands) do
Enum.reduce(commands, 0, fn {name, _}, max ->
current = String.length(name)
if current > max, do: current, else: max
end)
end
defp remove_hidden_commands(commands) do
Enum.filter(commands, fn {_, command} -> !command.hidden? end)
end
end