feat: add pager support

This commit is contained in:
Dan Anglin 2024-06-08 19:00:51 +01:00
parent 9c8476fa97
commit 24bd593dd9
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
7 changed files with 61 additions and 20 deletions

View file

@ -65,6 +65,7 @@ func run() error {
topLevelFlags := executor.TopLevelFlags{ topLevelFlags := executor.TopLevelFlags{
ConfigDir: "", ConfigDir: "",
NoColor: nil, NoColor: nil,
Pager: "",
} }
flag.StringVar(&topLevelFlags.ConfigDir, "config-dir", "", "specify your config directory") flag.StringVar(&topLevelFlags.ConfigDir, "config-dir", "", "specify your config directory")
@ -79,6 +80,7 @@ func run() error {
return nil return nil
}) })
flag.StringVar(&topLevelFlags.Pager, "pager", "", "specify your preferred pager if you would like pager support")
flag.Usage = usageFunc(commandSummaries) flag.Usage = usageFunc(commandSummaries)

View file

@ -116,7 +116,7 @@ func (c *CreateExecutor) createList(gtsClient *client.Client) error {
} }
fmt.Println("Successfully created the following list:") fmt.Println("Successfully created the following list:")
utilities.Display(list, *c.topLevelFlags.NoColor) utilities.Display(list, *c.topLevelFlags.NoColor, c.topLevelFlags.Pager)
return nil return nil
} }
@ -197,7 +197,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
} }
fmt.Println("Successfully created the following status:") fmt.Println("Successfully created the following status:")
utilities.Display(status, *c.topLevelFlags.NoColor) utilities.Display(status, *c.topLevelFlags.NoColor, c.topLevelFlags.Pager)
return nil return nil
} }

View file

@ -90,7 +90,7 @@ func (e *EditExecutor) editList(gtsClient *client.Client) error {
} }
fmt.Println("Successfully updated the list.") fmt.Println("Successfully updated the list.")
utilities.Display(updatedList, *e.topLevelFlags.NoColor) utilities.Display(updatedList, *e.topLevelFlags.NoColor, e.topLevelFlags.Pager)
return nil return nil
} }

View file

