Skip to content

Commit

Permalink
feat: allow viewport to wrap lines
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionMan1212 committed Jan 14, 2022
1 parent 465a66e commit 23842ea
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap"
"github.com/muesli/reflow/wrap"
)

// WrapType sets the type of line wrapping the viewport will have.
type WrapType int

// Line wrap options.
const (
// NoWrap leaves the lines as is.
NoWrap WrapType = iota

// Wrap will try to wrap lines on words first
// and will wrap on characters if it encounters long words.
Wrap

// SoftWrapOnly wraps lines on words only.
SoftWrapOnly

// HardWrapOnly Wraps lines on characters only.
HardWrapOnly
)

// New returns a new model with the given width and height as well as default
Expand Down Expand Up @@ -52,6 +73,9 @@ type Model struct {
// which is usually via the alternate screen buffer.
HighPerformanceRendering bool

// Wrap sets the wrap mode for the viewport
Wrap WrapType

initialized bool
lines []string
}
Expand Down Expand Up @@ -101,6 +125,16 @@ func (m Model) ScrollPercent() float64 {
// Sync command should also be called.
func (m *Model) SetContent(s string) {
s = strings.ReplaceAll(s, "\r\n", "\n") // normalize line endings

switch m.Wrap {
case Wrap:
s = wrap.String(wordwrap.String(s, m.Width), m.Width)
case SoftWrapOnly:
s = wordwrap.String(s, m.Width)
case HardWrapOnly:
s = wrap.String(s, m.Width)
}

m.lines = strings.Split(s, "\n")

if m.YOffset > len(m.lines)-1 {
Expand Down

0 comments on commit 23842ea

Please sign in to comment.