From 33b57f10ecab9beb0ece43f925f601293ae66721 Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Wed, 12 Jun 2019 12:07:00 -0700 Subject: [PATCH] ref T611 detailed post list adds a new flag to posts command, d, for detailed listing detailed list includes: - tile - last modifed date in user local time - blog/collection name - slug - ID - URL - a two line excerpt both slug and collection will state if none was present --- cmd/writeas/main.go | 4 ++ commands/commands.go | 49 ++------------------ commands/list_posts.go | 101 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 45 deletions(-) create mode 100644 commands/list_posts.go diff --git a/cmd/writeas/main.go b/cmd/writeas/main.go index c113d19..6ed01e9 100644 --- a/cmd/writeas/main.go +++ b/cmd/writeas/main.go @@ -160,6 +160,10 @@ func main() { Name: "id", Usage: "Show list with post IDs (default)", }, + cli.BoolFlag{ + Name: "d", + Usage: "Show detailed view of posts", + }, cli.BoolFlag{ Name: "md", Usage: "Use with --url to return URLs with Markdown enabled", diff --git a/commands/commands.go b/commands/commands.go index e594620..bd33714 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -185,53 +185,12 @@ func CmdAdd(c *cli.Context) error { } func CmdListPosts(c *cli.Context) error { - urls := c.Bool("url") - ids := c.Bool("id") - - var p api.Post posts := api.GetPosts(c) tw := tabwriter.NewWriter(os.Stdout, 10, 0, 2, ' ', tabwriter.TabIndent) - numPosts := len(*posts) - if ids || !urls && numPosts != 0 { - fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "ID", "Token") - } else if numPosts != 0 { - fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "URL", "Token") - } else { - fmt.Fprintf(tw, "No local posts found\n") - } - for i := range *posts { - p = (*posts)[numPosts-1-i] - if ids || !urls { - fmt.Fprintf(tw, "unsynced\t%s\t%s\t\n", p.ID, p.EditToken) - } else { - fmt.Fprintf(tw, "unsynced\t%s\t%s\t\n", getPostURL(c, p.ID), p.EditToken) - } - } - u, _ := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"])) - if u != nil { - remotePosts, err := api.GetUserPosts(c) - if err != nil { - fmt.Println(err) - } - - if len(remotePosts) > 0 { - identifier := "URL" - if ids || !urls { - identifier = "ID" - } - fmt.Fprintf(tw, "\nAccount\t%s\t%s\t\n", identifier, "Title") - } - for _, p := range remotePosts { - identifier := getPostURL(c, p.ID) - if ids || !urls { - identifier = p.ID - } - synced := "unsynced" - if p.Synced { - synced = "synced" - } - fmt.Fprintf(tw, "%s\t%s\t%s\t\n", synced, identifier, p.Title) - } + printLocalPosts(c, tw, posts) + err := printRemotePosts(c, tw) + if err != nil { + log.Errorln("Could not print remote posts: %v", err) } return tw.Flush() } diff --git a/commands/list_posts.go b/commands/list_posts.go new file mode 100644 index 0000000..0574ee6 --- /dev/null +++ b/commands/list_posts.go @@ -0,0 +1,101 @@ +package commands + +import ( + "fmt" + "text/tabwriter" + "time" + + "github.com/writeas/writeas-cli/api" + "github.com/writeas/writeas-cli/config" + cli "gopkg.in/urfave/cli.v1" +) + +const ( + dashBar = "--------------------------------------------------------------------------------" +) + +func printLocalPosts(c *cli.Context, tw *tabwriter.Writer, posts *[]api.Post) { + ids := c.Bool("id") + urls := c.Bool("url") + numPosts := len(*posts) + if ids || !urls && numPosts != 0 { + fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "ID", "Token") + } else if numPosts != 0 { + fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "URL", "Token") + } else { + fmt.Fprintf(tw, "No local posts found\n") + } + for i := range *posts { + p := (*posts)[numPosts-1-i] + if ids || urls { + fmt.Fprintf(tw, "unsynced\t%s\t%s\n", p.ID, p.EditToken) + } else { + fmt.Fprintf(tw, "unsynced\t%s\t%s\n", getPostURL(c, p.ID), p.EditToken) + } + } +} + +func printRemotePosts(c *cli.Context, tw *tabwriter.Writer) error { + ids := c.Bool("id") + urls := c.Bool("url") + details := c.Bool("d") + u, _ := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"])) + if u != nil { + remotePosts, err := api.GetUserPosts(c) + if err != nil { + return err + } + + if len(remotePosts) > 0 { + identifier := "URL" + if ids || !urls { + identifier = "ID" + } + if !details { + fmt.Fprintf(tw, "\nAccount\t%s\t%s\n", identifier, "Title") + } + } + for _, p := range remotePosts { + identifier := getPostURL(c, p.ID) + if ids || !urls { + identifier = p.ID + } + if details { + slug := p.ID + if p.Slug != "" { + slug = p.Slug + if p.Collection != "" { + slug = p.Collection + "/" + slug + } + } + url := getPostURL(c, slug) + if p.Slug == "" { + p.Slug = "no-slug" + } + if p.Collection == "" { + p.Collection = "no-blog" + } + fmt.Fprintf(tw, "\n%s\t%s\t%s\n", "Title: ", p.Title, " ") + fmt.Fprintf(tw, "%s\t%s\t%s\n", "Last Updated: ", prettyDate(p.Updated), "") + fmt.Fprintf(tw, "%s\t%s\t%s\n", "Blog: ", p.Collection, " ") + fmt.Fprintf(tw, "%s\t%s\t%s\n", "Slug/ID: ", p.Slug+" / "+p.ID, " ") + fmt.Fprintf(tw, "%s\t%s\t%s\n\n", "URL: ", url, " ") + } + synced := "unsynced" + if p.Synced { + synced = "synced" + } + if details { + fmt.Fprintln(tw, p.Excerpt) + fmt.Fprintln(tw, dashBar) + } else { + fmt.Fprintf(tw, "%s\t%s\t%s\n", synced, identifier, p.Title) + } + } + } + return nil +} + +func prettyDate(date time.Time) string { + return date.Local().Format(time.RFC822) +}