@@ -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]
@@ -421,16 +404,59 @@ func (l *list[T]) View() string {
422 cellStr := cell.String()
405 cellStr := cell.String()
423 if len(cellStr) > 0 && !specialChars[cellStr] {
406 if len(cellStr) > 0 && !specialChars[cellStr] {
408+ // Collect selected text without styles
409+ selectedText.WriteString(cell.String())
424 cell = cell.Clone()
413 cell = cell.Clone()
425 cell.Style = cell.Style.Background(t.BgOverlay).Foreground(t.White)
414 cell.Style = cell.Style.Background(t.BgOverlay).Foreground(t.White)
426 scr.SetCell(x, y, cell)
415 scr.SetCell(x, y, cell)
420+ // Make sure we add a newline after each line of selected text
421+ selectedText.WriteByte('\n')
426+ return strings.TrimSpace(selectedText.String())
431 return scr.Render()
429 return scr.Render()
432+// View implements List.
433+func (l *list[T]) View() string {
434+ if l.height <= 0 || l.width <= 0 {
437+ t := styles.CurrentTheme()
439+ lines := strings.Split(view, "\n")
441+ start, end := l.viewPosition()
442+ viewStart := max(0, start)
443+ viewEnd := min(len(lines), end+1)
444+ lines = lines[viewStart:viewEnd]
446+ return strings.Join(lines, "\n")
451+ Render(strings.Join(lines, "\n"))
453+ if !l.hasSelection() {
457+ return l.selectionView(view, false)
434func (l *list[T]) viewPosition() (int, int) {
460func (l *list[T]) viewPosition() (int, int) {
435 start, end := 0, 0
461 start, end := 0, 0
436 renderedLines := lipgloss.Height(l.rendered) - 1
462 renderedLines := lipgloss.Height(l.rendered) - 1
@@ -1374,69 +1400,16 @@ func (l *list[T]) SelectParagraph(col, line int) {
1374 l.selectionActive = false // Not actively selecting, just selected
1400 l.selectionActive = false // Not actively selecting, just selected
1403+// HasSelection returns whether there is an active selection.
1404+func (l *list[T]) HasSelection() bool {
1405+ return l.hasSelection()
1377// GetSelectedText returns the currently selected text.
1408// GetSelectedText returns the currently selected text.
1378func (l *list[T]) GetSelectedText(paddingLeft int) string {
1409func (l *list[T]) GetSelectedText(paddingLeft int) string {
1380− // if !l.hasSelection() {
1410+ if !l.hasSelection() {
1381− // return ""
1411+ return ""
1384− // startLine := l.selectionStartLine
1414+ 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()