File: times.go

package info (click to toggle)
tea-cli 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,364 kB
  • sloc: makefile: 120; sh: 17
file content (64 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (2)
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
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package print

import (
	"fmt"

	"code.gitea.io/sdk/gitea"
)

// TrackedTimesList print list of tracked times to stdout
func TrackedTimesList(times []*gitea.TrackedTime, outputType string, fields []string, printTotal bool) {
	var printables = make([]printable, len(times))
	var totalDuration int64
	for i, t := range times {
		totalDuration += t.Time
		printables[i] = &printableTrackedTime{t, outputType}
	}
	t := tableFromItems(fields, printables, isMachineReadable(outputType))

	if printTotal {
		total := make([]string, len(fields))
		total[0] = "TOTAL"
		total[len(fields)-1] = formatDuration(totalDuration, outputType)
		t.addRowSlice(total)
	}

	t.print(outputType)
}

// TrackedTimeFields contains all available fields for printing of tracked times.
var TrackedTimeFields = []string{
	"id",
	"created",
	"repo",
	"issue",
	"user",
	"duration",
}

type printableTrackedTime struct {
	*gitea.TrackedTime
	outputFormat string
}

func (t printableTrackedTime) FormatField(field string, machineReadable bool) string {
	switch field {
	case "id":
		return fmt.Sprintf("%d", t.ID)
	case "created":
		return FormatTime(t.Created, machineReadable)
	case "repo":
		return t.Issue.Repository.FullName
	case "issue":
		return fmt.Sprintf("#%d", t.Issue.Index)
	case "user":
		return t.UserName
	case "duration":
		return formatDuration(t.Time, t.outputFormat)
	}
	return ""
}