From fc7a19448591289c72ea34cde46c12aa6c810250 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Mon, 16 Jan 2023 11:21:59 -0700 Subject: [PATCH] wip, misc --- cmd/list.go | 2 +- pkg/mastodon/account.go | 22 +++++++++++++++++----- pkg/mastodon/{ => display}/display.go | 11 +++++++---- 3 files changed, 25 insertions(+), 10 deletions(-) rename pkg/mastodon/{ => display}/display.go (71%) diff --git a/cmd/list.go b/cmd/list.go index 82f3dc3..a7cab4d 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -51,7 +51,7 @@ var listAccountsCmd = &cobra.Command{ log.Fatal(err.Error()) } - accounts.Display() + print(accounts) }, } diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index 0a630a9..e01b018 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -67,11 +67,8 @@ type Emoji struct { Category string `json:"category"` } -// AccountCollection is a group of Accounts -type AccountCollection []Account - // Display a single Account -func (a *Account) Display() string { +func (a *Account) String() string { return strings.Join([]string{a.ID, a.Acct}, "\t") } @@ -93,15 +90,30 @@ func (a *Account) DisplayLong() (err error) { return err } +// AccountCollection is a group of Accounts +type AccountCollection []Account + // Display a collection of Accounts func (ac AccountCollection) Display() { w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) for _, value := range ac { - fmt.Fprintln(w, value.Display()) + fmt.Fprintln(w, value.String()) } w.Flush() } +func (ac AccountCollection) Len() int { + return len(ac) +} + +func (ac AccountCollection) Less(i, j int) bool { + return ac[i].ID < ac[j].ID +} + +func (ac AccountCollection) Swap(i, j int) { + ac[i], ac[j] = ac[j], ac[i] +} + // 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) { diff --git a/pkg/mastodon/display.go b/pkg/mastodon/display/display.go similarity index 71% rename from pkg/mastodon/display.go rename to pkg/mastodon/display/display.go index 9bd233f..b5c7688 100644 --- a/pkg/mastodon/display.go +++ b/pkg/mastodon/display/display.go @@ -1,4 +1,4 @@ -package mastodon +package display import ( "fmt" @@ -7,17 +7,20 @@ import ( ) // DisplayItem interface provides a means to display a single item. -type DisplayItem interface { +type Item interface { Display() string } // DisplayItemCollection provides a means to display a collection of items. -type DisplayItemCollection []DisplayItem +type ItemCollection []Item -func (c DisplayItemCollection) Display() { +func AsTable(c ItemCollection) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) for _, item := range c { fmt.Fprintln(w, item.Display()) } w.Flush() } + +func AsJSON(c ItemCollection) { +} -- 2.39.5