@@ -218,7 +218,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
218
218
service := github .New (http .TokenSourcedClient (cmd .Context (), cnf .Token ))
219
219
date , weeks , half := time .Now ().UTC (), - 1 , false
220
220
221
- // input validation: date/{+-}weeks
221
+ // input validation: date[ /{+-}weeks]
222
222
if len (args ) == 1 {
223
223
var err error
224
224
wrap := func (err error ) error {
@@ -333,33 +333,52 @@ func Contribution(cnf *config.Tool) *cobra.Command {
333
333
// ------------- ------ ------ ------ ----- -----
334
334
// Contributions for 2013-11-17: -3119d, 0 → 5
335
335
//
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
339
339
//
340
340
suggest := cobra.Command {
341
341
Use : "suggest" ,
342
342
Args : cobra .MaximumNArgs (1 ),
343
343
RunE : func (cmd * cobra.Command , args []string ) error {
344
344
// dependencies and defaults
345
345
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
347
347
delta := unsafe .ReturnBool (cmd .Flags ().GetBool ("delta" ))
348
348
short := unsafe .ReturnBool (cmd .Flags ().GetBool ("short" ))
349
349
target := unsafe .ReturnInt (cmd .Flags ().GetInt ("target" ))
350
- weeks := unsafe .ReturnInt (cmd .Flags ().GetInt ("weeks" ))
351
350
352
- // input validation: date(year,+month,+week{day})
351
+ // input validation: date(year,+month,+week{day})[/{+-}weeks]
353
352
if len (args ) == 1 {
354
353
var err error
355
354
wrap := func (err error ) error {
356
355
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" ,
358
357
fmt .Errorf ("invalid argument %q: %w" , args [0 ], err ),
359
358
)
360
359
}
361
360
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 ) {
363
382
case len (time .RFC3339Year ):
364
383
date , err = time .Parse (time .RFC3339Year , input )
365
384
case len (time .RFC3339Month ):
@@ -386,7 +405,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
386
405
}
387
406
388
407
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
390
409
data := contribution .HistogramByWeekday (chm .Subset (area ), false )
391
410
392
411
// data presentation
@@ -401,7 +420,6 @@ func Contribution(cnf *config.Tool) *cobra.Command {
401
420
suggest .Flags ().Bool ("delta" , false , "shows relatively" )
402
421
suggest .Flags ().Bool ("short" , false , "shows only date" )
403
422
suggest .Flags ().Int ("target" , 5 , "minimum contributions" )
404
- suggest .Flags ().Int ("weeks" , 5 , "how many weeks to show" )
405
423
cmd .AddCommand (& suggest )
406
424
407
425
return & cmd
0 commit comments