1package main
2
3import (
4 "github.com/nmeum/tracktime/parser"
5
6 "fmt"
7 "io"
8 "log"
9 "os"
10 "strings"
11 "time"
12)
13
14type Table [][]string
15
16const outputFormat = time.RFC822
17
18func escapeCell(cell string) string {
19 var builder strings.Builder
20 builder.Grow(len(cell))
21
22 for _, r := range cell {
23 switch r {
24 case '&':
25 builder.WriteString("\\&")
26 default:
27 builder.WriteRune(r)
28 }
29 }
30
31 return builder.String()
32}
33
34func printTabular(w io.Writer, rows Table) {
35 numCols := len(rows[0])
36
37 alignment := make([]string, numCols)
38 for i := 0; i < numCols; i++ {
39 alignment[i] = "r"
40 }
41 alignment[0] = "l"
42
43 fmt.Fprintf(w, "\\begin{tabular}{%s}\n", strings.Join(alignment, "|"))
44 for _, row := range rows {
45 fmt.Fprintf(w, "\t")
46 for n, col := range row {
47 delim := "&"
48 if n == numCols-1 {
49 delim = "\\\\"
50 }
51
52 fmt.Fprintf(w, "%v %v ", escapeCell(col), delim)
53 }
54 fmt.Fprintf(w, "\n")
55 }
56 fmt.Fprintf(w, "\\end{tabular}\n")
57}
58
59func main() {
60 p := parser.NewParser(parser.DefaultTimeFormat())
61 entries, err := p.ParseEntries("stdin", os.Stdin)
62 if err != nil {
63 log.Fatal(err)
64 }
65
66 var rows Table
67 for _, entry := range entries {
68 rows = append(rows, []string{
69 entry.Date.Format(outputFormat),
70 entry.Duration.String(),
71 entry.Description,
72 })
73 }
74
75 if len(rows) == 0 {
76 log.Fatal("no entries in given file")
77 }
78 printTabular(os.Stdout, rows)
79}