add flags for a template file to format tweets

This commit is contained in:
gutmet 2020-10-16 19:20:53 +02:00
parent b5d843e4a6
commit 008c973565
3 changed files with 54 additions and 9 deletions

View File

@ -88,6 +88,8 @@ drivel will automatically split large status messages and multiple files into se
The results of the 'home' and 'mentions' commands can be filtered to exclude certain user names. Create text files 'FilterHome' and 'FilterMentions' in HOME/.drivel and list excluded users line by line.
Each command (other than wipe) can use a go text template to format the printed results - pass a file name to '--template'. The template is executed with a slice of []Status. See types.go for available data fields and example.template for a minimalist example.
final note
----------

View File

@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"strings"
"text/template"
"time"
)
@ -227,8 +228,16 @@ func updateStatus(args []string, previous ObjectID, embedTweet ObjectID) []Statu
}
func PrintTweets(tweets []Status, userFilter hashset) {
filtered := []Status{}
for _, tweet := range tweets {
if !userFilter.contains(tweet.User.Screen_name) {
filtered = append(filtered, tweet)
}
}
if formatTemplate != nil {
optLogFatal("printTweets", formatTemplate.Execute(os.Stdout, filtered))
} else {
for _, tweet := range filtered {
fmt.Println(tweet.String())
fmt.Println("---------")
}
@ -462,23 +471,53 @@ func setFilters(appDir string) {
mentionsFilter = getHashset(goutil.ReadFile(filepath.Join(appDir, "FilterMentions")))
}
type generalFlags struct {
templateFile string
}
func wrapCommand(cmd goutil.CommandFunc) func() (goutil.CommandFlagsInit, goutil.CommandFunc) {
return wrapCommandFl(func() (goutil.CommandFlagsInit, goutil.CommandFunc) { return nil, cmd })
}
func wrapCommandFl(cmd func() (goutil.CommandFlagsInit, goutil.CommandFunc)) func() (goutil.CommandFlagsInit, goutil.CommandFunc) {
f := generalFlags{}
flagsInit := func(s *flag.FlagSet) {
s.StringVar(&f.templateFile, "template", "", "use a template file to format tweets")
}
return func() (goutil.CommandFlagsInit, goutil.CommandFunc) {
formerInit, commandFunc := cmd()
return func(s *flag.FlagSet) {
if formerInit != nil {
formerInit(s)
}
flagsInit(s)
}, func(args []string) error {
if f.templateFile != "" {
formatTemplate = template.Must(template.ParseFiles(f.templateFile))
}
return commandFunc(args)
}
}
}
var client *http.Client
var homeFilter hashset
var mentionsFilter hashset
var formatTemplate *template.Template
func main() {
client = getClient()
setFilters(appDir())
commands := []goutil.Command{
goutil.NewCommand("status", status, "post a status with message and/or media"),
goutil.NewCommand("home", home, "get your home timeline"),
goutil.NewCommand("mentions", mentions, "get your mention timeline"),
goutil.NewCommandWithFlags("timeline", userTimelineCommand, "get timeline of a specific user"),
goutil.NewCommand("lookup", lookup, "lookup tweets with specific IDs"),
goutil.NewCommand("reply", reply, "reply to a tweet with a specific ID"),
goutil.NewCommand("quote", quote, "quote retweet a tweet with a specific ID"),
goutil.NewCommand("retweet", retweet, "retweet a tweet with a specific ID"),
goutil.NewCommand("like", like, "like a tweet with a specific ID"),
goutil.NewCommandWithFlags("status", wrapCommand(status), "post a status with message and/or media"),
goutil.NewCommandWithFlags("home", wrapCommand(home), "get your home timeline"),
goutil.NewCommandWithFlags("mentions", wrapCommand(mentions), "get your mention timeline"),
goutil.NewCommandWithFlags("timeline", wrapCommandFl(userTimelineCommand), "get timeline of a specific user"),
goutil.NewCommandWithFlags("lookup", wrapCommand(lookup), "lookup tweets with specific IDs"),
goutil.NewCommandWithFlags("reply", wrapCommand(reply), "reply to a tweet with a specific ID"),
goutil.NewCommandWithFlags("quote", wrapCommand(quote), "quote retweet a tweet with a specific ID"),
goutil.NewCommandWithFlags("retweet", wrapCommand(retweet), "retweet a tweet with a specific ID"),
goutil.NewCommandWithFlags("like", wrapCommand(like), "like a tweet with a specific ID"),
goutil.NewCommandWithFlags("wipe", wipeCommand, "wipe your timeline and likes"),
}
err := goutil.Execute(commands)

4
example.template Normal file
View File

@ -0,0 +1,4 @@
{{range $tweet := .}}
{{- $tweet.String}}
----------
{{end}}