checkpoint: printer prints accounts, account list and account relationships

This commit is contained in:
Dan Anglin 2024-06-16 21:56:24 +01:00
parent 947b1b8c46
commit d842233c98
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 115 additions and 144 deletions

View file

@ -132,7 +132,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(account, false, "") s.printer.PrintAccount(account)
if !s.myAccount && !s.skipAccountRelationship { if !s.myAccount && !s.skipAccountRelationship {
relationship, err := gtsClient.GetAccountRelationship(account.ID) 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) 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 { if s.myAccount && s.showUserPreferences {
@ -283,7 +283,7 @@ func (s *ShowExecutor) showFollowers(gtsClient *client.Client) error {
} }
if len(followers.Accounts) > 0 { if len(followers.Accounts) > 0 {
utilities.Display(followers, false, "") s.printer.PrintAccountList(followers)
} else { } else {
s.printer.PrintInfo("There are no followers for this account (or the list is hidden).\n") 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 { if len(following.Accounts) > 0 {
utilities.Display(following, false, "") s.printer.PrintAccountList(following)
} else { } else {
s.printer.PrintInfo("This account is not following anyone or the list is hidden.\n") 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 { if len(blocked.Accounts) > 0 {
utilities.Display(blocked, false, "") s.printer.PrintAccountList(blocked)
} else { } else {
s.printer.PrintInfo("You have no blocked accounts.\n") 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 { if len(accounts.Accounts) > 0 {
utilities.Display(accounts, false, "") s.printer.PrintAccountList(accounts)
} else { } else {
s.printer.PrintInfo("You have no follow requests.\n") s.printer.PrintInfo("You have no follow requests.\n")
} }

View file

@ -5,10 +5,7 @@
package model package model
import ( import (
"fmt"
"time" "time"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
) )
type Account struct { type Account struct {
@ -63,59 +60,6 @@ type Field struct {
VerifiedAt string `json:"verified_at"` 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 { type AccountRelationship struct {
ID string `json:"id"` ID string `json:"id"`
PrivateNote string `json:"note"` PrivateNote string `json:"note"`
@ -133,53 +77,6 @@ type AccountRelationship struct {
ShowingReblogs bool `json:"showing_reblogs"` 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 type AccountListType int
const ( const (
@ -193,36 +90,3 @@ type AccountList struct {
Type AccountListType Type AccountListType
Accounts []Account 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
}

View file

@ -1 +1,108 @@
package printer 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())
}

View file

@ -9,7 +9,7 @@ import (
func (p Printer) PrintList(list model.List) { func (p Printer) PrintList(list model.List) {
var builder strings.Builder 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(list.Title + "\n\n")
builder.WriteString(p.headerFormat("LIST ID:") + "\n") builder.WriteString(p.headerFormat("LIST ID:") + "\n")
builder.WriteString(list.ID + "\n\n") builder.WriteString(list.ID + "\n\n")
@ -19,7 +19,7 @@ func (p Printer) PrintList(list model.List) {
if len(list.Accounts) > 0 { if len(list.Accounts) > 0 {
for acct, name := range list.Accounts { 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 { } else {
builder.WriteString("\n" + "None") builder.WriteString("\n" + "None")