/
recommend.go
67 lines (59 loc) · 1.62 KB
/
recommend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cmd
import (
"fmt"
"sort"
"strings"
"time"
"github.com/charmbracelet/glamour"
letseat "github.com/drewstinnett/letseat/pkg"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newRecommendCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "recommend",
Aliases: []string{"rec", "r"},
Short: "Recommend some places to eat!",
RunE: runRecommend,
}
bindFilter(cmd)
cmd.PersistentFlags().Int("top", 3, "return N number recommendations")
return cmd
}
func runRecommend(cmd *cobra.Command, args []string) error {
diary := letseat.New(
letseat.WithFilter(*mustNewEntryFilterWithCmd(cmd)),
letseat.WithDBFilename(mustGetCmd[string](*cmd, "data")),
)
topN := mustGetCmd[int](*cmd, "top")
placesDetails := diary.PlaceDetails()
sort.Slice(placesDetails, func(i, j int) bool {
return placesDetails[i].LastVisit.Before(*placesDetails[j].LastVisit)
})
lvisited := placesDetails[0:min(topN, len(placesDetails))]
doc := strings.Builder{}
doc.WriteString("# Recommendations Based on Time\n")
for _, item := range lvisited {
doc.WriteString(fmt.Sprintf("* %v (%v days ago)\n", item.Name, int(time.Since(*item.LastVisit).Hours()/24)))
}
doc.WriteString("\n")
out, err := getRenderer().Render(docStyle.Render(doc.String()))
if err != nil {
return err
}
fmt.Fprint(cmd.OutOrStdout(), docStyle.Render(out))
return nil
}
func getRenderer() *glamour.TermRenderer {
theme := viper.GetString("theme")
if theme == "" {
theme = "dark"
}
r, err := glamour.NewTermRenderer(
// glamour.WithAutoStyle(),
glamour.WithStandardStyle(theme),
glamour.WithWordWrap(80),
)
panicIfErr(err)
return r
}