11package main
22
33import (
4+ "database/sql"
45"fmt"
56"github.com/urfave/cli"
67"strconv"
78"time"
89)
910
11+ type tip struct {
12+ Date string
13+ Amount string
14+ Message string
15+ }
16+
1017// TODO: add description?
11- // TODO: show sender of tips?
1218var summaryCommand = cli.Command {
1319Name : "summary" ,
1420Usage : "Shows a summary of received tips" ,
@@ -19,7 +25,7 @@ func summary(ctx *cli.Context) error {
1925db , err := openDatabase (ctx )
2026
2127if err == nil {
22- rows , err := db . Query ( "SELECT * FROM tips ORDER BY date DESC" )
28+ rows , err := getTips ( db )
2329
2430if err == nil {
2531var tips int64
@@ -37,10 +43,13 @@ func summary(ctx *cli.Context) error {
3743}
3844
3945if err == nil {
40- date := time .Unix (unixDate , 0 )
46+ date := formatUnixDate (unixDate )
47+
48+ // Trim hours and minutes
49+ date = date [:len (date )- 6 ]
4150
42- fmt .Println ("Received " + strconv . FormatInt (tips , 10 ) + " of tips since " + date . Format ( "02-01-2006" ) +
43- " totalling " + strconv . FormatInt (sum , 10 ) + " satoshis" )
51+ fmt .Println ("Received " + formatInt (tips ) + " tips since " + date +
52+ " totalling " + formatInt (sum ) + " satoshis" )
4453}
4554
4655}
@@ -49,3 +58,86 @@ func summary(ctx *cli.Context) error {
4958
5059return err
5160}
61+
62+ // TODO: show sender of tips?
63+ var listCommand = cli.Command {
64+ Name : "list" ,
65+ Usage : "Shows all received tips" ,
66+ Action : list ,
67+ }
68+
69+ func list (ctx * cli.Context ) error {
70+ db , err := openDatabase (ctx )
71+
72+ if err == nil {
73+ rows , err := getTips (db )
74+
75+ if err == nil {
76+ var tips []tip
77+
78+ // To ensure that the grid looks right
79+ maxAmountSize := 6
80+
81+ var unixDate int64
82+ var amount int64
83+ var message string
84+
85+ for rows .Next () {
86+ err = rows .Scan (& unixDate , & amount , & message )
87+
88+ amountString := formatInt (amount )
89+
90+ tips = append (tips , tip {
91+ Date : formatUnixDate (unixDate ),
92+ Amount : amountString ,
93+ Message : message ,
94+ })
95+
96+ if amountSize := len (amountString ); amountSize > maxAmountSize {
97+ maxAmountSize = amountSize
98+ }
99+
100+ }
101+
102+ fmt .Println ("Date Amount" + getSpacing (6 , maxAmountSize ) + "Message" )
103+
104+ for _ , tip := range tips {
105+ tipSpacing := getSpacing (len (tip .Amount ), maxAmountSize )
106+
107+ fmt .Println (tip .Date + " " + tip .Amount + tipSpacing + tip .Message )
108+ }
109+
110+ }
111+
112+ }
113+
114+ return err
115+ }
116+
117+ func getSpacing (entrySize int , maxSize int ) string {
118+ spacing := " "
119+
120+ spacingSize := maxSize - entrySize
121+
122+ for spacingSize > 0 {
123+ spacing += " "
124+
125+ spacingSize --
126+ }
127+
128+ return spacing
129+ }
130+
131+ func formatUnixDate (unixDate int64 ) string {
132+ date := time .Unix (unixDate , 0 )
133+
134+ return date .Format ("02-01-2006 15:04" )
135+ }
136+
137+ func formatInt (i int64 ) string {
138+ return strconv .FormatInt (i , 10 )
139+ }
140+
141+ func getTips (db * sql.DB ) (rows * sql.Rows , err error ) {
142+ return db .Query ("SELECT * FROM tips ORDER BY date DESC" )
143+ }
0 commit comments