@ -23,4 +23,5 @@ func (a *AccountNames) Set(value string) error {
type TopLevelFlags struct { type TopLevelFlags struct {
ConfigDir string ConfigDir string
NoColor *bool NoColor *bool
Pager string
} }

View file

@ -90,7 +90,7 @@ func (s *ShowExecutor) showInstance(gtsClient *client.Client) error {
return fmt.Errorf("unable to retrieve the instance details: %w", err) return fmt.Errorf("unable to retrieve the instance details: %w", err)
} }
utilities.Display(instance, *s.topLevelFlags.NoColor) utilities.Display(instance, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
return nil return nil
} }
@ -123,7 +123,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(account, *s.topLevelFlags.NoColor) utilities.Display(account, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
if !s.myAccount && !s.skipAccountRelationship { if !s.myAccount && !s.skipAccountRelationship {
relationship, err := gtsClient.GetAccountRelationship(account.ID) relationship, err := gtsClient.GetAccountRelationship(account.ID)
@ -131,7 +131,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, *s.topLevelFlags.NoColor) utilities.Display(relationship, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} }
if s.myAccount && s.showUserPreferences { if s.myAccount && s.showUserPreferences {
@ -140,7 +140,7 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
return fmt.Errorf("unable to retrieve the user preferences: %w", err) return fmt.Errorf("unable to retrieve the user preferences: %w", err)
} }
utilities.Display(preferences, *s.topLevelFlags.NoColor) utilities.Display(preferences, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} }
return nil return nil
@ -162,7 +162,7 @@ func (s *ShowExecutor) showStatus(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(status, *s.topLevelFlags.NoColor) utilities.Display(status, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
return nil return nil
} }
@ -211,7 +211,7 @@ func (s *ShowExecutor) showTimeline(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(timeline, *s.topLevelFlags.NoColor) utilities.Display(timeline, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
return nil return nil
} }
@ -240,7 +240,7 @@ func (s *ShowExecutor) showList(gtsClient *client.Client) error {
list.Accounts = accountMap list.Accounts = accountMap
} }
utilities.Display(list, *s.topLevelFlags.NoColor) utilities.Display(list, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
return nil return nil
} }
@ -257,7 +257,7 @@ func (s *ShowExecutor) showLists(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(lists, *s.topLevelFlags.NoColor) utilities.Display(lists, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
return nil return nil
} }
@ -274,7 +274,7 @@ func (s *ShowExecutor) showFollowers(gtsClient *client.Client) error {
} }
if len(followers.Accounts) > 0 { if len(followers.Accounts) > 0 {
utilities.Display(followers, *s.topLevelFlags.NoColor) utilities.Display(followers, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} else { } else {
fmt.Println("There are no followers for this account or the list is hidden.") fmt.Println("There are no followers for this account or the list is hidden.")
} }
@ -294,7 +294,7 @@ func (s *ShowExecutor) showFollowing(gtsClient *client.Client) error {
} }
if len(following.Accounts) > 0 { if len(following.Accounts) > 0 {
utilities.Display(following, *s.topLevelFlags.NoColor) utilities.Display(following, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} else { } else {
fmt.Println("This account is not following anyone or the list is hidden.") fmt.Println("This account is not following anyone or the list is hidden.")
} }
@ -309,7 +309,7 @@ func (s *ShowExecutor) showBlocked(gtsClient *client.Client) error {
} }
if len(blocked.Accounts) > 0 { if len(blocked.Accounts) > 0 {
utilities.Display(blocked, *s.topLevelFlags.NoColor) utilities.Display(blocked, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} else { } else {
fmt.Println("You have no blocked accounts.") fmt.Println("You have no blocked accounts.")
} }
@ -324,7 +324,7 @@ func (s *ShowExecutor) showBookmarks(gtsClient *client.Client) error {
} }
if len(bookmarks.Statuses) > 0 { if len(bookmarks.Statuses) > 0 {
utilities.Display(bookmarks, *s.topLevelFlags.NoColor) utilities.Display(bookmarks, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} else { } else {
fmt.Println("You have no bookmarks.") fmt.Println("You have no bookmarks.")
} }
@ -339,7 +339,7 @@ func (s *ShowExecutor) showLiked(gtsClient *client.Client) error {
} }
if len(liked.Statuses) > 0 { if len(liked.Statuses) > 0 {
utilities.Display(liked, *s.topLevelFlags.NoColor) utilities.Display(liked, *s.topLevelFlags.NoColor, s.topLevelFlags.Pager)
} else { } else {
fmt.Printf("You have no %s statuses.\n", s.resourceType) fmt.Printf("You have no %s statuses.\n", s.resourceType)
} }

View file

@ -209,7 +209,7 @@ type StatusList struct {
func (s StatusList) Display(noColor bool) string { func (s StatusList) Display(noColor bool) string {
var builder strings.Builder var builder strings.Builder
separator := "────────────────────────────────────────────────────────────────────────────────" separator := strings.Repeat("─", 80)
builder.WriteString(utilities.HeaderFormat(noColor, s.Name) + "\n") builder.WriteString(utilities.HeaderFormat(noColor, s.Name) + "\n")

View file

@ -4,12 +4,50 @@
package utilities package utilities
import "os" import (
"fmt"
"os"
"os/exec"
"strings"
)
type Displayer interface { type Displayer interface {
Display(noColor bool) string Display(noColor bool) string
} }
func Display(d Displayer, noColor bool) { func Display(displayer Displayer, noColor bool, pagerCommand string) {
os.Stdout.WriteString(d.Display(noColor) + "\n") if pagerCommand == "" {
os.Stdout.WriteString(displayer.Display(noColor) + "\n")
return
}
strArr := strings.Split(pagerCommand, " ")
pager := new(exec.Cmd)
if len(strArr) == 1 {
pager = exec.Command(strArr[0]) //nolint:gosec
} else {
pager = exec.Command(strArr[0], strArr[1:]...) //nolint:gosec
}
pipe, err := pager.StdinPipe()
if err != nil {
os.Stdout.WriteString(displayer.Display(noColor) + "\n")
return
}
pager.Stdout = os.Stdout
pager.Stderr = os.Stderr
_ = pager.Start()
defer func() {
_ = pipe.Close()
_ = pager.Wait()
}()
fmt.Fprintln(pipe, displayer.Display(noColor)+"\n")
} }