Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v2-exp' into v2-area
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Nov 1, 2024
2 parents 6dfd0e0 + cc9a727 commit 714dc7a
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 165 deletions.
2 changes: 1 addition & 1 deletion cursor/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
)

const defaultBlinkSpeed = time.Millisecond * 530
Expand Down
50 changes: 30 additions & 20 deletions filepicker/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/charmbracelet/bubbles/v2/key"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
"github.com/dustin/go-humanize"
)

Expand All @@ -35,7 +35,7 @@ func New() Model {
DirAllowed: false,
FileAllowed: true,
AutoHeight: true,
Height: 0,
height: 0,
max: 0,
min: 0,
selectedStack: newStack(),
Expand Down Expand Up @@ -152,7 +152,7 @@ type Model struct {
maxStack stack
minStack stack

Height int
height int
AutoHeight bool

Cursor string
Expand Down Expand Up @@ -222,6 +222,16 @@ func (m Model) readDir(path string, showHidden bool) tea.Cmd {
}
}

// SetHeight sets the height of the file picker.
func (m *Model) SetHeight(h int) {
m.height = h
}

// Height returns the height of the file picker.
func (m Model) Height() int {
return m.height
}

// Init initializes the file picker model.
func (m Model) Init() (Model, tea.Cmd) {
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
Expand All @@ -235,21 +245,21 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
break
}
m.files = msg.entries
m.max = max(m.max, m.Height-1)
m.max = max(m.max, m.Height()-1)
case tea.WindowSizeMsg:
if m.AutoHeight {
m.Height = msg.Height - marginBottom
m.SetHeight(msg.Height - marginBottom)
}
m.max = m.Height - 1
m.max = m.Height() - 1
case tea.KeyPressMsg:
switch {
case key.Matches(msg, m.KeyMap.GoToTop):
m.selected = 0
m.min = 0
m.max = m.Height - 1
m.max = m.Height() - 1
case key.Matches(msg, m.KeyMap.GoToLast):
m.selected = len(m.files) - 1
m.min = len(m.files) - m.Height
m.min = len(m.files) - m.Height()
m.max = len(m.files) - 1
case key.Matches(msg, m.KeyMap.Down):
m.selected++
Expand All @@ -270,28 +280,28 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.max--
}
case key.Matches(msg, m.KeyMap.PageDown):
m.selected += m.Height
m.selected += m.Height()
if m.selected >= len(m.files) {
m.selected = len(m.files) - 1
}
m.min += m.Height
m.max += m.Height
m.min += m.Height()
m.max += m.Height()

if m.max >= len(m.files) {
m.max = len(m.files) - 1
m.min = m.max - m.Height
m.min = m.max - m.Height()
}
case key.Matches(msg, m.KeyMap.PageUp):
m.selected -= m.Height
m.selected -= m.Height()
if m.selected < 0 {
m.selected = 0
}
m.min -= m.Height
m.max -= m.Height
m.min -= m.Height()
m.max -= m.Height()

if m.min < 0 {
m.min = 0
m.max = m.min + m.Height
m.max = m.min + m.Height()
}
case key.Matches(msg, m.KeyMap.Back):
m.CurrentDirectory = filepath.Dir(m.CurrentDirectory)
Expand All @@ -300,7 +310,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
} else {
m.selected = 0
m.min = 0
m.max = m.Height - 1
m.max = m.Height() - 1
}
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
case key.Matches(msg, m.KeyMap.Open):
Expand Down Expand Up @@ -342,7 +352,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.pushView(m.selected, m.min, m.max)
m.selected = 0
m.min = 0
m.max = m.Height - 1
m.max = m.Height() - 1
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
}
}
Expand All @@ -352,7 +362,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
// View returns the view of the file picker.
func (m Model) View() string {
if len(m.files) == 0 {
return m.Styles.EmptyDirectory.Height(m.Height).MaxHeight(m.Height).String()
return m.Styles.EmptyDirectory.Height(m.Height()).MaxHeight(m.Height()).String()
}
var s strings.Builder

Expand Down Expand Up @@ -418,7 +428,7 @@ func (m Model) View() string {
s.WriteRune('\n')
}

for i := lipgloss.Height(s.String()); i <= m.Height; i++ {
for i := lipgloss.Height(s.String()); i <= m.Height(); i++ {
s.WriteRune('\n')
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1.0.20241024164833-a31857d07198
github.com/charmbracelet/harmonica v0.2.0
github.com/charmbracelet/lipgloss v0.13.2-0.20241023173701-23b08d1d3588
github.com/charmbracelet/lipgloss/v2 v2.0.0-20241029194924-049a2d260c67
github.com/charmbracelet/x/ansi v0.4.0
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91
github.com/dustin/go-humanize v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/charmbracelet/colorprofile v0.1.2 h1:nuB1bd/yAExT4fkcZvpqtQ2N5/8cJHSR
github.com/charmbracelet/colorprofile v0.1.2/go.mod h1:1htIKZYeI4TQs+OykPvpuBTUbUJxBYeSYBDIZuejMj0=
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.13.2-0.20241023173701-23b08d1d3588 h1:P0eMGSpqhHhVEN9HRD9Dl0ZaXIJWHINkW4DjPC/0EIQ=
github.com/charmbracelet/lipgloss v0.13.2-0.20241023173701-23b08d1d3588/go.mod h1:S+zi6HCChYq08TKQZpf3KEi7D/RO62JjxwNXbv6KVxA=
github.com/charmbracelet/lipgloss/v2 v2.0.0-20241029194924-049a2d260c67 h1:t/4KovO/31pdh9e74EVGVSbIlcY3bG4lmtrL3pD4fgU=
github.com/charmbracelet/lipgloss/v2 v2.0.0-20241029194924-049a2d260c67/go.mod h1:EcAf9+4/UeilG4rNQmiRzrou258LAVjVK4YUh+BvfqQ=
github.com/charmbracelet/x/ansi v0.4.0 h1:NqwHA4B23VwsDn4H3VcNX1W1tOmgnvY1NDx5tOXdnOU=
github.com/charmbracelet/x/ansi v0.4.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
Expand Down
2 changes: 1 addition & 1 deletion help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/charmbracelet/bubbles/v2/key"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
)

// KeyMap is a map of keybindings used to generate help. Since it's an
Expand Down
2 changes: 1 addition & 1 deletion list/defaultitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/charmbracelet/bubbles/v2/key"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
)

Expand Down
4 changes: 2 additions & 2 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
"github.com/sahilm/fuzzy"

Expand Down Expand Up @@ -691,7 +691,7 @@ func (m *Model) setSize(width, height int) {
m.width = width
m.height = height
m.Help.Width = width
m.FilterInput.Width = width - promptWidth - lipgloss.Width(m.spinnerView())
m.FilterInput.SetWidth(width - promptWidth - lipgloss.Width(m.spinnerView()))
m.updatePagination()
}

Expand Down
2 changes: 1 addition & 1 deletion list/style.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package list

import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
)

