From 008c97356566bd5ccc00929c09009864b8d496d5 Mon Sep 17 00:00:00 2001 From: gutmet Date: Fri, 16 Oct 2020 19:20:53 +0200 Subject: [PATCH] add flags for a template file to format tweets --- Readme.md | 2 ++ drivel.go | 57 ++++++++++++++++++++++++++++++++++++++++-------- example.template | 4 ++++ 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 example.template diff --git a/Readme.md b/Readme.md index c537deb..48a19c0 100644 --- a/Readme.md +++ b/Readme.md @@ -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 ---------- diff --git a/drivel.go b/drivel.go index 6ffb1d0..5f169c5 100644 --- a/drivel.go +++ b/drivel.go @@ -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) diff --git a/example.template b/example.template new file mode 100644 index 0000000..1717e84 --- /dev/null +++ b/example.template @@ -0,0 +1,4 @@ +{{range $tweet := .}} +{{- $tweet.String}} +---------- +{{end}}