Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ up:
cd examples && go get -u ./... && go mod tidy

test:
go test -v ./...
cd examples && go test -v ./...
GORACE="exitcode=1 halt_on_error=1" go test -v -race -timeout 3m -count 3 -cpu 1,4 ./...
cd examples && GORACE="exitcode=1 halt_on_error=1" go test -v -race -timeout 3m -count 3 -cpu 1,4 ./...

fuzz:
go test -fuzz=FuzzScan -timeout=1m
Expand Down
37 changes: 18 additions & 19 deletions examples/full-lipgloss/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package main

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss/v2"
zone "github.com/lrstanley/bubblezone/v2"
)

var (
Expand All @@ -23,33 +23,32 @@ var (
MarginTop(1).
MarginRight(2)

activeButtonStyle = buttonStyle.Copy().
activeButtonStyle = buttonStyle.
Foreground(lipgloss.Color("#FFF7DB")).
Background(lipgloss.Color("#F25D94")).
MarginRight(2).
Underline(true)
)

type dialog struct {
id string
height int
width int

id string
active string
question string
}

func (m dialog) Init() tea.Cmd {
func (m *dialog) Init() tea.Cmd {
return nil
}

func (m dialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *dialog) GetHeight() int {
return lipgloss.Height(m.View())
}

func (m *dialog) Update(msg tea.Msg) tea.Cmd { //nolint:unparam
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
case tea.MouseMsg:
if msg.Action != tea.MouseActionRelease || msg.Button != tea.MouseButtonLeft {
return m, nil
case tea.MouseReleaseMsg:
if msg.Button != tea.MouseLeft {
return nil
}

if zone.Get(m.id + "confirm").InBounds(msg) {
Expand All @@ -58,12 +57,12 @@ func (m dialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.active = "cancel"
}

return m, nil
return nil
}
return m, nil
return nil
}

func (m dialog) View() string {
func (m *dialog) View() string {
var okButton, cancelButton string

if m.active == "confirm" {
Expand All @@ -74,7 +73,7 @@ func (m dialog) View() string {
cancelButton = activeButtonStyle.Render("Maybe")
}

question := lipgloss.NewStyle().Width(27).Align(lipgloss.Center).Render("Are you sure you want to eat marmalade?")
question := lipgloss.NewStyle().Width(27).Align(lipgloss.Center).Render(m.question)
buttons := lipgloss.JoinHorizontal(
lipgloss.Top,
zone.Mark(m.id+"confirm", okButton),
Expand Down
31 changes: 17 additions & 14 deletions examples/full-lipgloss/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,35 @@
package main

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss/v2"
zone "github.com/lrstanley/bubblezone/v2"
)

type history struct {
id string
height int
width int
dark bool

active string
items []string
}

func (m history) Init() tea.Cmd {
func (m *history) Init() tea.Cmd {
return nil
}

func (m history) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *history) Update(msg tea.Msg) tea.Cmd { //nolint:unparam
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = msg.Height
m.width = msg.Width
case tea.MouseMsg:
if msg.Action != tea.MouseActionRelease || msg.Button != tea.MouseButtonLeft {
return m, nil
case tea.BackgroundColorMsg:
m.dark = msg.IsDark()
case tea.MouseReleaseMsg:
if msg.Button != tea.MouseLeft {
return nil
}

for _, item := range m.items {
Expand All @@ -41,26 +44,26 @@ func (m history) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
}
return m, nil
return nil
}

func (m history) View() string {
func (m *history) View() string {
historyStyle := lipgloss.NewStyle().
Align(lipgloss.Left).
Foreground(lipgloss.Color("#FAFAFA")).
Background(subtle).
Margin(1).
Background(subtle.Adapt(m.dark)).
Margin(0, 1).
Padding(1, 2).
Width((m.width / len(m.items)) - 2).
Height(m.height - 2).
Height(m.height).
MaxHeight(m.height)

out := []string{}

for _, item := range m.items {
if item == m.active {
// Customize the active item.
out = append(out, zone.Mark(m.id+item, historyStyle.Background(highlight).Render(item)))
out = append(out, zone.Mark(m.id+item, historyStyle.Background(highlight.Adapt(m.dark)).Render(item)))
} else {
// Make sure to mark all zones.
out = append(out, zone.Mark(m.id+item, historyStyle.Render(item)))
Expand Down
77 changes: 38 additions & 39 deletions examples/full-lipgloss/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,30 @@
package main

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss/v2"
zone "github.com/lrstanley/bubblezone/v2"
)

var (
listStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, true, false, false).
BorderForeground(subtle).
MarginRight(2)

listHeader = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
BorderForeground(subtle).
MarginRight(2).
Render

listItemStyle = lipgloss.NewStyle().PaddingLeft(2).Render

checkMark = lipgloss.NewStyle().SetString("βœ“").
Foreground(special).
PaddingRight(1).
String()

listDoneStyle = func(s string) string {
return checkMark + lipgloss.NewStyle().
Strikethrough(true).
Foreground(lipgloss.AdaptiveColor{Light: "#969B86", Dark: "#696969"}).
Render(s)
}
MarginRight(2)

listItemStyle = lipgloss.NewStyle().
PaddingLeft(2)

checkMark = lipgloss.NewStyle().
SetString("βœ“").
PaddingRight(1)

listDoneStyle = lipgloss.NewStyle().
Strikethrough(true)
)

type listItem struct {
Expand All @@ -44,25 +37,27 @@ type listItem struct {
}

type list struct {
id string
height int
width int

id string
dark bool
title string
items []listItem
}

func (m list) Init() tea.Cmd {
func (m *list) Init() tea.Cmd {
return nil
}

func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *list) GetHeight() int {
return lipgloss.Height(m.View())
}

func (m *list) Update(msg tea.Msg) tea.Cmd { //nolint:unparam
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
case tea.MouseMsg:
if msg.Action != tea.MouseActionRelease || msg.Button != tea.MouseButtonLeft {
return m, nil
case tea.BackgroundColorMsg:
m.dark = msg.IsDark()
case tea.MouseReleaseMsg:
if msg.Button != tea.MouseLeft {
return nil
}

for i, item := range m.items {
Expand All @@ -73,24 +68,28 @@ func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

return m, nil
return nil
}
return m, nil
return nil
}

func (m list) View() string {
out := []string{listHeader(m.title)}
func (m *list) View() string {
out := []string{listHeader.BorderForeground(subtle.Adapt(m.dark)).Render(m.title)}

for _, item := range m.items {
if item.done {
out = append(out, zone.Mark(m.id+item.name, listDoneStyle(item.name)))
out = append(out, zone.Mark(
m.id+item.name,
checkMark.Foreground(special.Adapt(m.dark)).String()+
listDoneStyle.Foreground(completed.Adapt(m.dark)).Render(item.name),
))
continue
}

out = append(out, zone.Mark(m.id+item.name, listItemStyle(item.name)))
out = append(out, zone.Mark(m.id+item.name, listItemStyle.Render(item.name)))
}

return listStyle.Render(
return listStyle.BorderForeground(subtle.Adapt(m.dark)).Render(
lipgloss.JoinVertical(lipgloss.Left, out...),
)
}
Loading