const (
Expand Down
10 changes: 6 additions & 4 deletions paginator/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ type KeyMap struct {

// DefaultKeyMap is the default set of key bindings for navigating and acting
// upon the paginator.
var DefaultKeyMap = KeyMap{
PrevPage: key.NewBinding(key.WithKeys("pgup", "left", "h")),
NextPage: key.NewBinding(key.WithKeys("pgdown", "right", "l")),
func DefaultKeyMap() KeyMap {
return KeyMap{
PrevPage: key.NewBinding(key.WithKeys("pgup", "left", "h")),
NextPage: key.NewBinding(key.WithKeys("pgdown", "right", "l")),
}
}

// Model is the Bubble Tea model for this user interface.
Expand Down Expand Up @@ -129,7 +131,7 @@ func New(opts ...Option) Model {
Page: 0,
PerPage: 1,
TotalPages: 1,
KeyMap: DefaultKeyMap,
KeyMap: DefaultKeyMap(),
ActiveDot: "•",
InactiveDot: "○",
ArabicFormat: "%d/%d",
Expand Down
20 changes: 15 additions & 5 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/harmonica"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
"github.com/lucasb-eyer/go-colorful"
"github.com/muesli/termenv"
Expand Down Expand Up @@ -92,7 +92,7 @@ func WithoutPercentage() Option {
// waiting for a tea.WindowSizeMsg.
func WithWidth(w int) Option {
return func(m *Model) {
m.Width = w
m.width = w
}
}

Expand Down Expand Up @@ -131,7 +131,7 @@ type Model struct {
tag int

// Total width of the progress bar, including percentage, if set.
Width int
width int

// "Filled" sections of the progress bar.
Full rune
Expand Down Expand Up @@ -171,7 +171,7 @@ type Model struct {
func New(opts ...Option) Model {
m := Model{
id: nextID(),
Width: defaultWidth,
width: defaultWidth,
Full: '█',
FullColor: "#7571F9",
Empty: '░',
Expand Down Expand Up @@ -278,6 +278,16 @@ func (m Model) ViewAs(percent float64) string {
return b.String()
}

// SetWidth sets the width of the progress bar.
func (m *Model) SetWidth(w int) {
m.width = w
}

// Width returns the width of the progress bar.
func (m Model) Width() int {
return m.width
}

func (m *Model) nextFrame() tea.Cmd {
return tea.Tick(time.Second/time.Duration(fps), func(time.Time) tea.Msg {
return FrameMsg{id: m.id, tag: m.tag}
Expand All @@ -286,7 +296,7 @@ func (m *Model) nextFrame() tea.Cmd {

func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {
var (
tw = max(0, m.Width-textWidth) // total width
tw = max(0, m.width-textWidth) // total width
fw = int(math.Round((float64(tw) * percent))) // filled width
p float64
)
Expand Down
2 changes: 1 addition & 1 deletion progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestGradient(t *testing.T) {
expLast := strings.Split(sb.String(), AnsiReset)[0]

for _, width := range []int{3, 5, 50} {
p.Width = width
p.SetWidth(width)
res := p.ViewAs(1.0)

// extract colors from the progrss bar by splitting at p.Full+AnsiReset, leaving us with just the color sequences
Expand Down
6 changes: 3 additions & 3 deletions spinner/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/v2"
)

// Internal ID management. Used during animating to ensure that frame messages
Expand Down Expand Up @@ -195,14 +195,14 @@ func (m Model) tick(id, tag int) tea.Cmd {
// spinner := New(WithSpinner(Dot))
type Option func(*Model)

// WithSpinner is an option to set the spinner.
// WithSpinner is an option to set the spinner. Pass this to [Spinner.New].
func WithSpinner(spinner Spinner) Option {
return func(m *Model) {
m.Spinner = spinner
}
}

// WithStyle is an option to set the spinner style.
// WithStyle is an option to set the spinner style. Pass this to [Spinner.New].
func WithStyle(style lipgloss.Style) Option {
return func(m *Model) {
m.Style = style
Expand Down
31 changes: 21 additions & 10 deletions stopwatch/stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ func nextID() int {
return int(atomic.AddInt64(&lastID, 1))
}

// Option is a configuration option in [New]. For example:
//
// timer := New(time.Second*10, WithInterval(5*time.Second))
type Option func(*Model)

// WithInterval is an option for setting the interval between ticks. Pass as
// an argument to [New].
func WithInterval(interval time.Duration) Option {
return func(m *Model) {
m.Interval = interval
}
}

// TickMsg is a message that is sent on every timer tick.
type TickMsg struct {
// ID is the identifier of the stopwatch that sends the message. This makes
Expand Down Expand Up @@ -47,18 +60,16 @@ type Model struct {
Interval time.Duration
}

// NewWithInterval creates a new stopwatch with the given timeout and tick
// interval.
func NewWithInterval(interval time.Duration) Model {
return Model{
Interval: interval,
id: nextID(),
// New creates a new stopwatch with 1s interval.
func New(opts ...Option) Model {
m := Model{
id: nextID(),
}
}

// New creates a new stopwatch with 1s interval.
func New() Model {
return NewWithInterval(time.Second)
for _, opt := range opts {
opt(&m)
}
return m
}

// ID returns the unique ID of the model.
Expand Down
Loading

0 comments on commit 714dc7a

Please sign in to comment.