diff --git a/table/style.go b/table/style.go index cb850e8..b575dd0 100644 --- a/table/style.go +++ b/table/style.go @@ -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 ( diff --git a/table/table.go b/table/table.go index 6776333..e9146d2 100644 --- a/table/table.go +++ b/table/table.go @@ -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 @@ -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 { diff --git a/text/align.go b/text/align.go index 2a1068a..6b0fb66 100644 --- a/text/align.go +++ b/text/align.go @@ -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.: @@ -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) }