|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + core "github.com/statloc/core" |
| 9 | +) |
| 10 | + |
| 11 | +func GetTable(items map[string]*core.TableItem) string { |
| 12 | + // im sorry for that |
| 13 | + maxTitleLength, maxLOCLength, maxFilesLength := 5, 3, 5 |
| 14 | + for title, item := range items { |
| 15 | + if len(title) > maxTitleLength { |
| 16 | + maxTitleLength = len(title) |
| 17 | + } |
| 18 | + |
| 19 | + LOC := strconv.FormatUint(item.LOC, 10) |
| 20 | + if len(LOC) > maxLOCLength { |
| 21 | + maxLOCLength = len(LOC) |
| 22 | + } |
| 23 | + |
| 24 | + files := strconv.FormatUint(item.Files, 10) |
| 25 | + if len(files) > maxFilesLength { |
| 26 | + maxFilesLength = len(files) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + separator := fmt.Sprintf( |
| 31 | + "+-%s-+-%s-+-%s-+\n", |
| 32 | + strings.Repeat("-", maxTitleLength), |
| 33 | + strings.Repeat("-", maxLOCLength), |
| 34 | + strings.Repeat("-", maxFilesLength), |
| 35 | + ) |
| 36 | + |
| 37 | + result := fmt.Sprint( |
| 38 | + separator, |
| 39 | + fmt.Sprintf( |
| 40 | + "| Title%s | LOC%s | Files%s |\n", |
| 41 | + strings.Repeat(" ", maxTitleLength - 5), |
| 42 | + strings.Repeat(" ", maxLOCLength - 3), |
| 43 | + strings.Repeat(" ", maxFilesLength - 5), |
| 44 | + ), |
| 45 | + separator, |
| 46 | + ) |
| 47 | + |
| 48 | + for title, item := range items { |
| 49 | + result += fmt.Sprintf( |
| 50 | + "| %s%s | %d%s | %d%s |\n", |
| 51 | + title, strings.Repeat(" ", maxTitleLength - len(title)), |
| 52 | + item.LOC, strings.Repeat(" ", maxLOCLength - len(strconv.FormatUint(item.LOC, 10))), |
| 53 | + item.Files, strings.Repeat(" ", maxFilesLength - len(strconv.FormatUint(item.Files, 10))), |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + result += separator |
| 58 | + return result |
| 59 | +} |
0 commit comments