From d842233c9824db91b626d92101fd03611270356d Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 16 Jun 2024 21:56:24 +0100 Subject: [PATCH] checkpoint: printer prints accounts, account list and account relationships --- internal/executor/show.go | 12 ++-- internal/model/account.go | 136 ------------------------------------ internal/printer/account.go | 107 ++++++++++++++++++++++++++++ internal/printer/list.go | 4 +- 4 files changed, 115 insertions(+), 144 deletions(-) diff --git a/internal/executor/show.go b/internal/executor/show.go index c842f28..97e1bbf 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -132,7 +132,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error { return nil } - utilities.Display(account, false, "") + s.printer.PrintAccount(account) if !s.myAccount && !s.skipAccountRelationship { relationship, err := gtsClient.GetAccountRelationship(account.ID) @@ -140,7 +140,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error { return fmt.Errorf("unable to retrieve the relationship to this account: %w", err) } - utilities.Display(relationship, false, "") + s.printer.PrintAccountRelationship(relationship) } if s.myAccount && s.showUserPreferences { @@ -283,7 +283,7 @@ func (s *ShowExecutor) showFollowers(gtsClient *client.Client) error { } if len(followers.Accounts) > 0 { - utilities.Display(followers, false, "") + s.printer.PrintAccountList(followers) } else { s.printer.PrintInfo("There are no followers for this account (or the list is hidden).\n") } @@ -303,7 +303,7 @@ func (s *ShowExecutor) showFollowing(gtsClient *client.Client) error { } if len(following.Accounts) > 0 { - utilities.Display(following, false, "") + s.printer.PrintAccountList(following) } else { s.printer.PrintInfo("This account is not following anyone or the list is hidden.\n") } @@ -318,7 +318,7 @@ func (s *ShowExecutor) showBlocked(gtsClient *client.Client) error { } if len(blocked.Accounts) > 0 { - utilities.Display(blocked, false, "") + s.printer.PrintAccountList(blocked) } else { s.printer.PrintInfo("You have no blocked accounts.\n") } @@ -363,7 +363,7 @@ func (s *ShowExecutor) showFollowRequests(gtsClient *client.Client) error { } if len(accounts.Accounts) > 0 { - utilities.Display(accounts, false, "") + s.printer.PrintAccountList(accounts) } else { s.printer.PrintInfo("You have no follow requests.\n") } diff --git a/internal/model/account.go b/internal/model/account.go index ad3fe5b..6a1f520 100644 --- a/internal/model/account.go +++ b/internal/model/account.go @@ -5,10 +5,7 @@ package model import ( - "fmt" "time" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" ) type Account struct { @@ -63,59 +60,6 @@ type Field struct { VerifiedAt string `json:"verified_at"` } -func (a Account) Display(noColor bool) string { - format := ` -%s - -%s - %s - -%s - %s - -%s - %s %d - %s %d - %s %d - -%s - %s - -%s %s - -%s - %s` - - metadata := "" - - for _, field := range a.Fields { - metadata += fmt.Sprintf( - "\n %s: %s", - utilities.FieldFormat(noColor, field.Name), - utilities.ConvertHTMLToText(field.Value), - ) - } - - return fmt.Sprintf( - format, - utilities.FullDisplayNameFormat(noColor, a.DisplayName, a.Acct), - utilities.HeaderFormat(noColor, "ACCOUNT ID:"), - a.ID, - utilities.HeaderFormat(noColor, "JOINED ON:"), - utilities.FormatDate(a.CreatedAt), - utilities.HeaderFormat(noColor, "STATS:"), - utilities.FieldFormat(noColor, "Followers:"), a.FollowersCount, - utilities.FieldFormat(noColor, "Following:"), a.FollowingCount, - utilities.FieldFormat(noColor, "Statuses:"), a.StatusCount, - utilities.HeaderFormat(noColor, "BIOGRAPHY:"), - utilities.WrapLines(utilities.ConvertHTMLToText(a.Note), "\n ", 80), - utilities.HeaderFormat(noColor, "METADATA:"), - metadata, - utilities.HeaderFormat(noColor, "ACCOUNT URL:"), - a.URL, - ) -} - type AccountRelationship struct { ID string `json:"id"` PrivateNote string `json:"note"` @@ -133,53 +77,6 @@ type AccountRelationship struct { ShowingReblogs bool `json:"showing_reblogs"` } -func (a AccountRelationship) Display(noColor bool) string { - format := ` -%s - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t - %s: %t` - - privateNoteFormat := ` -%s - %s` - - output := fmt.Sprintf( - format, - utilities.HeaderFormat(noColor, "YOUR RELATIONSHIP WITH THIS ACCOUNT:"), - utilities.FieldFormat(noColor, "Following"), a.Following, - utilities.FieldFormat(noColor, "Is following you"), a.FollowedBy, - utilities.FieldFormat(noColor, "A follow request was sent and is pending"), a.FollowRequested, - utilities.FieldFormat(noColor, "Received a pending follow request"), a.FollowRequestedBy, - utilities.FieldFormat(noColor, "Endorsed"), a.Endorsed, - utilities.FieldFormat(noColor, "Showing Reposts (boosts)"), a.ShowingReblogs, - utilities.FieldFormat(noColor, "Muted"), a.Muting, - utilities.FieldFormat(noColor, "Notifications muted"), a.MutingNotifications, - utilities.FieldFormat(noColor, "Blocking"), a.Blocking, - utilities.FieldFormat(noColor, "Is blocking you"), a.BlockedBy, - utilities.FieldFormat(noColor, "Blocking account's domain"), a.DomainBlocking, - ) - - if a.PrivateNote != "" { - output += "\n" - output += fmt.Sprintf( - privateNoteFormat, - utilities.HeaderFormat(noColor, "YOUR PRIVATE NOTE ABOUT THIS ACCOUNT:"), - utilities.WrapLines(a.PrivateNote, "\n ", 80), - ) - } - - return output -} - type AccountListType int const ( @@ -193,36 +90,3 @@ type AccountList struct { Type AccountListType Accounts []Account } - -func (a AccountList) Display(noColor bool) string { - output := "\n" - - switch a.Type { - case AccountListFollowers: - output += utilities.HeaderFormat(noColor, "Followed by:") - case AccountListFollowing: - output += utilities.HeaderFormat(noColor, "Following:") - case AccountListBlockedAccount: - output += utilities.HeaderFormat(noColor, "Blocked accounts:") - case AccountListFollowRequests: - output += utilities.HeaderFormat(noColor, "Accounts that have requested to follow you:") - default: - output += utilities.HeaderFormat(noColor, "Accounts:") - } - - if a.Type == AccountListBlockedAccount { - for i := range a.Accounts { - output += fmt.Sprintf( - "\n • %s (%s)", - a.Accounts[i].Acct, - a.Accounts[i].ID, - ) - } - } else { - for i := range a.Accounts { - output += "\n • " + utilities.FullDisplayNameFormat(noColor, a.Accounts[i].DisplayName, a.Accounts[i].Acct) - } - } - - return output -} diff --git a/internal/printer/account.go b/internal/printer/account.go index b0c0dd8..31fe418 100644 --- a/internal/printer/account.go +++ b/internal/printer/account.go @@ -1 +1,108 @@ package printer + +import ( + "strconv" + "strings" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" + "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" +) + +func (p Printer) PrintAccount(account model.Account) { + var builder strings.Builder + + builder.WriteString("\n" + p.fullDisplayNameFormat(account.DisplayName, account.Acct)) + builder.WriteString("\n\n" + p.headerFormat("ACCOUNT ID:")) + builder.WriteString("\n" + account.ID) + builder.WriteString("\n\n" + p.headerFormat("JOINED ON:")) + builder.WriteString("\n" + p.formatDate(account.CreatedAt)) + builder.WriteString("\n\n" + p.headerFormat("STATS:")) + builder.WriteString("\n" + p.fieldFormat("Followers:")) + builder.WriteString(" " + strconv.Itoa(account.FollowersCount)) + builder.WriteString("\n" + p.fieldFormat("Following:")) + builder.WriteString(" " + strconv.Itoa(account.FollowingCount)) + builder.WriteString("\n" + p.fieldFormat("Statuses:")) + builder.WriteString(" " + strconv.Itoa(account.StatusCount)) + builder.WriteString("\n\n" + p.headerFormat("BIOGRAPHY:")) + builder.WriteString(utilities.WrapLines(utilities.ConvertHTMLToText(account.Note), "\n", p.maxTerminalWidth)) + builder.WriteString("\n\n" + p.headerFormat("METADATA:")) + + for _, field := range account.Fields { + builder.WriteString("\n" + p.fieldFormat(field.Name) + ": " + field.Value) + } + + builder.WriteString("\n\n" + p.headerFormat("ACCOUNT URL:")) + builder.WriteString("\n" + account.URL + "\n") + + printToStdout(builder.String()) +} + +func (p Printer) PrintAccountList(list model.AccountList) { + var builder strings.Builder + + builder.WriteString("\n") + + switch list.Type { + case model.AccountListFollowers: + builder.WriteString(p.headerFormat("Followed by:")) + case model.AccountListFollowing: + builder.WriteString(p.headerFormat("Following:")) + case model.AccountListBlockedAccount: + builder.WriteString(p.headerFormat("Blocked accounts:")) + case model.AccountListFollowRequests: + builder.WriteString(p.headerFormat("Accounts that have requested to follow you:")) + default: + builder.WriteString(p.headerFormat("Accounts:")) + } + + if list.Type == model.AccountListBlockedAccount { + for ind := range list.Accounts { + builder.WriteString("\n" + p.bullet + " " + list.Accounts[ind].Acct + " (" + list.Accounts[ind].ID + ")") + } + } else { + for ind := range list.Accounts { + builder.WriteString("\n" + p.bullet + " " + p.fullDisplayNameFormat(list.Accounts[ind].DisplayName, list.Accounts[ind].Acct)) + } + } + + builder.WriteString("\n") + + printToStdout(builder.String()) +} + +func (p Printer) PrintAccountRelationship(relationship model.AccountRelationship) { + var builder strings.Builder + + builder.WriteString("\n" + p.headerFormat("YOUR RELATIONSHIP WITH THIS ACCOUNT:")) + builder.WriteString("\n" + p.fieldFormat("Following:")) + builder.WriteString(" " + strconv.FormatBool(relationship.Following)) + builder.WriteString("\n" + p.fieldFormat("Is following you:")) + builder.WriteString(" " + strconv.FormatBool(relationship.FollowedBy)) + builder.WriteString("\n" + p.fieldFormat("A follow request was sent and is pending:")) + builder.WriteString(" " + strconv.FormatBool(relationship.FollowRequested)) + builder.WriteString("\n" + p.fieldFormat("Received a pending follow request:")) + builder.WriteString(" " + strconv.FormatBool(relationship.FollowRequestedBy)) + builder.WriteString("\n" + p.fieldFormat("Endorsed:")) + builder.WriteString(" " + strconv.FormatBool(relationship.Endorsed)) + builder.WriteString("\n" + p.fieldFormat("Showing Reposts (boosts):")) + builder.WriteString(" " + strconv.FormatBool(relationship.ShowingReblogs)) + builder.WriteString("\n" + p.fieldFormat("Muted:")) + builder.WriteString(" " + strconv.FormatBool(relationship.Muting)) + builder.WriteString("\n" + p.fieldFormat("Notifications muted:")) + builder.WriteString(" " + strconv.FormatBool(relationship.MutingNotifications)) + builder.WriteString("\n" + p.fieldFormat("Blocking:")) + builder.WriteString(" " + strconv.FormatBool(relationship.Blocking)) + builder.WriteString("\n" + p.fieldFormat("Is blocking you:")) + builder.WriteString(" " + strconv.FormatBool(relationship.BlockedBy)) + builder.WriteString("\n" + p.fieldFormat("Blocking account's domain:")) + builder.WriteString(" " + strconv.FormatBool(relationship.DomainBlocking)) + + if relationship.PrivateNote != "" { + builder.WriteString("\n\n" + p.headerFormat("YOUR PRIVATE NOTE ABOUT THIS ACCOUNT:")) + builder.WriteString("\n" + utilities.WrapLines(relationship.PrivateNote, "\n", p.maxTerminalWidth)) + } + + builder.WriteString("\n") + + printToStdout(builder.String()) +} diff --git a/internal/printer/list.go b/internal/printer/list.go index 3e21a94..2d51461 100644 --- a/internal/printer/list.go +++ b/internal/printer/list.go @@ -9,7 +9,7 @@ import ( func (p Printer) PrintList(list model.List) { var builder strings.Builder - builder.WriteString(p.headerFormat("\n" + "LIST TITLE:") + "\n") + builder.WriteString("\n" + p.headerFormat("LIST TITLE:") + "\n") builder.WriteString(list.Title + "\n\n") builder.WriteString(p.headerFormat("LIST ID:") + "\n") builder.WriteString(list.ID + "\n\n") @@ -19,7 +19,7 @@ func (p Printer) PrintList(list model.List) { if len(list.Accounts) > 0 { for acct, name := range list.Accounts { - builder.WriteString("\n" + p.bullet + p.fullDisplayNameFormat(name, acct)) + builder.WriteString("\n" + p.bullet + " " + p.fullDisplayNameFormat(name, acct)) } } else { builder.WriteString("\n" + "None")