Skip to content

Commit

Permalink
another try of aligning refactoring
Browse files Browse the repository at this point in the history
- skip aligning if no rows in result set
- don't truncate long values, such as names
  • Loading branch information
lesovsky committed Oct 3, 2019
1 parent c42a21a commit 77db2dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
24 changes: 16 additions & 8 deletions lib/stat/pgstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,22 @@ func (r *PGresult) Sort(key int, desc bool) {
}

// SetAlign method aligns length of values depending of the columns width
func (r *PGresult) SetAlign(widthes map[int]int, truncLimit int, dynamic bool) {
func (r *PGresult) SetAlign(widthes map[int]int, truncLimit int, dynamic bool) (err error) {
var lastColTruncLimit, lastColMaxWidth int
lastColTruncLimit = truncLimit
truncLimit = utils.Max(truncLimit, colsTruncMinLimit)

/* calculate max length of columns based on the longest value of the column */
var valuelen, colnamelen int
for colidx, colname := range r.Cols { // walk per-column
if len(r.Result) == 0 {
// no rows in result, set width using length of a column name
// no rows in result, set width using length of a column name and return with error (because not aligned using result's values)
if len(r.Result) == 0 {
for colidx, colname := range r.Cols { // walk per-column
widthes[colidx] = len(colname)
}
return fmt.Errorf("RESULT_NO_ROWS")
}

/* calculate max length of columns based on the longest value of the column */
var valuelen, colnamelen int
for colidx, colname := range r.Cols { // walk per-column
for rownum := 0; rownum < len(r.Result); rownum++ { // walk through rows
valuelen = len(r.Result[rownum][colidx].String)
colnamelen = utils.Max(len(colname), colsWidthMin)
Expand All @@ -463,7 +466,11 @@ func (r *PGresult) SetAlign(widthes map[int]int, truncLimit int, dynamic bool) {
if dynamic {
widthes[colidx] = valuelen
} else {
widthes[colidx] = colnamelen
if valuelen > colnamelen*2 {
widthes[colidx] = utils.Min(valuelen, 32)
} else {
widthes[colidx] = colnamelen
}
}
// for last column set width using truncation limit
case colidx == r.Ncols-1:
Expand All @@ -488,6 +495,7 @@ func (r *PGresult) SetAlign(widthes map[int]int, truncLimit int, dynamic bool) {
}
}
}
return nil
}

// aligningIsValueEmpty is the aligning helper: return true if value is empty, e.g. NULL - set width based on colname length
Expand All @@ -500,7 +508,7 @@ func aligningIsLessThanColname(vlen, cnlen, width int) bool {
return vlen > 0 && vlen <= cnlen && vlen >= width
}

// aligningIsMoreThanColname isthe aligning helper: returns true if passed non-empty values, but for if its length longer than length of colnames
// aligningIsMoreThanColname is the aligning helper: returns true if passed non-empty values, but for if its length longer than length of colnames
func aligningIsMoreThanColname(vlen, cnlen, width, trunclim, colidx, cols int) bool {
return vlen > 0 && vlen > cnlen && vlen < trunclim && vlen >= width && colidx < cols-1
}
Expand Down
6 changes: 4 additions & 2 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ func formatReport(d *stat.PGresult, opts *ReportOptions) {

// align values for printing, use dynamic aligning
if !opts.Context.Aligned {
d.SetAlign(opts.Context.ColsWidth, opts.TruncLimit, true)
opts.Context.Aligned = true
err := d.SetAlign(opts.Context.ColsWidth, opts.TruncLimit, true)
if err == nil {
opts.Context.Aligned = true
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions top/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ func printDbstat(v *gocui.View, s stat.Stat) {

// configure aligning, use fixed aligning instead of dynamic
if !ctx.current.Aligned {
s.DiffPGresult.SetAlign(ctx.current.ColsWidth, 1000, false) // we don't want truncate lines here, so just use high limit
ctx.current.Aligned = true
err := s.DiffPGresult.SetAlign(ctx.current.ColsWidth, 1000, false) // we don't want truncate lines here, so just use high limit
if err == nil {
ctx.current.Aligned = true
}
}

// is filter required?
Expand Down

0 comments on commit 77db2dd

Please sign in to comment.