Skip to content

Commit

Permalink
fix cognitive complexity smells
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Aug 20, 2021
1 parent eaf4190 commit 8f37028
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 240 deletions.
38 changes: 21 additions & 17 deletions list/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func (l *List) renderItem(out *strings.Builder, idx int, item *listItem, hint re

// render the prefix or the leading text before the actual item
l.renderItemBulletPrefix(out, idx, item.Level, lineIdx, hint)
l.renderItemBullet(out, idx, item.Level, lineIdx, hint)
l.renderItemBullet(out, lineIdx, hint)

// render the actual item
out.WriteString(lineStr)
}
}

func (l *List) renderItemBullet(out *strings.Builder, itemIdx int, itemLevel int, lineIdx int, hint renderHint) {
func (l *List) renderItemBullet(out *strings.Builder, lineIdx int, hint renderHint) {
if lineIdx > 0 {
// multi-line item.Text
if hint.isLastItem {
Expand All @@ -73,24 +73,28 @@ func (l *List) renderItemBullet(out *strings.Builder, itemIdx int, itemLevel int
out.WriteString(l.style.CharItemVertical)
}
} else {
// single-line item.Text (or first line of a multi-line item.Text)
if hint.isOnlyItem {
if hint.isTopItem {
out.WriteString(l.style.CharItemSingle)
} else {
out.WriteString(l.style.CharItemBottom)
}
} else if hint.isTopItem {
out.WriteString(l.style.CharItemTop)
} else if hint.isFirstItem {
out.WriteString(l.style.CharItemFirst)
} else if hint.isBottomItem || hint.isLastItem {
out.WriteString(l.style.CharItemBottom)
l.renderItemBulletSingleLine(out, lineIdx, hint)
}
}

func (l *List) renderItemBulletSingleLine(out *strings.Builder, lineIdx int, hint renderHint) {
// single-line item.Text (or first line of a multi-line item.Text)
if hint.isOnlyItem {
if hint.isTopItem {
out.WriteString(l.style.CharItemSingle)
} else {
out.WriteString(l.style.CharItemMiddle)
out.WriteString(l.style.CharItemBottom)
}
out.WriteRune(' ')
} else if hint.isTopItem {
out.WriteString(l.style.CharItemTop)
} else if hint.isFirstItem {
out.WriteString(l.style.CharItemFirst)
} else if hint.isBottomItem || hint.isLastItem {
out.WriteString(l.style.CharItemBottom)
} else {
out.WriteString(l.style.CharItemMiddle)
}
out.WriteRune(' ')
}

func (l *List) renderItemBulletPrefix(out *strings.Builder, itemIdx int, itemLevel int, lineIdx int, hint renderHint) {
Expand Down
38 changes: 21 additions & 17 deletions progress/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,30 +318,34 @@ func (p *Progress) renderTrackerStats(out *strings.Builder, t *Tracker, hint ren
outStats.WriteString(" in ")
}
if !hint.hideTime {
var td, tp time.Duration
if t.IsDone() {
td = t.timeStop.Sub(t.timeStart)
} else {
td = time.Since(t.timeStart)
}
if hint.isOverallTracker {
tp = p.style.Options.TimeOverallPrecision
} else if t.IsDone() {
tp = p.style.Options.TimeDonePrecision
} else {
tp = p.style.Options.TimeInProgressPrecision
}
outStats.WriteString(p.style.Colors.Time.Sprint(td.Round(tp)))
if p.showETA || hint.isOverallTracker {
p.renderTrackerStatsETA(&outStats, t, hint)
}
p.renderTrackerStatsTime(&outStats, t, hint)
}
outStats.WriteRune(']')

out.WriteString(p.style.Colors.Stats.Sprint(outStats.String()))
}
}

func (p *Progress) renderTrackerStatsTime(outStats *strings.Builder, t *Tracker, hint renderHint) {
var td, tp time.Duration
if t.IsDone() {
td = t.timeStop.Sub(t.timeStart)
} else {
td = time.Since(t.timeStart)
}
if hint.isOverallTracker {
tp = p.style.Options.TimeOverallPrecision
} else if t.IsDone() {
tp = p.style.Options.TimeDonePrecision
} else {
tp = p.style.Options.TimeInProgressPrecision
}
outStats.WriteString(p.style.Colors.Time.Sprint(td.Round(tp)))
if p.showETA || hint.isOverallTracker {
p.renderTrackerStatsETA(outStats, t, hint)
}
}

func (p *Progress) renderTrackerStatsETA(out *strings.Builder, t *Tracker, hint renderHint) {
tpETA := p.style.Options.ETAPrecision
if eta := t.ETA().Round(tpETA); hint.isOverallTracker || eta > tpETA {
Expand Down
111 changes: 38 additions & 73 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {
out.WriteRune('\n')
}

// use a brand new strings.Builder if a row length limit has been set
// use a brand-new strings.Builder if a row length limit has been set
var outLine *strings.Builder
if t.allowedRowLength > 0 {
outLine = &strings.Builder{}
Expand All @@ -202,16 +202,7 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {

// merge the strings.Builder objects if a new one was created earlier
if outLine != out {
outLineStr := outLine.String()
if text.RuneCount(outLineStr) > t.allowedRowLength {
trimLength := t.allowedRowLength - utf8.RuneCountInString(t.style.Box.UnfinishedRow)
if trimLength > 0 {
out.WriteString(text.Trim(outLineStr, trimLength))
out.WriteString(t.style.Box.UnfinishedRow)
}
} else {
out.WriteString(outLineStr)
}
t.renderLineMergeOutputs(out, outLine)
}

// if a page size has been set, and said number of lines has already
Expand All @@ -229,27 +220,22 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {
}
}

func (t *Table) renderMarginLeft(out *strings.Builder, hint renderHint) {
if t.style.Options.DrawBorder {
border := t.style.Box.Left
if hint.isBorderTop {
if t.title != "" {
border = t.style.Box.LeftSeparator
} else {
border = t.style.Box.TopLeft
}
} else if hint.isBorderBottom {
border = t.style.Box.BottomLeft
} else if hint.isSeparatorRow {
if t.autoIndex && hint.isHeaderOrFooterSeparator() {
border = t.style.Box.Left
} else if !t.autoIndex && t.shouldMergeCellsVertically(0, hint) {
border = t.style.Box.Left
} else {
border = t.style.Box.LeftSeparator
}
func (t *Table) renderLineMergeOutputs(out *strings.Builder, outLine *strings.Builder) {
outLineStr := outLine.String()
if text.RuneCount(outLineStr) > t.allowedRowLength {
trimLength := t.allowedRowLength - utf8.RuneCountInString(t.style.Box.UnfinishedRow)
if trimLength > 0 {
out.WriteString(text.Trim(outLineStr, trimLength))
out.WriteString(t.style.Box.UnfinishedRow)
}
} else {
out.WriteString(outLineStr)
}
}

func (t *Table) renderMarginLeft(out *strings.Builder, hint renderHint) {
if t.style.Options.DrawBorder {
border := t.getBorderLeft(hint)
colors := t.getBorderColors(hint)
if colors.EscapeSeq() != "" {
out.WriteString(colors.Sprint(border))
Expand All @@ -261,23 +247,7 @@ func (t *Table) renderMarginLeft(out *strings.Builder, hint renderHint) {

func (t *Table) renderMarginRight(out *strings.Builder, hint renderHint) {
if t.style.Options.DrawBorder {
border := t.style.Box.Right
if hint.isBorderTop {
if t.title != "" {
border = t.style.Box.RightSeparator
} else {
border = t.style.Box.TopRight
}
} else if hint.isBorderBottom {
border = t.style.Box.BottomRight
} else if hint.isSeparatorRow {
if t.shouldMergeCellsVertically(t.numColumns-1, hint) {
border = t.style.Box.Right
} else {
border = t.style.Box.RightSeparator
}
}

border := t.getBorderRight(hint)
colors := t.getBorderColors(hint)
if colors.EscapeSeq() != "" {
out.WriteString(colors.Sprint(border))
Expand All @@ -292,16 +262,7 @@ func (t *Table) renderRow(out *strings.Builder, row rowStr, hint renderHint) {
// fit every column into the allowedColumnLength/maxColumnLength limit
// and in the process find the max. number of lines in any column in
// this row
colMaxLines := 0
rowWrapped := make(rowStr, len(row))
for colIdx, colStr := range row {
widthEnforcer := t.columnConfigMap[colIdx].getWidthMaxEnforcer()
rowWrapped[colIdx] = widthEnforcer(colStr, t.maxColumnLengths[colIdx])
colNumLines := strings.Count(rowWrapped[colIdx], "\n") + 1
if colNumLines > colMaxLines {
colMaxLines = colNumLines
}
}
colMaxLines, rowWrapped := t.wrapRow(row)

// if there is just 1 line in all columns, add the row as such; else
// split each column into individual lines and render them one-by-one
Expand Down Expand Up @@ -410,22 +371,26 @@ func (t *Table) renderTitle(out *strings.Builder) {
}
titleText := text.WrapText(t.title, lenText)
for _, titleLine := range strings.Split(titleText, "\n") {
titleLine = strings.TrimSpace(titleLine)
titleLine = t.style.Title.Format.Apply(titleLine)
titleLine = t.style.Title.Align.Apply(titleLine, lenText)
titleLine = t.style.Box.PaddingLeft + titleLine + t.style.Box.PaddingRight
titleLine = t.style.Title.Colors.Sprint(titleLine)

if out.Len() > 0 {
out.WriteRune('\n')
}
if t.style.Options.DrawBorder {
out.WriteString(t.style.Box.Left)
}
out.WriteString(titleLine)
if t.style.Options.DrawBorder {
out.WriteString(t.style.Box.Right)
}
t.renderTitleLine(out, lenText, titleLine)
}
}
}

func (t *Table) renderTitleLine(out *strings.Builder, lenText int, titleLine string) {
titleLine = strings.TrimSpace(titleLine)
titleLine = t.style.Title.Format.Apply(titleLine)
titleLine = t.style.Title.Align.Apply(titleLine, lenText)
titleLine = t.style.Box.PaddingLeft + titleLine + t.style.Box.PaddingRight
titleLine = t.style.Title.Colors.Sprint(titleLine)

if out.Len() > 0 {
out.WriteRune('\n')
}
if t.style.Options.DrawBorder {
out.WriteString(t.style.Box.Left)
}
out.WriteString(titleLine)
if t.style.Options.DrawBorder {
out.WriteString(t.style.Box.Right)
}
}
47 changes: 28 additions & 19 deletions table/render_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func (t *Table) RenderHTML() string {
return t.render(&out)
}

func (t *Table) htmlGetColStrAndTag(row rowStr, colIdx int, hint renderHint) (string, string) {
// get the column contents
var colStr string
if colIdx < len(row) {
colStr = row[colIdx]
}

// header uses "th" instead of "td"
colTagName := "td"
if hint.isHeaderRow {
colTagName = "th"
}

return colStr, colTagName
}

func (t *Table) htmlRenderCaption(out *strings.Builder) {
if t.caption != "" {
out.WriteString(" <caption class=\"caption\" style=\"caption-side: bottom;\">")
Expand All @@ -86,6 +102,16 @@ func (t *Table) htmlRenderCaption(out *strings.Builder) {
}
}

func (t *Table) htmlRenderColumn(out *strings.Builder, colStr string) {
if t.style.HTML.EscapeText {
colStr = html.EscapeString(colStr)
}
if t.style.HTML.Newline != "\n" {
colStr = strings.Replace(colStr, "\n", t.style.HTML.Newline, -1)
}
out.WriteString(colStr)
}

func (t *Table) htmlRenderColumnAttributes(out *strings.Builder, row rowStr, colIdx int, hint renderHint) {
// determine the HTML "align"/"valign" property values
align := t.getAlign(colIdx, hint).HTMLProperty()
Expand Down Expand Up @@ -131,18 +157,7 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
t.htmlRenderColumnAutoIndex(out, hint)
}

// get the column contents
var colStr string
if colIdx < len(row) {
colStr = row[colIdx]
}

// header uses "th" instead of "td"
colTagName := "td"
if hint.isHeaderRow {
colTagName = "th"
}

colStr, colTagName := t.htmlGetColStrAndTag(row, colIdx, hint)
// write the row
out.WriteString(" <")
out.WriteString(colTagName)
Expand All @@ -151,13 +166,7 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
if len(colStr) == 0 {
out.WriteString(t.style.HTML.EmptyColumn)
} else {
if t.style.HTML.EscapeText {
colStr = html.EscapeString(colStr)
}
if t.style.HTML.Newline != "\n" {
colStr = strings.Replace(colStr, "\n", t.style.HTML.Newline, -1)
}
out.WriteString(colStr)
t.htmlRenderColumn(out, colStr)
}
out.WriteString("</")
out.WriteString(colTagName)
Expand Down
23 changes: 13 additions & 10 deletions table/render_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,7 @@ func (t *Table) markdownRenderRow(out *strings.Builder, row rowStr, hint renderH
// render each column up to the max. columns seen in all the rows
out.WriteRune('|')
for colIdx := 0; colIdx < t.numColumns; colIdx++ {
// auto-index column
if colIdx == 0 && t.autoIndex {
out.WriteRune(' ')
if hint.isSeparatorRow {
out.WriteString("---:")
} else if hint.isRegularRow() {
out.WriteString(fmt.Sprintf("%d ", hint.rowNumber))
}
out.WriteRune('|')
}
t.markdownRenderRowAutoIndex(out, colIdx, hint)

if hint.isSeparatorRow {
out.WriteString(t.getAlign(colIdx, hint).MarkdownProperty())
Expand All @@ -76,6 +67,18 @@ func (t *Table) markdownRenderRow(out *strings.Builder, row rowStr, hint renderH
}
}

func (t *Table) markdownRenderRowAutoIndex(out *strings.Builder, colIdx int, hint renderHint) {
if colIdx == 0 && t.autoIndex {
out.WriteRune(' ')
if hint.isSeparatorRow {
out.WriteString("---:")
} else if hint.isRegularRow() {
out.WriteString(fmt.Sprintf("%d ", hint.rowNumber))
}
out.WriteRune('|')
}
}

func (t *Table) markdownRenderRows(out *strings.Builder, rows []rowStr, hint renderHint) {
if len(rows) > 0 {
for idx, row := range rows {
Expand Down
Loading

0 comments on commit 8f37028

Please sign in to comment.