Skip to content

Commit

Permalink
table: add DoNotFillSpaceWhenEndOfLine option
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar committed Apr 10, 2024
1 parent 027182a commit a1b789b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
13 changes: 13 additions & 0 deletions table/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,19 @@ type Options struct {
// │ │ │ TOTAL │ 10000 │ │
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
SeparateRows bool

// DoNotFillSpaceWhenEndOfLine disables filling the space at the end of each
// line for AlignCenter and AlignLeft.It should be used when not draw border.
// Example of a table where it is enabled:
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
// │ # │ FIRST NAME │ LAST NAME │ SALARY ││
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ 1 │ Arya │ Stark │ 3000 ││
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
// │ 300 │ Tyrion │ Lannister │ 5000 │test│
// │ │ │ TOTAL │ 10000 │anothertest│
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
DoNotFillSpaceWhenEndOfLine bool
}

var (
Expand Down
6 changes: 6 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ func (t *Table) getAlign(colIdx int, hint renderHint) text.Align {
align = text.AlignRight
} else if hint.isAutoIndexRow {
align = text.AlignCenter
} else if t.style.Options.DoNotFillSpaceWhenEndOfLine && t.isLastColumn(colIdx) {
align = text.AlignDisabled
}
}
return align
Expand Down Expand Up @@ -687,6 +689,10 @@ func (t *Table) isIndexColumn(colIdx int, hint renderHint) bool {
return t.indexColumn == colIdx+1 || hint.isAutoIndexColumn
}

func (t *Table) isLastColumn(colIdx int) bool {
return colIdx == t.numColumns-1
}

func (t *Table) render(out *strings.Builder) string {
outStr := out.String()
if t.supressTrailingSpaces {
Expand Down
20 changes: 14 additions & 6 deletions text/align.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ type Align int

// Align enumerations
const (
AlignDefault Align = iota // same as AlignLeft
AlignLeft // "left "
AlignCenter // " center "
AlignJustify // "justify it"
AlignRight // " right"
AlignAuto // AlignRight for numbers, AlignLeft for the rest
AlignDefault Align = iota // same as AlignLeft
AlignLeft // "left "
AlignCenter // " center "
AlignJustify // "justify it"
AlignRight // " right"
AlignAuto // AlignRight for numbers, AlignLeft for the rest
AlignDisabled // "left"
AlignCenterWithoutRightSpace // " center"
)

// Apply aligns the text as directed. For ex.:
Expand Down Expand Up @@ -55,6 +57,12 @@ func (a Align) Apply(text string, maxLength int) string {
}
case AlignJustify:
return justifyText(text, sLenWoE, maxLength)
case AlignDisabled:
return text
case AlignCenterWithoutRightSpace:
if sLenWoE < maxLength {
return strings.Repeat(" ", (maxLength-sLenWoE+1)/2) + text
}
}
return fmt.Sprintf("%"+strconv.Itoa(maxLength+numEscChars)+"s", text)
}
Expand Down

0 comments on commit a1b789b

Please sign in to comment.