author | Mikael Berthe <mikael@lilotux.net> |
Fri, 28 Apr 2017 15:39:32 +0200 | |
changeset 20 | b0ccc09f07a2 |
parent 13 | f862af8faf17 |
child 22 | 5778b09bc6fe |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"github.com/spf13/cobra" |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
10 |
|
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
11 |
"github.com/McKael/madon" |
0 | 12 |
) |
13 |
||
14 |
var timelineOpts struct { |
|
15 |
local bool |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
16 |
limit uint |
0 | 17 |
} |
18 |
||
19 |
// timelineCmd represents the timelines command |
|
20 |
var timelineCmd = &cobra.Command{ |
|
21 |
Use: "timeline [home|public|:HASHTAG] [--local]", |
|
22 |
Aliases: []string{"tl"}, |
|
23 |
Short: "Fetch a timeline", |
|
24 |
Long: ` |
|
25 |
The timeline command fetches a timeline (home, local or federated). |
|
26 |
It can also get a hashtag-based timeline if the keyword or prefixed with |
|
27 |
':' or '#'.`, |
|
28 |
Example: ` madonctl timeline |
|
29 |
madonctl timeline public --local |
|
30 |
madonctl timeline :mastodon`, |
|
31 |
RunE: timelineRunE, |
|
32 |
ValidArgs: []string{"home", "public"}, |
|
33 |
} |
|
34 |
||
35 |
func init() { |
|
36 |
RootCmd.AddCommand(timelineCmd) |
|
37 |
||
38 |
timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance") |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
39 |
timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of results") |
0 | 40 |
} |
41 |
||
42 |
func timelineRunE(cmd *cobra.Command, args []string) error { |
|
43 |
opt := timelineOpts |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
44 |
var limOpts *madon.LimitParams |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
45 |
|
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
46 |
if opt.limit > 0 { |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
47 |
limOpts = new(madon.LimitParams) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
48 |
limOpts.Limit = int(opt.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
49 |
} |
0 | 50 |
|
51 |
tl := "home" |
|
52 |
if len(args) > 0 { |
|
53 |
tl = args[0] |
|
54 |
} |
|
55 |
||
56 |
// The home timeline is the only one requiring to be logged in |
|
57 |
if err := madonInit(tl == "home"); err != nil { |
|
58 |
return err |
|
59 |
} |
|
60 |
||
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
61 |
sl, err := gClient.GetTimelines(tl, opt.local, limOpts) |
0 | 62 |
if err != nil { |
63 |
errPrint("Error: %s", err.Error()) |
|
64 |
return nil |
|
65 |
} |
|
66 |
||
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
67 |
if opt.limit > 0 && len(sl) > int(opt.limit) { |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
68 |
sl = sl[:opt.limit] |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
69 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
70 |
|
0 | 71 |
p, err := getPrinter() |
72 |
if err != nil { |
|
73 |
return err |
|
74 |
} |
|
75 |
return p.PrintObj(sl, nil, "") |
|
76 |
} |