-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.go
More file actions
57 lines (49 loc) · 1.18 KB
/
Copy pathstats.go
File metadata and controls
57 lines (49 loc) · 1.18 KB
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
package main
import (
"fmt"
"strings"
"github.com/icco/etu/client"
"github.com/spf13/cobra"
)
var statsCmd = &cobra.Command{
Use: "stats",
Short: "Show journal stats (blips, tags, words written).",
Args: cobra.NoArgs,
RunE: showStats,
}
func showStats(cmd *cobra.Command, _ []string) error {
personal, err := cfg.GetStats(cmd.Context(), false)
if err != nil {
return err
}
global, err := cmd.Flags().GetBool("global")
if err != nil {
return err
}
var community *client.Stats
if global {
stats, err := cfg.GetStats(cmd.Context(), true)
if err != nil {
return err
}
community = &stats
}
fmt.Print(formatStats(personal, community))
return nil
}
// formatStats renders stats as one metric per line; a non-nil community adds
// a second "Community" block.
func formatStats(personal client.Stats, community *client.Stats) string {
var b strings.Builder
writeBlock := func(s client.Stats) {
fmt.Fprintf(&b, "Blips: %d\n", s.TotalBlips)
fmt.Fprintf(&b, "Tags: %d\n", s.UniqueTags)
fmt.Fprintf(&b, "Words written: %d\n", s.WordsWritten)
}
writeBlock(personal)
if community != nil {
b.WriteString("\nCommunity\n")
writeBlock(*community)
}
return b.String()
}