feat!(viewport): width and height are now optional args in New()

This commit is contained in:
Christian Rocha
2024-10-31 15:02:05 -04:00
parent 241db06b4a
commit 3b879e7694
3 changed files with 28 additions and 5 deletions
+1 -1
View File
@@ -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(),
+1 -1
View File
@@ -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()
+26 -3
View File
@@ -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
}