From 3b879e7694fc93428917cb49054420c784ec2307 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 31 Oct 2024 14:41:28 -0400 Subject: [PATCH] feat!(viewport): width and height are now optional args in New() --- table/table.go | 2 +- textarea/textarea.go | 2 +- viewport/viewport.go | 29 ++++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/table/table.go b/table/table.go index c5482c3..2aa64e5 100644 --- a/table/table.go +++ b/table/table.go @@ -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(), diff --git a/textarea/textarea.go b/textarea/textarea.go index 52fd845..28bad34 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -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() diff --git a/viewport/viewport.go b/viewport/viewport.go index 5d602a5..c8ccedb 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -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 }