From 53624e0bbf7fdac3eab8c7b3be931976eacf2ac9 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 18 Feb 2022 12:57:51 +0100 Subject: [PATCH] print issues as table and mix format. also: please not that this is my approach to Exercise: OrganizingAProject-4 --- issues/lib/issues/cli.ex | 66 ++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/issues/lib/issues/cli.ex b/issues/lib/issues/cli.ex index e9b6055..cd0c567 100644 --- a/issues/lib/issues/cli.ex +++ b/issues/lib/issues/cli.ex @@ -26,25 +26,26 @@ defmodule Issues.CLI do ) |> elem(1) |> args_to_internal_representation - end def args_to_internal_representation([user, project, count]) do - { user, project, String.to_integer(count) } + {user, project, String.to_integer(count)} end def args_to_internal_representation([user, project]) do - { user, project, @default_count } + {user, project, @default_count} end - def args_to_internal_representation(_) do # either bad arg or --help + # either bad arg or --help + def args_to_internal_representation(_) do :help end def process(:help) do - IO.puts """ + IO.puts(""" usage: issues [count | #{@default_count}] - """ + """) + System.halt(0) end @@ -57,35 +58,62 @@ defmodule Issues.CLI do end def decode_response({:ok, body}), do: body + 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) end def sort_into_descending_order(list_of_issues) do 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 def last(list, count) do list |> Enum.take(count) - |> Enum.reverse + |> Enum.reverse() end def show_issues_as_table(issues_list) do - # get the width of id-column = length(max(all_ids)) + 2 - # do the same for created_at and title + # get the width of each colum for id, 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 - |> Enum.each(fn issue -> IO.puts "#{Map.get(issue, "id")} | #{Map.get(issue, "created_at")} | #{Map.get(issue, "title")}" end) - - # list is a list of maps - # print #id | created_at | title - # print ----+------------+------ - # print issue.id | issue.created_at | issue.title - # + |> Enum.each(fn issue -> + IO.puts( + "#{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)}" + ) + end) end end