From 18ef56725d97d501b2dd9a903254466d906f7d07 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Thu, 29 Dec 2022 23:34:19 -0700 Subject: [PATCH] improve display handling --- cmd/account.go | 22 +++------------------- cmd/list.go | 4 ++-- pkg/mastodon/account.go | 26 +++++++++++++++++++++++--- pkg/mastodon/display.go | 9 +++++++++ pkg/mastodon/list.go | 25 +++++++++++++++++++++++-- 5 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 pkg/mastodon/display.go diff --git a/cmd/account.go b/cmd/account.go index 325b203..014fd72 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -2,9 +2,6 @@ package tooter import ( "fmt" - "os" - "strings" - "text/tabwriter" "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon" "github.com/spf13/cobra" @@ -75,11 +72,7 @@ var getFollowersCmd = &cobra.Command{ panic(err.Error()) } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) - for _, value := range followers { - fmt.Fprintln(w, strings.Join([]string{value.ID, value.Acct}, "\t")) - } - w.Flush() + followers.Display() }, } @@ -101,11 +94,7 @@ var getFollowingCmd = &cobra.Command{ panic(err.Error()) } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) - for _, value := range following { - fmt.Fprintln(w, strings.Join([]string{value.ID, value.Acct}, "\t")) - } - w.Flush() + following.Display() }, } @@ -127,11 +116,6 @@ var getListsCmd = &cobra.Command{ panic(err.Error()) } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) - for _, value := range lists { - fmt.Fprintln(w, strings.Join([]string{ - value.ID, value.Title, value.RepliesPolicy}, "\t")) - } - w.Flush() + lists.Display() }, } diff --git a/cmd/list.go b/cmd/list.go index 52a233f..8ac42e4 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -52,7 +52,7 @@ var listAccountsCmd = &cobra.Command{ panic(err.Error()) } - fmt.Printf("%+v", accounts) + accounts.Display() }, } @@ -82,6 +82,6 @@ var listsContainingCmd = &cobra.Command{ panic(err.Error()) } - fmt.Printf("%+v", lists) + lists.Display() }, } diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index e75ba5e..d91699e 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -7,7 +7,10 @@ import ( "io" "net/http" "net/url" + "os" "os/exec" + "strings" + "text/tabwriter" "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api" ) @@ -62,6 +65,23 @@ type Emoji struct { VisibleInPicker bool `json:"visible_in_picker"` } +// AccountCollection is a group of Accounts +type AccountCollection []Account + +// Display a single Account +func (a *Account) Display() string { + return strings.Join([]string{a.ID, a.Acct}, "\t") +} + +// Display a collection of Accounts +func (aa AccountCollection) Display() { + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) + for _, value := range aa { + fmt.Fprintln(w, value.Display()) + } + w.Flush() +} + // Authorize opens the default browser to initiate the authorization flow // for the current user. func (a *Account) Authorize(host string, app Application) (code string, err error) { @@ -191,7 +211,7 @@ func (a *Account) Get(host string, token api.Token) (account Account, err error) // GetFollowers returns a list of all accounts following the logged in user func (a *Account) GetFollowers( - host string, token api.Token) (followers []Account, err error) { + host string, token api.Token) (followers AccountCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "followers") @@ -220,7 +240,7 @@ func (a *Account) GetFollowers( // GetFollowing returns a list of all accounts followed by the logged in user func (a *Account) GetFollowing( - host string, token api.Token) (following []Account, err error) { + host string, token api.Token) (following AccountCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "following") @@ -248,7 +268,7 @@ func (a *Account) GetFollowing( } // GetLists fetches all lists the user owns -func (a *Account) GetLists(host string, token api.Token) (lists []List, err error) { +func (a *Account) GetLists(host string, token api.Token) (lists ListCollection, err error) { u := url.URL{ Host: host, Path: "api/v1/lists", diff --git a/pkg/mastodon/display.go b/pkg/mastodon/display.go new file mode 100644 index 0000000..a7ebc0f --- /dev/null +++ b/pkg/mastodon/display.go @@ -0,0 +1,9 @@ +package mastodon + +type DisplayItem interface { + Display() (string, error) +} + +type DisplayList interface { + Display(items []interface{}) error +} diff --git a/pkg/mastodon/list.go b/pkg/mastodon/list.go index 9c1e530..3a4a919 100644 --- a/pkg/mastodon/list.go +++ b/pkg/mastodon/list.go @@ -2,7 +2,11 @@ package mastodon import ( "encoding/json" + "fmt" "net/url" + "os" + "strings" + "text/tabwriter" "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api" ) @@ -14,6 +18,23 @@ type List struct { RepliesPolicy string `json:"replies_policy"` } +// ListCollection is a group of Lists, usually associated with an account. +type ListCollection []List + +// Display a single List +func (l *List) Display() string { + return strings.Join([]string{l.ID, l.Title, l.RepliesPolicy}, "\t") +} + +// Display a collection of Lists +func (ll ListCollection) Display() { + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) + for _, value := range ll { + fmt.Fprintln(w, value.Display()) + } + w.Flush() +} + // GetList returns a list specified by id. func GetList(ID string, host string, token api.Token) (list List, err error) { id := url.PathEscape(ID) @@ -43,7 +64,7 @@ func GetList(ID string, host string, token api.Token) (list List, err error) { // ListsContainingAccount returns lists containing this account. func ListsContainingAccount( - accountID string, host string, token api.Token) (lists []List, err error) { + accountID string, host string, token api.Token) (lists ListCollection, err error) { id := url.PathEscape(accountID) path, err := url.JoinPath("api/v1/accounts", id, "lists") @@ -71,7 +92,7 @@ func ListsContainingAccount( } // GetAccounts returns the accounts associated with a list. -func (l *List) GetAccounts(host string, token api.Token) (accounts []Account, err error) { +func (l *List) GetAccounts(host string, token api.Token) (accounts AccountCollection, err error) { id := url.PathEscape(l.ID) path, err := url.JoinPath("api/v1/lists", id, "accounts") if err != nil { -- 2.39.5