Improve tea time
#319
|
@ -64,6 +64,7 @@ func RunTimesList(cmd *cli.Context) error {
|
|||
var times []*gitea.TrackedTime
|
||||
var err error
|
||||
var from, until time.Time
|
||||
var fields []string
|
||||
|
||||
if ctx.IsSet("from") {
|
||||
from, err = dateparse.ParseLocal(ctx.String("from"))
|
||||
|
@ -83,6 +84,7 @@ func RunTimesList(cmd *cli.Context) error {
|
|||
user := ctx.Args().First()
|
||||
if ctx.Bool("mine") {
|
||||
times, _, err = client.GetMyTrackedTimes()
|
||||
fields = []string{"created", "repo", "issue", "duration"}
|
||||
} else if user == "" {
|
||||
// get all tracked times on the repo
|
||||
times, _, err = client.ListRepoTrackedTimes(ctx.Owner, ctx.Repo, opts)
|
||||
|
@ -93,16 +95,18 @@ func RunTimesList(cmd *cli.Context) error {
|
|||
return err
|
||||
}
|
||||
times, _, err = client.ListIssueTrackedTimes(ctx.Owner, ctx.Repo, issue, opts)
|
||||
noerw marked this conversation as resolved
|
||||
fields = []string{"created", "user", "duration"}
|
||||
} else {
|
||||
// get all tracked times by the specified user
|
||||
opts.User = user
|
||||
times, _, err = client.ListRepoTrackedTimes(ctx.Owner, ctx.Repo, opts)
|
||||
fields = []string{"created", "issue", "duration"}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
print.TrackedTimesList(times, ctx.Output, ctx.Bool("total"))
|
||||
print.TrackedTimesList(times, ctx.Output, fields, ctx.Bool("total"))
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -5,33 +5,60 @@
|
|||
package print
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
// TrackedTimesList print list of tracked times to stdout
|
||||
func TrackedTimesList(times []*gitea.TrackedTime, outputType string, printTotal bool) {
|
||||
tab := tableWithHeader(
|
||||
"Created",
|
||||
"Issue",
|
||||
"User",
|
||||
"Duration",
|
||||
)
|
||||
func TrackedTimesList(times []*gitea.TrackedTime, outputType string, fields []string, printTotal bool) {
|
||||
var printables = make([]printable, len(times))
|
||||
var totalDuration int64
|
||||
|
||||
for _, t := range times {
|
||||
for i, t := range times {
|
||||
totalDuration += t.Time
|
||||
tab.addRow(
|
||||
FormatTime(t.Created),
|
||||
"#"+strconv.FormatInt(t.Issue.Index, 10),
|
||||
t.UserName,
|
||||
formatDuration(t.Time, outputType),
|
||||
)
|
||||
printables[i] = &printableTrackedTime{t, outputType}
|
||||
}
|
||||
t := tableFromItems(fields, printables)
|
||||
|
||||
if printTotal {
|
||||
tab.addRow("TOTAL", "", "", formatDuration(totalDuration, outputType))
|
||||
total := make([]string, len(fields))
|
||||
total[0] = "TOTAL"
|
||||
total[len(fields)-1] = formatDuration(totalDuration, outputType)
|
||||
t.addRowSlice(total)
|
||||
}
|
||||
tab.print(outputType)
|
||||
|
||||
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) string {
|
||||
switch field {
|
||||
case "id":
|
||||
return fmt.Sprintf("%d", t.ID)
|
||||
case "created":
|
||||
return FormatTime(t.Created)
|
||||
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 ""
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user
add
fields = []string{"created", "repo", "issue", "user", "duration"}
?