package action import ( "strings" tea "github.com/charmbracelet/bubbletea" ) type JoinLines struct { Preserve bool Count int } // WithCount sets the count (required by Repeatable interface) func (m JoinLines) WithCount(n int) Action { m.Count = n return m } func (a JoinLines) Execute(m Model) tea.Cmd { win := m.ActiveWindow() buf := m.ActiveBuffer() y := win.Cursor.Line var line strings.Builder line.WriteString(buf.Line(y)) spacePending := false for range max(1, a.Count-1) { if (y + 1) >= buf.LineCount() { break } l := buf.Line(y + 1) if a.Preserve { line.WriteString(l) } else { l = strings.TrimLeft(l, " ") if l == "" { spacePending = true } else { line.WriteString(" " + l) spacePending = false } } buf.DeleteLine(y + 1) } if !a.Preserve && spacePending { line.WriteString(" ") } if line.String() != buf.Line(y) { buf.SetLine(y, line.String()) } return nil }