package redhat import ( "fmt" "strconv" ) // 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 _, 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)] } return fa.dataRows[0:topLines] } func reverseDataRows(rows []dataRow) { for index := len(rows)/2 - 1; index >= 0; index-- { opposite := len(rows) - 1 - index rows[index], rows[opposite] = rows[opposite], rows[index] } }