@@ -56,6 +56,7 @@ type List[T Item] interface {
56 SelectWord(col, line int)
56 SelectWord(col, line int)
57 SelectParagraph(col, line int)
57 SelectParagraph(col, line int)
58 GetSelectedText(paddingLeft int) string
58 GetSelectedText(paddingLeft int) string
61type direction int
62type direction int
@@ -286,30 +287,10 @@ func (l *list[T]) handleMouseWheel(msg tea.MouseWheelMsg) (tea.Model, tea.Cmd) {
286 return l, cmd
287 return l, cmd
289−// View implements List.
290+// selectionView renders the highlighted selection in the view and returns it
291+// as a string. If textOnly is true, it won't render any styles.
290−func (l *list[T]) View() string {
292+func (l *list[T]) selectionView(view string, textOnly bool) string {
291− if l.height <= 0 || l.width <= 0 {
294 t := styles.CurrentTheme()
293 t := styles.CurrentTheme()
296− lines := strings.Split(view, "\n")
298− start, end := l.viewPosition()
299− viewStart := max(0, start)
300− viewEnd := min(len(lines), end+1)
301− lines = lines[viewStart:viewEnd]
303− return strings.Join(lines, "\n")
308− Render(strings.Join(lines, "\n"))
310− if !l.hasSelection() {
313 area := uv.Rect(0, 0, l.width, l.height)
294 area := uv.Rect(0, 0, l.width, l.height)
314 scr := uv.NewScreenBuffer(area.Dx(), area.Dy())
295 scr := uv.NewScreenBuffer(area.Dx(), area.Dy())
315 uv.NewStyledString(view).Draw(scr, area)
296 uv.NewStyledString(view).Draw(scr, area)
@@ -397,6 +378,8 @@ func (l *list[T]) View() string {
397 lineTextBounds[y] = bounds
378 lineTextBounds[y] = bounds
381+ var selectedText strings.Builder
400 // Second pass: apply selection highlighting
383 // Second pass: apply selection highlighting
401 for y := range scr.Height() {
384 for y := range scr.Height() {
402 selBounds := lineSelections[y]
385 selBounds := lineSelections[y]
@@ -406,6 +389,11 @@ func (l *list[T]) View() string {
407 textBounds := lineTextBounds[y]
390 textBounds := lineTextBounds[y]
408 if textBounds.start < 0 {
391 if textBounds.start < 0 {
393+ // We don't want to get rid of all empty lines in text-only mode
394+ selectedText.WriteByte('\n')
409 continue // No text on this line
397 continue // No text on this line
@@ -421,16 +409,63 @@ func (l *list[T]) View() string {
422 cellStr := cell.String()
410 cellStr := cell.String()
423 if len(cellStr) > 0 && !specialChars[cellStr] {
411 if len(cellStr) > 0 && !specialChars[cellStr] {
413+ // Collect selected text without styles
414+ selectedText.WriteString(cell.String())
418+ // Text selection styling, which is a Lip Gloss style. We must
419+ // extract the values to use in a UV style, below.
420+ ts := t.TextSelection
424 cell = cell.Clone()
422 cell = cell.Clone()
425− cell.Style = cell.Style.Background(t.BgOverlay).Foreground(t.White)
423+ cell.Style = cell.Style.Background(ts.GetBackground()).Foreground(ts.GetForeground())
426 scr.SetCell(x, y, cell)
424 scr.SetCell(x, y, cell)
429+ // Make sure we add a newline after each line of selected text
430+ selectedText.WriteByte('\n')
435+ return strings.TrimSpace(selectedText.String())
431 return scr.Render()
438 return scr.Render()
441+// View implements List.
442+func (l *list[T]) View() string {
443+ if l.height <= 0 || l.width <= 0 {
446+ t := styles.CurrentTheme()
448+ lines := strings.Split(view, "\n")
450+ start, end := l.viewPosition()
451+ viewStart := max(0, start)
452+ viewEnd := min(len(lines), end+1)
453+ lines = lines[viewStart:viewEnd]
455+ return strings.Join(lines, "\n")
460+ Render(strings.Join(lines, "\n"))
462+ if !l.hasSelection() {
466+ return l.selectionView(view, false)
434func (l *list[T]) viewPosition() (int, int) {
469func (l *list[T]) viewPosition() (int, int) {
435 start, end := 0, 0
470 start, end := 0, 0
436 renderedLines := lipgloss.Height(l.rendered) - 1
471 renderedLines := lipgloss.Height(l.rendered) - 1
@@ -1374,69 +1409,16 @@ func (l *list[T]) SelectParagraph(col, line int) {
1374 l.selectionActive = false // Not actively selecting, just selected
1409 l.selectionActive = false // Not actively selecting, just selected
1412+// HasSelection returns whether there is an active selection.
1413+func (l *list[T]) HasSelection() bool {
1414+ return l.hasSelection()
1377// GetSelectedText returns the currently selected text.
1417// GetSelectedText returns the currently selected text.
1378func (l *list[T]) GetSelectedText(paddingLeft int) string {
1418func (l *list[T]) GetSelectedText(paddingLeft int) string {
1380− // if !l.hasSelection() {
1419+ if !l.hasSelection() {
1381− // return ""
1420+ return ""
1384− // startLine := l.selectionStartLine
1423+ return l.selectionView(l.View(), true)
1385− // endLine := l.selectionEndLine
1386− // startCol := l.selectionStartCol
1387− // endCol := l.selectionEndCol
1389− // if l.direction == DirectionBackward {
1390− // startLine = (lipgloss.Height(l.rendered) - 1) - startLine
1391− // endLine = (lipgloss.Height(l.rendered) - 1) - endLine
1394− // if l.offset > 0 {
1395− // if l.direction == DirectionBackward {
1396− // startLine += l.offset
1397− // endLine += l.offset
1399− // startLine -= l.offset
1400− // endLine -= l.offset
1404− // lines := strings.Split(l.rendered, "\n")
1406− // if startLine < 0 || endLine < 0 || startLine >= len(lines) || endLine >= len(lines) {
1410− // var result strings.Builder
1411− // for i := range lines {
1412− // lines[i] = ansi.Strip(lines[i])
1413− // for _, icon := range styles.SelectionIgnoreIcons {
1414− // lines[i] = strings.ReplaceAll(lines[i], icon, " ")
1417− // if i == startLine {
1418− // if startCol < 0 || startCol >= len(lines[i]) {
1421− // if startCol < paddingLeft {
1422− // startCol = paddingLeft
1424− // if i != endLine {
1425− // endCol = len(lines[i])
1427− // result.WriteString(strings.TrimRightFunc(lines[i][startCol:endCol], unicode.IsSpace))
1428− // } else if i > startLine && i < endLine {
1429− // result.WriteString(strings.TrimRightFunc(lines[i][paddingLeft:], unicode.IsSpace))
1430− // } else if i == endLine {
1431− // if endCol < 0 || endCol >= len(lines[i]) {
1432− // endCol = len(lines[i])
1434− // if endCol < paddingLeft {
1435− // endCol = paddingLeft
1437− // result.WriteString(strings.TrimRightFunc(lines[i][paddingLeft:endCol], unicode.IsSpace))
1441− // return result.String()