Skip to content

Commit

Permalink
feat!(viewport): width and height are now optional args in New()
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Oct 31, 2024
1 parent 71b2b3e commit eda22f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type Option func(*Model)
func New(opts ...Option) Model {
m := Model{
cursor: 0,
viewport: viewport.New(0, 20), //nolint:mnd
viewport: viewport.New(viewport.WithHeight(20)), //nolint:mnd

KeyMap: DefaultKeyMap(),
Help: help.New(),
Expand Down
2 changes: 1 addition & 1 deletion textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ type Model struct {

// New creates a new model with default settings.
func New() Model {
vp := viewport.New(0, 0)
vp := viewport.New()
vp.KeyMap = viewport.KeyMap{}
cur := cursor.New()

Expand Down
29 changes: 26 additions & 3 deletions viewport/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,34 @@ import (
"github.com/charmbracelet/lipgloss/v2"
)

// Option is a configuration option that works in conjunction with [New]. For
// example:
//
// timer := New(WithWidth(10, WithHeight(5)))
type Option func(*Model)

// WithWidth is an initialization option that sets the width of the
// viewport. Pass as an argument to [New].
func WithWidth(w int) Option {
return func(m *Model) {
m.Width = w
}
}

// WithHeight is an initialization option that sets the height of the
// viewport. Pass as an argument to [New].
func WithHeight(h int) Option {
return func(m *Model) {
m.Height = h
}
}

// New returns a new model with the given width and height as well as default
// key mappings.
func New(width, height int) (m Model) {
m.Width = width
m.Height = height
func New(opts ...Option) (m Model) {
for _, opt := range opts {
opt(&m)
}
m.setInitialValues()
return m
}
Expand Down

0 comments on commit eda22f0

Please sign in to comment.