Skip to content

Commit dac6a2a

Browse files
committed
fix #60: replace --weeks by /weeks argument format for suggest
1 parent defb795 commit dac6a2a

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

docs/changelog.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@
100100
------------- ------ ------ ------ ----- -----
101101
Contributions for 2013-11-17: -3119d, 0 -> 5
102102
103-
$ maintainer github contribution suggest 2013-11
104-
$ maintainer github contribution suggest 2013
105-
$ maintainer github contribution suggest --short 2013
103+
$ maintainer github contribution suggest 2013-11/10
104+
$ maintainer github contribution suggest --target=5 2013/+10
105+
$ maintainer github contribution suggest --short 2013/-10
106106
```
107107

108108
[calendar]: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/managing-contribution-graphs-on-your-profile/viewing-contributions-on-your-profile#contributions-calendar

docs/readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
* Suggests a reasonable date to contribute
8888

8989
```bash
90-
$ maintainer github contribution suggest 2013-11-20
90+
$ maintainer github contribution suggest --delta 2013-11-20
9191
Day / Week #45 #46 #47 #48 #49
9292
------------- ------ ------ ------ ----- -----
9393
Sunday - - - 1 -
@@ -100,8 +100,9 @@
100100
------------- ------ ------ ------ ----- -----
101101
Contributions for 2013-11-17: -3119d, 0 -> 5
102102
103-
$ maintainer github contribution suggest 2013-11
104-
$ maintainer github contribution suggest 2013
103+
$ maintainer github contribution suggest 2013-11/10
104+
$ maintainer github contribution suggest --target=5 2013/+10
105+
$ maintainer github contribution suggest --short 2013/-10
105106
```
106107

107108
[calendar]: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/managing-contribution-graphs-on-your-profile/viewing-contributions-on-your-profile#contributions-calendar

internal/command/github/contribution.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
218218
service := github.New(http.TokenSourcedClient(cmd.Context(), cnf.Token))
219219
date, weeks, half := time.Now().UTC(), -1, false
220220

221-
// input validation: date/{+-}weeks
221+
// input validation: date[/{+-}weeks]
222222
if len(args) == 1 {
223223
var err error
224224
wrap := func(err error) error {
@@ -333,33 +333,52 @@ func Contribution(cnf *config.Tool) *cobra.Command {
333333
// ------------- ------ ------ ------ ----- -----
334334
// Contributions for 2013-11-17: -3119d, 0 → 5
335335
//
336-
// $ maintainer github contribution suggest 2013-11
337-
// $ maintainer github contribution suggest 2013
338-
// $ maintainer github contribution suggest --short 2013
336+
// $ maintainer github contribution suggest 2013-11/10
337+
// $ maintainer github contribution suggest --target=5 2013/+10
338+
// $ maintainer github contribution suggest --short 2013/-10
339339
//
340340
suggest := cobra.Command{
341341
Use: "suggest",
342342
Args: cobra.MaximumNArgs(1),
343343
RunE: func(cmd *cobra.Command, args []string) error {
344344
// dependencies and defaults
345345
service := github.New(http.TokenSourcedClient(cmd.Context(), cnf.Token))
346-
date := time.TruncateToYear(time.Now().UTC())
346+
date, weeks, half := time.TruncateToYear(time.Now().UTC()), 5, false
347347
delta := unsafe.ReturnBool(cmd.Flags().GetBool("delta"))
348348
short := unsafe.ReturnBool(cmd.Flags().GetBool("short"))
349349
target := unsafe.ReturnInt(cmd.Flags().GetInt("target"))
350-
weeks := unsafe.ReturnInt(cmd.Flags().GetInt("weeks"))
351350

352-
// input validation: date(year,+month,+week{day})
351+
// input validation: date(year,+month,+week{day})[/{+-}weeks]
353352
if len(args) == 1 {
354353
var err error
355354
wrap := func(err error) error {
356355
return fmt.Errorf(
357-
"please provide argument in format YYYY[-mm[-dd]], e.g., 2006-01: %w",
356+
"please provide argument in format YYYY[-mm[-dd]][/[+|-]weeks], e.g., 2006-01: %w",
358357
fmt.Errorf("invalid argument %q: %w", args[0], err),
359358
)
360359
}
361360

362-
switch input := args[0]; len(input) {
361+
input := args[0]
362+
raw := strings.Split(input, "/")
363+
switch len(raw) {
364+
case 2:
365+
weeks, err = strconv.Atoi(raw[1])
366+
if err != nil {
367+
return wrap(err)
368+
}
369+
// +%d and positive %d have the same value, but different semantic
370+
// invariant: len(raw[1]) > 0, because weeks > 0 and invariant(time.RangeByWeeks)
371+
if weeks > 0 && raw[1][0] != '+' {
372+
half = true
373+
}
374+
fallthrough
375+
case 1:
376+
input = raw[0]
377+
default:
378+
return wrap(fmt.Errorf("too many parts"))
379+
}
380+
381+
switch len(input) {
363382
case len(time.RFC3339Year):
364383
date, err = time.Parse(time.RFC3339Year, input)
365384
case len(time.RFC3339Month):
@@ -386,7 +405,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
386405
}
387406

388407
value := contribution.Suggest(chm, start, scope.To(), target)
389-
area := time.RangeByWeeks(value.Day, weeks, true).Shift(-time.Day) // Sunday
408+
area := time.RangeByWeeks(value.Day, weeks, half).Shift(-time.Day) // Sunday
390409
data := contribution.HistogramByWeekday(chm.Subset(area), false)
391410

392411
// data presentation
@@ -401,7 +420,6 @@ func Contribution(cnf *config.Tool) *cobra.Command {
401420
suggest.Flags().Bool("delta", false, "shows relatively")
402421
suggest.Flags().Bool("short", false, "shows only date")
403422
suggest.Flags().Int("target", 5, "minimum contributions")
404-
suggest.Flags().Int("weeks", 5, "how many weeks to show")
405423
cmd.AddCommand(&suggest)
406424

407425
return &cmd

0 commit comments

Comments
 (0)