print issues as table
and mix format. also: please not that this is my approach to Exercise: OrganizingAProject-4
This commit is contained in:
parent
2fb10a5042
commit
53624e0bbf
|
@ -26,25 +26,26 @@ defmodule Issues.CLI do
|
||||||
)
|
)
|
||||||
|> elem(1)
|
|> elem(1)
|
||||||
|> args_to_internal_representation
|
|> args_to_internal_representation
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def args_to_internal_representation([user, project, count]) do
|
def args_to_internal_representation([user, project, count]) do
|
||||||
{ user, project, String.to_integer(count) }
|
{user, project, String.to_integer(count)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def args_to_internal_representation([user, project]) do
|
def args_to_internal_representation([user, project]) do
|
||||||
{ user, project, @default_count }
|
{user, project, @default_count}
|
||||||
end
|
end
|
||||||
|
|
||||||
def args_to_internal_representation(_) do # either bad arg or --help
|
# either bad arg or --help
|
||||||
|
def args_to_internal_representation(_) do
|
||||||
:help
|
:help
|
||||||
end
|
end
|
||||||
|
|
||||||
def process(:help) do
|
def process(:help) do
|
||||||
IO.puts """
|
IO.puts("""
|
||||||
usage: issues <user> <project> [count | #{@default_count}]
|
usage: issues <user> <project> [count | #{@default_count}]
|
||||||
"""
|
""")
|
||||||
|
|
||||||
System.halt(0)
|
System.halt(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,35 +58,62 @@ defmodule Issues.CLI do
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_response({:ok, body}), do: body
|
def decode_response({:ok, body}), do: body
|
||||||
|
|
||||||
def decode_response({:error, error}) do
|
def decode_response({:error, error}) do
|
||||||
IO.puts "Error fetching from Github: #{error["message"]}"
|
IO.puts("Error fetching from Github: #{error["message"]}")
|
||||||
System.halt(2)
|
System.halt(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_into_descending_order(list_of_issues) do
|
def sort_into_descending_order(list_of_issues) do
|
||||||
list_of_issues
|
list_of_issues
|
||||||
|> Enum.sort(fn lhs, rhs -> Map.get(lhs, "created_at") >= Map.get(rhs, "created_at") end)
|
|> Enum.sort(fn lhs, rhs -> Map.get(lhs, "created_at") >= Map.get(rhs, "created_at") end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last(list, count) do
|
def last(list, count) do
|
||||||
list
|
list
|
||||||
|> Enum.take(count)
|
|> Enum.take(count)
|
||||||
|> Enum.reverse
|
|> Enum.reverse()
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_issues_as_table(issues_list) do
|
def show_issues_as_table(issues_list) do
|
||||||
# get the width of id-column = length(max(all_ids)) + 2
|
# get the width of each colum for id, created at and title
|
||||||
# do the same for created_at and title
|
longest_id =
|
||||||
|
issues_list
|
||||||
|
|> Enum.map(&Map.get(&1, "id"))
|
||||||
|
|> Enum.max_by(&(String.length("#{&1}") + 2))
|
||||||
|
|
||||||
IO.puts "#id | created_at | title"
|
id_column_width = String.length(Integer.to_string(longest_id))
|
||||||
|
|
||||||
|
longest_creation_date =
|
||||||
|
issues_list
|
||||||
|
|> Enum.map(&Map.get(&1, "created_at"))
|
||||||
|
|> Enum.max_by(&(String.length(&1) + 2))
|
||||||
|
|
||||||
|
creation_date_width = String.length(longest_creation_date)
|
||||||
|
|
||||||
|
longest_title =
|
||||||
|
issues_list
|
||||||
|
|> Enum.map(&Map.get(&1, "title"))
|
||||||
|
|> Enum.max_by(&(String.length(&1) + 2))
|
||||||
|
|
||||||
|
title_length = String.length(longest_title)
|
||||||
|
|
||||||
|
# print the title
|
||||||
|
IO.puts(
|
||||||
|
"#{String.pad_trailing("#id", id_column_width)} | #{String.pad_trailing("created_at", creation_date_width)} | #{String.pad_trailing("title", title_length)} "
|
||||||
|
)
|
||||||
|
|
||||||
|
# print seperator
|
||||||
|
IO.puts(
|
||||||
|
"#{String.pad_trailing("-", id_column_width, "-")}-+-#{String.pad_trailing("-", creation_date_width, "-")}-+-#{String.pad_trailing("-", title_length, "-")}-"
|
||||||
|
)
|
||||||
|
|
||||||
|
# print each issue
|
||||||
issues_list
|
issues_list
|
||||||
|> Enum.each(fn issue -> IO.puts "#{Map.get(issue, "id")} | #{Map.get(issue, "created_at")} | #{Map.get(issue, "title")}" end)
|
|> Enum.each(fn issue ->
|
||||||
|
IO.puts(
|
||||||
# list is a list of maps
|
"#{String.pad_trailing(Integer.to_string(Map.get(issue, "id")), id_column_width)} | #{String.pad_trailing(Map.get(issue, "created_at"), creation_date_width)} | #{String.pad_trailing(Map.get(issue, "title"), title_length)}"
|
||||||
# print #id | created_at | title
|
)
|
||||||
# print ----+------------+------
|
end)
|
||||||
# print issue.id | issue.created_at | issue.title
|
|
||||||
#
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue