Skip to content

Instantly share code, notes, and snippets.

@hauleth
Created November 11, 2023 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hauleth/8f7e25223de73eb6b7373c6adae43fb3 to your computer and use it in GitHub Desktop.
Save hauleth/8f7e25223de73eb6b7373c6adae43fb3 to your computer and use it in GitHub Desktop.
defmodule Mix.Tasks.Man do
@moduledoc """
Print documentation for given Elixir term.
"""
@shortdoc "Show manual for term"
use Mix.Task
require IEx.Helpers
@impl Mix.Task
def run([]), do: Mix.shell().error("What manual you want?")
def run([string]) do
function =
if string =~ ~r/^~[A-Za-z]$/ do
"~" <> rest = string
{Kernel, :"sigil_#{rest}"}
else
{:ok, code} = Code.string_to_quoted(string)
ast = IEx.Introspection.decompose(code, __ENV__)
{data, _} = Code.eval_quoted(ast)
data
end
IEx.Introspection.h(function)
end
def run(["mix", command]) do
Mix.Task.run("help", [command])
end
def run(args) do
Mix.shell().error("Unknown term: #{join_args(args)}")
end
defp join_args(args) do
Enum.map_join(args, " ", fn arg ->
if arg =~ ~r/\s/ do
inspect(arg)
else
arg
end
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment