Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: add DoNotFillSpaceWhenEndOfLine option #318

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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│
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
Comment on lines +773 to +780
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the example I was referring to.

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