Add option --limit (-l) to limit the number of displayed results
authorMikael Berthe <mikael@lilotux.net>
Sun, 23 Apr 2017 23:32:16 +0200
changeset 13 f862af8faf17
parent 12 e94c9ed9b1c8
child 14 da2059f2fa6a
Add option --limit (-l) to limit the number of displayed results
cmd/accounts.go
cmd/notifications.go
cmd/search.go
cmd/status.go
cmd/timelines.go
--- a/cmd/accounts.go	Sun Apr 23 22:40:52 2017 +0200
+++ b/cmd/accounts.go	Sun Apr 23 23:32:16 2017 +0200
@@ -17,8 +17,8 @@
 
 var accountsOpts struct {
 	accountID                 int
-	unset                     bool // TODO remove eventually?
-	limit                     int
+	unset                     bool   // TODO remove eventually?
+	limit                     uint   // Limit the number of results
 	onlyMedia, excludeReplies bool   // For acccount statuses
 	remoteUID                 string // For account follow
 	acceptFR, rejectFR        bool   // For account follow_requests
@@ -41,7 +41,7 @@
 
 	// Global flags
 	accountsCmd.PersistentFlags().IntVarP(&accountsOpts.accountID, "account-id", "a", 0, "Account ID number")
-	//accountsCmd.PersistentFlags().IntVarP(&accountsOpts.limit, "limit", "l", 0, "Limit number of results")
+	accountsCmd.PersistentFlags().UintVarP(&accountsOpts.limit, "limit", "l", 0, "Limit number of results")
 
 	// Subcommand flags
 	accountStatusesSubcommand.Flags().BoolVar(&accountsOpts.onlyMedia, "only-media", false, "Only statuses with media attachments")
@@ -311,20 +311,28 @@
 		obj = account
 	case "search":
 		var accountList []madon.Account
-		limit := 0 // TODO use a global flag
-		accountList, err = gClient.SearchAccounts(strings.Join(args, " "), limit)
+		accountList, err = gClient.SearchAccounts(strings.Join(args, " "), int(opt.limit))
 		obj = accountList
 	case "followers":
 		var accountList []madon.Account
 		accountList, err = gClient.GetAccountFollowers(opt.accountID)
+		if opt.limit > 0 {
+			accountList = accountList[:opt.limit]
+		}
 		obj = accountList
 	case "following":
 		var accountList []madon.Account
 		accountList, err = gClient.GetAccountFollowing(opt.accountID)
+		if opt.limit > 0 {
+			accountList = accountList[:opt.limit]
+		}
 		obj = accountList
 	case "statuses":
 		var statusList []madon.Status
 		statusList, err = gClient.GetAccountStatuses(opt.accountID, opt.onlyMedia, opt.excludeReplies)
+		if opt.limit > 0 {
+			statusList = statusList[:opt.limit]
+		}
 		obj = statusList
 	case "follow":
 		if opt.unset {
@@ -342,6 +350,9 @@
 		if opt.list {
 			var followRequests []madon.Account
 			followRequests, err = gClient.GetAccountFollowRequests()
+			if opt.limit > 0 {
+				followRequests = followRequests[:opt.limit]
+			}
 			obj = followRequests
 		} else {
 			err = gClient.FollowRequestAuthorize(opt.accountID, !opt.rejectFR)
@@ -361,14 +372,23 @@
 	case "favourites":
 		var statusList []madon.Status
 		statusList, err = gClient.GetFavourites()
+		if opt.limit > 0 {
+			statusList = statusList[:opt.limit]
+		}
 		obj = statusList
 	case "blocks":
 		var accountList []madon.Account
 		accountList, err = gClient.GetBlockedAccounts()
+		if opt.limit > 0 {
+			accountList = accountList[:opt.limit]
+		}
 		obj = accountList
 	case "mutes":
 		var accountList []madon.Account
 		accountList, err = gClient.GetMutedAccounts()
+		if opt.limit > 0 {
+			accountList = accountList[:opt.limit]
+		}
 		obj = accountList
 	case "relationships":
 		var ids []int
@@ -389,6 +409,9 @@
 		if opt.list {
 			var reports []madon.Report
 			reports, err = gClient.GetReports()
+			if opt.limit > 0 {
+				reports = reports[:opt.limit]
+			}
 			obj = reports
 			break
 		}
--- a/cmd/notifications.go	Sun Apr 23 22:40:52 2017 +0200
+++ b/cmd/notifications.go	Sun Apr 23 23:32:16 2017 +0200
@@ -9,6 +9,8 @@
 	"errors"
 
 	"github.com/spf13/cobra"
+
+	"github.com/McKael/madon"
 )
 
 var notificationsOpts struct {
@@ -51,7 +53,12 @@
 	var err error
 
 	if opt.list {
-		obj, err = gClient.GetNotifications()
+		var notifications []madon.Notification
+		notifications, err = gClient.GetNotifications()
+		if accountsOpts.limit > 0 {
+			notifications = notifications[:accountsOpts.limit]
+		}
+		obj = notifications
 	} else if opt.notifID > 0 {
 		obj, err = gClient.GetNotification(opt.notifID)
 	}
--- a/cmd/search.go	Sun Apr 23 22:40:52 2017 +0200
+++ b/cmd/search.go	Sun Apr 23 23:32:16 2017 +0200
@@ -14,6 +14,7 @@
 
 var searchOpts struct {
 	resolve bool
+	//limit   uint
 }
 
 // searchCmd represents the search command
@@ -28,6 +29,7 @@
 	RootCmd.AddCommand(searchCmd)
 
 	searchCmd.Flags().BoolVar(&searchOpts.resolve, "resolve", false, "Resolve non-local accounts")
+	//searchCmd.Flags().UintVarP(&searchOpts.limit, "limit", "l", 0, "Limit number of results")
 }
 
 func searchRunE(cmd *cobra.Command, args []string) error {
--- a/cmd/status.go	Sun Apr 23 22:40:52 2017 +0200
+++ b/cmd/status.go	Sun Apr 23 23:32:16 2017 +0200
@@ -26,8 +26,8 @@
 	filePath    string
 	mediaIDs    string
 
-	// TODO
-	limit int
+	// Used for several subcommands to limit the number of results
+	limit uint
 }
 
 func init() {
@@ -38,7 +38,7 @@
 
 	// Global flags
 	statusCmd.PersistentFlags().IntVarP(&statusOpts.statusID, "status-id", "s", 0, "Status ID number")
-	//statusCmd.PersistentFlags().IntVarP(&statusOpts.limit, "limit", "l", 0, "Limit number of results")
+	statusCmd.PersistentFlags().UintVarP(&statusOpts.limit, "limit", "l", 0, "Limit number of results")
 
 	statusCmd.MarkPersistentFlagRequired("status-id")
 
@@ -175,10 +175,16 @@
 	case "reblogged-by":
 		var accountList []madon.Account
 		accountList, err = gClient.GetStatusRebloggedBy(opt.statusID)
+		if opt.limit > 0 {
+			accountList = accountList[:opt.limit]
+		}
 		obj = accountList
 	case "favourited-by":
 		var accountList []madon.Account
 		accountList, err = gClient.GetStatusFavouritedBy(opt.statusID)
+		if opt.limit > 0 {
+			accountList = accountList[:opt.limit]
+		}
 		obj = accountList
 	case "delete":
 		err = gClient.DeleteStatus(opt.statusID)
--- a/cmd/timelines.go	Sun Apr 23 22:40:52 2017 +0200
+++ b/cmd/timelines.go	Sun Apr 23 23:32:16 2017 +0200
@@ -11,6 +11,7 @@
 
 var timelineOpts struct {
 	local bool
+	limit uint
 }
 
 // timelineCmd represents the timelines command
@@ -33,6 +34,7 @@
 	RootCmd.AddCommand(timelineCmd)
 
 	timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance")
+	timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of results")
 }
 
 func timelineRunE(cmd *cobra.Command, args []string) error {
@@ -54,6 +56,10 @@
 		return nil
 	}
 
+	if opt.limit > 0 {
+		sl = sl[:opt.limit]
+	}
+
 	p, err := getPrinter()
 	if err != nil {
 		return err