This commit is contained in:
2020-01-14 13:33:57 +11:00
parent 6425d4b7db
commit c1d0be7f3b
2 changed files with 23 additions and 13 deletions
+14 -4
View File
@@ -1,21 +1,31 @@
package redhat
import "fmt"
import (
"fmt"
"strconv"
)
// Output print out the result with target format
// Output print out the result with proper order and format
func (fa *FileAnalyser) Output(topLines int) []string {
resultRows := fa.getResultRows(topLines)
maxLength := lengthOfMaxNumberFromDataRows(resultRows)
reverseDataRows(resultRows)
outputArray := make([]string, 0, len(resultRows))
for index := 0; index < len(resultRows); index++ {
outputArray = append(outputArray, fmt.Sprintf("%d %s", resultRows[index].count, resultRows[index].word))
for _, item := range resultRows {
outputArray = append(outputArray, fmt.Sprintf("%*d %s", maxLength, item.count, item.word))
}
return outputArray
}
func lengthOfMaxNumberFromDataRows(dataRows []dataRow) int {
countString := strconv.Itoa(dataRows[0].count)
return len(countString)
}
func (fa *FileAnalyser) getResultRows(topLines int) []dataRow {
if len(fa.dataRows) < topLines {
return fa.dataRows[0:len(fa.dataRows)]
+4 -4
View File
@@ -90,11 +90,11 @@ func TestFileAnalyser_Output(t *testing.T) {
dataRows: []dataRow{
{
word: "the",
count: 6,
count: 205,
},
{
word: "of",
count: 6,
count: 16,
},
{
word: "shape",
@@ -127,8 +127,8 @@ func TestFileAnalyser_Output(t *testing.T) {
" 3 future",
" 4 libraries",
" 5 shape",
"6 of",
"6 the",
" 16 of",
"205 the",
},
},
}