Skip to content

Commit a846b7f

Browse files
committed
Add long duration format, supports Jira time format
1 parent 70d264c commit a846b7f

11 files changed

Lines changed: 215 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ Session.vim
7171
# auto-generated tag files
7272
tags
7373

74-
74+
.vimrc

command/record.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,19 @@ Options:
5353
-terminal=false Record a terminal event.
5454
5555
-status=false Return total time recorded for event.
56+
57+
-long-duration=false Return total time recorded in long duration format
5658
`
5759
return strings.TrimSpace(helpText)
5860
}
5961

6062
// Run executes record command with args
6163
func (c RecordCmd) Run(args []string) int {
62-
var status, terminal bool
64+
var status, terminal, longDuration bool
6365
cmdFlags := flag.NewFlagSet("record", flag.ContinueOnError)
6466
cmdFlags.BoolVar(&status, "status", false, "")
6567
cmdFlags.BoolVar(&terminal, "terminal", false, "")
68+
cmdFlags.BoolVar(&longDuration, "long-duration", false, "")
6669
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
6770
if err := cmdFlags.Parse(args); err != nil {
6871
return 1
@@ -108,7 +111,7 @@ func (c RecordCmd) Run(args []string) int {
108111
c.Ui.Error(err.Error())
109112
return 1
110113
}
111-
out, err = report.Status(commitNote, report.OutputOptions{TotalOnly: true})
114+
out, err = report.Status(commitNote, report.OutputOptions{TotalOnly: true, LongDuration: longDuration})
112115
if err != nil {
113116
c.Ui.Error(err.Error())
114117
return 1

command/record_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,44 @@ func TestRecordFileWithStatus(t *testing.T) {
122122
}
123123
}
124124

125+
func TestRecordFileWithStatusLongDuration(t *testing.T) {
126+
repo := util.NewTestRepo(t, false)
127+
defer repo.Remove()
128+
repo.Seed()
129+
repoPath := repo.PathIn("")
130+
os.Chdir(repoPath)
131+
132+
(InitCmd{Ui: new(cli.MockUi)}).Run([]string{})
133+
134+
ui := new(cli.MockUi)
135+
c := RecordCmd{Ui: ui, Out: new(bytes.Buffer)}
136+
137+
args := []string{"-status", "-long-duration", filepath.Join(repoPath, "README")}
138+
rc := c.Run(args)
139+
140+
if rc != 0 {
141+
t.Errorf("gtm record(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter)
142+
}
143+
144+
if c.Out.String() != "1 minute" {
145+
t.Errorf("gtm record(%+v), want '1 minutes' got %s", args, c.Out.String())
146+
}
147+
148+
files, err := ioutil.ReadDir(filepath.Join(repoPath, ".gtm"))
149+
if err != nil {
150+
t.Fatalf("gtm record(%+v), want error nil got %s", args, err)
151+
}
152+
cnt := 1
153+
for _, f := range files {
154+
if filepath.Base(f.Name()) == ".event" {
155+
cnt++
156+
}
157+
}
158+
if cnt != 1 {
159+
t.Errorf("gtm record(%+v), want 1 event file got %d, %s", args, cnt, ui.ErrorWriter.String())
160+
}
161+
}
162+
125163
func TestRecordTerminal(t *testing.T) {
126164
repo := util.NewTestRepo(t, false)
127165
defer repo.Remove()

command/status.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Options:
4242
4343
-total-only=false Only display total pending time
4444
45+
-long-duration If total-only, display total pending time in long duration format
46+
4547
-tags="" Project tags to report status for, i.e --tags tag1,tag2
4648
4749
-all=false Show status for all projects
@@ -51,12 +53,13 @@ Options:
5153

5254
// Run executes status command with args
5355
func (c StatusCmd) Run(args []string) int {
54-
var color, terminalOff, totalOnly, all, profile bool
56+
var color, terminalOff, totalOnly, all, profile, longDuration bool
5557
var tags string
5658
cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
5759
cmdFlags.BoolVar(&color, "color", false, "Always output color even if no terminal is detected. Use this with pagers i.e 'less -R' or 'more -R'")
5860
cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "Exclude time spent in terminal (Terminal plugin is required)")
5961
cmdFlags.BoolVar(&totalOnly, "total-only", false, "Only display total time")
62+
cmdFlags.BoolVar(&longDuration, "long-duration", false, "Display total time in long duration format")
6063
cmdFlags.StringVar(&tags, "tags", "", "Project tags to show status on")
6164
cmdFlags.BoolVar(&all, "all", false, "Show status for all projects")
6265
cmdFlags.BoolVar(&profile, "profile", false, "Enable profiling")
@@ -96,9 +99,10 @@ func (c StatusCmd) Run(args []string) int {
9699
}
97100

98101
options := report.OutputOptions{
99-
TotalOnly: totalOnly,
100-
TerminalOff: terminalOff,
101-
Color: color}
102+
TotalOnly: totalOnly,
103+
LongDuration: longDuration,
104+
TerminalOff: terminalOff,
105+
Color: color}
102106

103107
for _, projPath := range projects {
104108
if commitNote, err = metric.Process(true, projPath); err != nil {

command/status_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ func TestStatusDefaultOptions(t *testing.T) {
2828
}
2929
}
3030

31+
func TestStatusTotalOnly(t *testing.T) {
32+
repo := util.NewTestRepo(t, false)
33+
defer repo.Remove()
34+
repo.Seed()
35+
os.Chdir(repo.PathIn(""))
36+
37+
(InitCmd{Ui: new(cli.MockUi)}).Run([]string{})
38+
39+
ui := new(cli.MockUi)
40+
c := StatusCmd{Ui: ui}
41+
42+
args := []string{"-total-only", "-long-duration"}
43+
rc := c.Run(args)
44+
45+
if rc != 0 {
46+
t.Errorf("gtm status(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
47+
}
48+
}
49+
3150
func TestStatusInvalidOption(t *testing.T) {
3251
ui := new(cli.MockUi)
3352
c := StatusCmd{Ui: ui}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ func main() {
6262
},
6363
}
6464

65-
exitStatus, err := c.Run()
65+
exitStatu, err := c.Run()
6666
if err != nil {
6767
ui.Error(err.Error())
6868
}
6969

70-
os.Exit(exitStatus)
70+
os.Exit(exitStatu)
7171
}

report/report.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ type ProjectCommits struct {
3636

3737
// OutputOptions contains cli options for reporting
3838
type OutputOptions struct {
39-
TotalOnly bool
40-
FullMessage bool
41-
TerminalOff bool
42-
Color bool
43-
Limit int
39+
TotalOnly bool
40+
LongDuration bool
41+
FullMessage bool
42+
TerminalOff bool
43+
Color bool
44+
Limit int
4445
}
4546

4647
func (o OutputOptions) limitNotes(notes commitNoteDetails) commitNoteDetails {
@@ -60,6 +61,9 @@ func Status(n note.CommitNote, options OutputOptions, projPath ...string) (strin
6061
}
6162

6263
if options.TotalOnly {
64+
if options.LongDuration {
65+
return util.DurationStrLong(n.Total()), nil
66+
}
6367
return util.DurationStr(n.Total()), nil
6468
}
6569

util/string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"regexp"
1010
"strings"
1111
"time"
12+
13+
"github.com/hako/durafmt"
1214
)
1315

1416
// Percent returns a values percent of the total
@@ -40,6 +42,14 @@ func DurationStr(secs int) string {
4042
return (time.Duration(secs) * time.Second).String()
4143
}
4244

45+
func DurationStrLong(secs int) string {
46+
d, err := durafmt.ParseString(DurationStr(secs))
47+
if err != nil {
48+
return ""
49+
}
50+
return d.String()
51+
}
52+
4353
// https://github.com/DaddyOh/golang-samples/blob/master/pad.go
4454

4555
// RightPad2Len https://github.com/DaddyOh/golang-samples/blob/master/pad.go

vendor/github.com/hako/durafmt/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hako/durafmt/durafmt.go

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)