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

Add SetSelectorWrap #703

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion table.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ type Table struct {
// versa).
wrapHorizontally, wrapVertically bool

// When set to true, this flag will cause the selector to wrap around the text
// instead of spanning across the entire column width.
selectorWrap bool

// The number of rows/columns by which the table is scrolled down/to the
// right.
rowOffset, columnOffset int
Expand Down Expand Up @@ -829,6 +833,16 @@ func (t *Table) SetWrapSelection(vertical, horizontal bool) *Table {
return t
}

// SetSelectorWrap will ensure that the selector does not extend across the entire
// column width, instead it will wrap around the length of the text in the currently
// selected TableCell.
//
// The default value is false.
func (t *Table) SetSelectorWrap(wrap bool) *Table {
t.selectorWrap = wrap
return t
}

// Draw draws this primitive onto the screen.
func (t *Table) Draw(screen tcell.Screen) {
t.Box.DrawForSubclass(screen, t)
Expand Down Expand Up @@ -1258,9 +1272,27 @@ func (t *Table) Draw(screen tcell.Screen) {
continue
}
bx, by, bw, bh := x+columnX, y+rowY, columnWidth+1, 1
if t.selectorWrap {
textlen := TaggedStringWidth(cell.Text)
if columnWidth+1 >= textlen {
if cell.Align == AlignRight {
bx += (columnWidth - textlen)
} else if cell.Align == AlignCenter {
bx += ((columnWidth - textlen) / 2) + 1
} else if cell.Align == AlignLeft && t.borders {
bx += 2
}

bw = textlen
} else if columnWidth+1 < textlen {
bw = columnWidth + 1
}
}
if t.borders {
by = y + rowY*2
bw++
if !t.selectorWrap {
bw++
}
bh = 3
}
columnSelected := t.columnsSelectable && !t.rowsSelectable && column == t.selectedColumn
Expand Down