mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-19 14:43:05 -03:00
cli: sort the results
Adds `FileCount` and `FileCountList` types for storing language file and their count which can be sorted based on the value of count. Signed-off-by: Sunny <me@darkowlzz.space>
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/src-d/enry.v1"
|
||||
@ -152,16 +153,19 @@ func printJson(out map[string][]string, buff *bytes.Buffer) {
|
||||
}
|
||||
|
||||
func printPercents(out map[string][]string, buff *bytes.Buffer) {
|
||||
fileCount := make(map[string]int, len(out))
|
||||
var fileCountList enry.FileCountList
|
||||
total := 0
|
||||
for name, language := range out {
|
||||
fileCount[name] = len(language)
|
||||
fc := enry.FileCount{Name: name, Count: len(language)}
|
||||
fileCountList = append(fileCountList, fc)
|
||||
total += len(language)
|
||||
}
|
||||
// Sort the fileCountList in descending order of their count value.
|
||||
sort.Sort(sort.Reverse(fileCountList))
|
||||
|
||||
for name, count := range fileCount {
|
||||
percent := float32(count) / float32(total) * 100
|
||||
buff.WriteString(fmt.Sprintf("%.2f%% %s\n", percent, name))
|
||||
for _, fc := range fileCountList {
|
||||
percent := float32(fc.Count) / float32(total) * 100
|
||||
buff.WriteString(fmt.Sprintf("%.2f%% %s\n", percent, fc.Name))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user