@@ -56,6 +56,7 @@ type List[T Item] interface {
5656 SelectWord(col, line int)
5757 SelectParagraph(col, line int)
5858 GetSelectedText(paddingLeft int) string
59+ HasSelection() bool
5960}
6061
6162type direction int
@@ -286,30 +287,10 @@ func (l *list[T]) handleMouseWheel(msg tea.MouseWheelMsg) (tea.Model, tea.Cmd) {
286287 return l, cmd
287288}
288289
289−// View implements List.
290−func (l *list[T]) View() string {
291− if l.height <= 0 || l.width <= 0 {
292− return ""
293− }
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.
292+func (l *list[T]) selectionView(view string, textOnly bool) string {
294293 t := styles.CurrentTheme()
295− view := l.rendered
296− lines := strings.Split(view, "\n")
297−
298− start, end := l.viewPosition()
299− viewStart := max(0, start)
300− viewEnd := min(len(lines), end+1)
301− lines = lines[viewStart:viewEnd]
302− if l.resize {
303− return strings.Join(lines, "\n")
304− }
305− view = t.S().Base.
306− Height(l.height).
307− Width(l.width).
308− Render(strings.Join(lines, "\n"))
309−
310− if !l.hasSelection() {
311− return view
312− }
313294 area := uv.Rect(0, 0, l.width, l.height)
314295 scr := uv.NewScreenBuffer(area.Dx(), area.Dy())
315296 uv.NewStyledString(view).Draw(scr, area)
@@ -397,6 +378,8 @@ func (l *list[T]) View() string {
397378 lineTextBounds[y] = bounds
398379 }
399380
381+ var selectedText strings.Builder
382+
400383 // Second pass: apply selection highlighting
401384 for y := range scr.Height() {
402385 selBounds := lineSelections[y]
@@ -421,16 +404,59 @@ func (l *list[T]) View() string {
421404
422405 cellStr := cell.String()
423406 if len(cellStr) > 0 && !specialChars[cellStr] {
407+ if textOnly {
408+ // Collect selected text without styles
409+ selectedText.WriteString(cell.String())
410+ continue
411+ }
412+
424413 cell = cell.Clone()
425414 cell.Style = cell.Style.Background(t.BgOverlay).Foreground(t.White)
426415 scr.SetCell(x, y, cell)
427416 }
428417 }
418+
419+ if textOnly {
420+ // Make sure we add a newline after each line of selected text
421+ selectedText.WriteByte('\n')
422+ }
423+ }
424+
425+ if textOnly {
426+ return strings.TrimSpace(selectedText.String())
429427 }
430428
431429 return scr.Render()
432430}
433431
432+// View implements List.
433+func (l *list[T]) View() string {
434+ if l.height <= 0 || l.width <= 0 {
435+ return ""
436+ }
437+ t := styles.CurrentTheme()
438+ view := l.rendered
439+ lines := strings.Split(view, "\n")
440+
441+ start, end := l.viewPosition()
442+ viewStart := max(0, start)
443+ viewEnd := min(len(lines), end+1)
444+ lines = lines[viewStart:viewEnd]
445+ if l.resize {
446+ return strings.Join(lines, "\n")
447+ }
448+ view = t.S().Base.
449+ Height(l.height).
450+ Width(l.width).
451+ Render(strings.Join(lines, "\n"))
452+
453+ if !l.hasSelection() {
454+ return view
455+ }
456+
457+ return l.selectionView(view, false)
458+}
459+
434460func (l *list[T]) viewPosition() (int, int) {
435461 start, end := 0, 0
436462 renderedLines := lipgloss.Height(l.rendered) - 1
@@ -1374,69 +1400,16 @@ func (l *list[T]) SelectParagraph(col, line int) {
13741400 l.selectionActive = false // Not actively selecting, just selected
13751401}
13761402
1403+// HasSelection returns whether there is an active selection.
1404+func (l *list[T]) HasSelection() bool {
1405+ return l.hasSelection()
1406+}
1407+
13771408// GetSelectedText returns the currently selected text.
13781409func (l *list[T]) GetSelectedText(paddingLeft int) string {
1379− return ""
1380− // if !l.hasSelection() {
1381− // return ""
1382− // }
1383− //
1384− // startLine := l.selectionStartLine
1385− // endLine := l.selectionEndLine
1386− // startCol := l.selectionStartCol
1387− // endCol := l.selectionEndCol
1388− //
1389− // if l.direction == DirectionBackward {
1390− // startLine = (lipgloss.Height(l.rendered) - 1) - startLine
1391− // endLine = (lipgloss.Height(l.rendered) - 1) - endLine
1392− // }
1393− //
1394− // if l.offset > 0 {
1395− // if l.direction == DirectionBackward {
1396− // startLine += l.offset
1397− // endLine += l.offset
1398− // } else {
1399− // startLine -= l.offset
1400− // endLine -= l.offset
1401− // }
1402− // }
1403− //
1404− // lines := strings.Split(l.rendered, "\n")
1405− //
1406− // if startLine < 0 || endLine < 0 || startLine >= len(lines) || endLine >= len(lines) {
1407− // return ""
1408− // }
1409− //
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, " ")
1415− // }
1416− //
1417− // if i == startLine {
1418− // if startCol < 0 || startCol >= len(lines[i]) {
1419− // startCol = 0
1420− // }
1421− // if startCol < paddingLeft {
1422− // startCol = paddingLeft
1423− // }
1424− // if i != endLine {
1425− // endCol = len(lines[i])
1426− // }
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])
1433− // }
1434− // if endCol < paddingLeft {
1435− // endCol = paddingLeft
1436− // }
1437− // result.WriteString(strings.TrimRightFunc(lines[i][paddingLeft:endCol], unicode.IsSpace))
1438− // }
1439− // }
1440− //
1441− // return result.String()
1410+ if !l.hasSelection() {
1411+ return ""
1412+ }
1413+
1414+ return l.selectionView(l.View(), true)
14421415}