]> Vexing Labs - dead-tooter.git/commitdiff
improve display handling
authorAdam Shamblin <adam@vexingworkshop.com>
Fri, 30 Dec 2022 06:34:19 +0000 (23:34 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Fri, 30 Dec 2022 06:34:19 +0000 (23:34 -0700)
cmd/account.go
cmd/list.go
pkg/mastodon/account.go
pkg/mastodon/display.go [new file with mode: 0644]
pkg/mastodon/list.go

index 325b2036ca4877899447d583ff7e7a41f380f9f5..014fd724fd65fa47a8f60cd0a79cc1cd942f0bc7 100644 (file)
@@ -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()
        },
 }
index 52a233f21551335442140c3e42b7abf71cb9ec5e..8ac42e4f22246bc1c04959cfc20ca1020ffc1e9b 100644 (file)
@@ -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()
        },
 }
index e75ba5e35dbbc5edf1da59b1256ddb30d106b94a..d91699ec3d5376dcb45fdcd3a39eec8de10173dd 100644 (file)
@@ -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 (file)
index 0000000..a7ebc0f
--- /dev/null
@@ -0,0 +1,9 @@
+package mastodon
+
+type DisplayItem interface {
+       Display() (string, error)
+}
+
+type DisplayList interface {
+       Display(items []interface{}) error
+}
index 9c1e530e376a87f43a9051819b54bf7d3becadbb..3a4a919ef321c28e1e523b944efc3567ab7769a3 100644 (file)
@@ -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 {