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:
Sunny
2017-09-30 18:41:48 +05:30
parent ded6d367fe
commit b84e338f9e
3 changed files with 65 additions and 5 deletions

View File

@ -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))
}
}