@@ -10,6 +10,7 @@ import (
10
10
"io"
11
11
"io/ioutil"
12
12
"os"
13
+ "sort"
13
14
"strings"
14
15
"syscall"
15
16
@@ -39,6 +40,7 @@ type statistics struct {
39
40
TotCount int
40
41
WordsCount int
41
42
ScoreCount []int
43
+ ScorePerc []int
42
44
DuplicateCount int
43
45
}
44
46
@@ -371,6 +373,7 @@ func initStats(c int) statistics {
371
373
TotCount : 0 ,
372
374
WordsCount : c ,
373
375
ScoreCount : []int {0 , 0 , 0 , 0 , 0 },
376
+ ScorePerc : []int {0 , 0 , 0 , 0 , 0 },
374
377
DuplicateCount : 0 ,
375
378
}
376
379
}
@@ -417,25 +420,87 @@ func showTable(data [][]string, w io.Writer) {
417
420
func showStats (stat statistics , w io.Writer ) {
418
421
// writer is a s parameter to pass buffer during tests
419
422
table := tablewriter .NewWriter (w )
420
- table .SetHeader ([]string {"Description" , "Count" })
423
+ table .SetHeader ([]string {"Description" , "Count" , "%" , "Bar" })
421
424
table .SetBorder (false )
425
+ table .SetAutoWrapText (false )
426
+
427
+ stat .ScorePerc = roundPercentage (stat .ScoreCount , stat .TotCount )
422
428
423
429
data := [][]string {
424
- {"Password checked" , fmt .Sprintf ("%d" , stat .TotCount )},
425
- {"Words in dictionaries" , fmt .Sprintf ("%d" , stat .WordsCount )},
426
- {"Duplicated passwords" , fmt .Sprintf ("%d" , stat .DuplicateCount )},
427
- {"Really bad passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [0 ])},
428
- {"Bad passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [1 ])},
429
- {"Weak passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [2 ])},
430
- {"Good passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [3 ])},
431
- {"Strong passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [4 ])},
430
+ {"Password checked" , fmt .Sprintf ("%d" , stat .TotCount ), "" , "" },
431
+ {"Words in dictionaries" , fmt .Sprintf ("%d" , stat .WordsCount ), "" , "" },
432
+ {"Duplicated passwords" , fmt .Sprintf ("%d" , stat .DuplicateCount ), "" , "" },
433
+ {"Really bad passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [0 ]), fmt .Sprintf ("%3d%%" , stat .ScorePerc [0 ]), showBarPerc (stat .ScorePerc [0 ])},
434
+ {"Bad passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [1 ]), fmt .Sprintf ("%3d%%" , stat .ScorePerc [1 ]), showBarPerc (stat .ScorePerc [1 ])},
435
+ {"Weak passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [2 ]), fmt .Sprintf ("%3d%%" , stat .ScorePerc [2 ]), showBarPerc (stat .ScorePerc [2 ])},
436
+ {"Good passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [3 ]), fmt .Sprintf ("%3d%%" , stat .ScorePerc [3 ]), showBarPerc (stat .ScorePerc [3 ])},
437
+ {"Strong passwords" , fmt .Sprintf ("%d" , stat .ScoreCount [4 ]), fmt .Sprintf ("%3d%%" , stat .ScorePerc [4 ]), showBarPerc (stat .ScorePerc [4 ])},
438
+ }
439
+
440
+ var scoreColor int
441
+ for i , row := range data {
442
+ switch i {
443
+ case 3 :
444
+ scoreColor = tablewriter .BgRedColor
445
+ case 4 :
446
+ scoreColor = tablewriter .BgHiRedColor
447
+ case 5 :
448
+ scoreColor = tablewriter .BgHiYellowColor
449
+ case 6 :
450
+ scoreColor = tablewriter .BgHiGreenColor
451
+ case 7 :
452
+ scoreColor = tablewriter .BgGreenColor
453
+ }
454
+ // remove color is bar is 0%
455
+ if row [1 ] == "0" {
456
+ scoreColor = 0
457
+ }
458
+ table .Rich (row , []tablewriter.Colors {nil , nil , nil , {scoreColor }})
432
459
}
460
+ table .Render ()
461
+ }
433
462
434
- for _ , row := range data {
435
- table .Append (row )
463
+ func showBarPerc (perc int ) string {
464
+ return fmt .Sprintf ("%v" , strings .Repeat (" " , perc ))
465
+ }
466
+
467
+ func roundPercentage (scoreCount []int , totCount int ) []int {
468
+
469
+ type Percentage struct {
470
+ Value float32
471
+ Order int
436
472
}
437
473
438
- table .Render ()
474
+ roundedPerc := []int {}
475
+ dataset := []Percentage {}
476
+ totalPerc := 0
477
+
478
+ //percentages []float32
479
+ for i , score := range scoreCount {
480
+ perc := float32 (score ) / float32 (totCount ) * 100.0
481
+ dataset = append (dataset , Percentage {Value : perc , Order : i })
482
+ totalPerc += int (perc )
483
+ }
484
+ diffToAdd := 100 - totalPerc
485
+
486
+ // order by decimal
487
+ sort .Slice (dataset , func (i , j int ) bool {
488
+ return dataset [i ].Value - float32 (int (dataset [i ].Value )) > dataset [j ].Value - float32 (int (dataset [j ].Value ))
489
+ })
490
+ // distribute diff to get to 100%
491
+ for n := 0 ; n < diffToAdd ; n ++ {
492
+ dataset [n ].Value ++
493
+ }
494
+ // order by original position
495
+ sort .Slice (dataset , func (i , j int ) bool {
496
+ return float32 (dataset [i ].Order ) < float32 (dataset [j ].Order )
497
+ })
498
+
499
+ for _ , n := range dataset {
500
+ roundedPerc = append (roundedPerc , int (n .Value ))
501
+ }
502
+
503
+ return roundedPerc
439
504
}
440
505
441
506
func getPwdStdin () (string , error ) {
0 commit comments