1package main23import (4 "github.com/nmeum/tracktime/parser"56 "fmt"7 "io"8 "log"9 "os"10 "strings"11 "time"12)1314type Table [][]string1516const outputFormat = time.RFC8221718func escapeCell(cell string) string {19 var builder strings.Builder20 builder.Grow(len(cell))2122 for _, r := range cell {23 switch r {24 case '&':25 builder.WriteString("\\&")26 default:27 builder.WriteRune(r)28 }29 }3031 return builder.String()32}3334func printTabular(w io.Writer, rows Table) {35 numCols := len(rows[0])3637 alignment := make([]string, numCols)38 for i := 0; i < numCols; i++ {39 alignment[i] = "r"40 }41 alignment[0] = "l"4243 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 }5152 fmt.Fprintf(w, "%v %v ", escapeCell(col), delim)53 }54 fmt.Fprintf(w, "\n")55 }56 fmt.Fprintf(w, "\\end{tabular}\n")57}5859func main() {60 p := parser.NewParser(parser.DefaultTimeFormat())61 entries, err := p.ParseEntries("stdin", os.Stdin)62 if err != nil {63 log.Fatal(err)64 }6566 var rows Table67 for _, entry := range entries {68 rows = append(rows, []string{69 entry.Date.Format(outputFormat),70 entry.Duration.String(),71 entry.Description,72 })73 }7475 if len(rows) == 0 {76 log.Fatal("no entries in given file")77 }78 printTabular(os.Stdout, rows)79}