blob: 7393c1160771c9ef02f90ece3920f0148392956b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// Package output implements means of outputting the results of mongotop's
// queries against MongoDB.
package output
import (
"fmt"
"github.com/mongodb/mongo-tools/common/util"
"github.com/mongodb/mongo-tools/mongotop/command"
"strings"
)
// Interface to output the results of the top command.
type Outputter interface {
Output(command.Diff) error
}
// Outputter that formats the results and prints them to the terminal.
type TerminalOutputter struct {
}
func (self *TerminalOutputter) Output(diff command.Diff) error {
tableRows := diff.ToRows()
// get the length of the longest row (the one with the most fields)
longestRow := 0
for _, row := range tableRows {
longestRow = util.MaxInt(longestRow, len(row))
}
// bookkeep the length of the longest member of each column
longestFields := make([]int, longestRow)
for _, row := range tableRows {
for idx, field := range row {
longestFields[idx] = util.MaxInt(longestFields[idx], len(field))
}
}
// write out each row
for _, row := range tableRows {
for idx, rowEl := range row {
fmt.Printf("\t\t%v%v", strings.Repeat(" ",
longestFields[idx]-len(rowEl)), rowEl)
}
fmt.Printf("\n")
}
fmt.Printf("\n")
return nil
}
|