Skip to content

Commit

Permalink
update CLI output
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 28, 2021
1 parent 1c55f53 commit 82bf163
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ defmodule ExFactor do

defp format(%{path: nil} = struct), do: struct

defp format(%{additions: adds, changes: changes, removals: removals}) do
defp format(%{additions: adds, changes: changes, removals: removals} = output) do
%{
additions: format(adds),
changes: format(changes),
removals: format(removals)
}
output
end

defp format(list) when is_list(list) do
Expand Down
7 changes: 0 additions & 7 deletions lib/ex_factor/callers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ defmodule ExFactor.Callers do
end

def callers(mod, func, arity) do
# Code.compiler_options(parser_options: [columns: true])
# Mix.Task.rerun("compile.elixir", ["--force", "--tracer", "ExFactor.CallerTracer"])

Mix.Tasks.Xref.calls([])
|> Enum.filter(fn x ->
# {:ok, _} = Import2Alias.Server.start_link(elem(x.callee, 0))
# entries = Import2Alias.Server.entries()
# |> IO.inspect(label: "")

x.callee == {cast(mod), cast(func), arity}
end)
end
Expand Down
1 change: 1 addition & 0 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule ExFactor.Extractor do
Optional keys:
- :source_path Specify an alternate (non-standard) path for the source module
- :target_path Specify an alternate (non-standard) path for the destination module
- :dry_run Don't write any updates
"""

def emplace(opts) do
Expand Down
38 changes: 37 additions & 1 deletion lib/mix/tasks/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ defmodule Mix.Tasks.ExFactor do
- `:arity` the arity of function to move.
- `:target` is the fully-qualified destination for the removed function. If the moduel does not exist, it will be created.
Optional command line args: --source_path, --target_path, --dryrun
Optional command line args: --source_path, --target_path, --dryrun, --verbose
- `:target_path` Specify an alternate (non-standard) path for the source file.
- `:source_path` Specify an alternate (non-standard) path for the destination file.
- `:dryrun` Don't write any updates, only return the built results.
- `:verbose` (Default false) include the :state and :file_contents key values.
Example Usage:
```
Expand All @@ -42,6 +43,7 @@ defmodule Mix.Tasks.ExFactor do
strict: [
arity: :integer,
dryrun: :boolean,
verbose: :boolean,
function: :string,
key: :string,
module: :string,
Expand All @@ -54,6 +56,7 @@ defmodule Mix.Tasks.ExFactor do
parsed_opts
|> options()
|> ExFactor.refactor()
|> cli_output(parsed_opts)
end

defp options(opts) do
Expand All @@ -63,4 +66,37 @@ defmodule Mix.Tasks.ExFactor do
|> Keyword.put(:target_module, Keyword.fetch!(opts, :target))
|> Keyword.put(:dry_run, Keyword.get(opts, :dryrun, false))
end

defp cli_output(map, opts) do
verbose = Keyword.get(opts, :verbose, false)

format_entry(Map.get(map, :additions), "Additions", :light_cyan_background, verbose)
format_entry(Map.get(map, :changes), "Changes", :light_green_background, verbose)
format_entry(Map.get(map, :removals), "Removals", :light_red_background, verbose)
end

defp format_entry(entry, title, color, verbose) do
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, title])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "================================================================================"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"])]))
handle_entry(entry, color, verbose)
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, "================================================================================"])]))
IO.puts("")
end

defp handle_entry(entries, color, verbose) when is_list(entries) do
Enum.map(entries, &handle_entry(&1, color, verbose))
end

defp handle_entry(entry, color, true) do
handle_entry(entry, color , false)
IO.puts(" File contents: \n#{entry.file_contents}")
IO.puts(" State: #{inspect(entry.state)}")
end
defp handle_entry(entry, color , false) do
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " Module: #{entry.module}"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " Path: #{entry.path}"])]))
IO.puts(IO.ANSI.format([color, IO.ANSI.format([:black, " Message: #{entry.message}"])]))
end
end

0 comments on commit 82bf163

Please sign in to comment.