Skip to content

feat!: v2 #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 0 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@ jobs:
- run: go mod download
- run: go build -v ./...
- run: go test -v -race ./...
spinner:
strategy:
matrix:
go-version: [oldstable, stable]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
GO111MODULE: "on"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true
- run: go mod download
working-directory: ./spinner
- run: go build -v ./...
working-directory: ./spinner
- run: go test -v -race ./...
working-directory: ./spinner
examples:
strategy:
matrix:
Expand Down
39 changes: 0 additions & 39 deletions accessibility/accessibility.go

This file was deleted.

2 changes: 1 addition & 1 deletion examples/accessibility-secure-input/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"log"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/v2"
)

func validate(s string) error {
Expand Down
2 changes: 1 addition & 1 deletion examples/accessibility/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"log"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/v2"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions examples/bubbletea-options/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"

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

func main() {
Expand Down
82 changes: 46 additions & 36 deletions examples/bubbletea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ package main

import (
"fmt"
"image/color"
"os"
"strings"

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

const maxWidth = 80

var (
red = lipgloss.AdaptiveColor{Light: "#FE5F86", Dark: "#FE5F86"}
indigo = lipgloss.AdaptiveColor{Light: "#5A56E0", Dark: "#7571F9"}
green = lipgloss.AdaptiveColor{Light: "#02BA84", Dark: "#02BF87"}
)

type Styles struct {
Base,
HeaderText,
Expand All @@ -26,29 +21,38 @@ type Styles struct {
Highlight,
ErrorHeaderText,
Help lipgloss.Style

Red, Indigo, Green color.Color
}

func NewStyles(lg *lipgloss.Renderer) *Styles {
s := Styles{}
s.Base = lg.NewStyle().
func NewStyles(hasDarkBg bool) *Styles {
var (
s = Styles{}
lightDark = lipgloss.LightDark(hasDarkBg)
)

s.Red = lightDark(lipgloss.Color("#FE5F86"), lipgloss.Color("#FE5F86"))
s.Indigo = lightDark(lipgloss.Color("#5A56E0"), lipgloss.Color("#7571F9"))
s.Green = lightDark(lipgloss.Color("#02BA84"), lipgloss.Color("#02BF87"))
s.Base = lipgloss.NewStyle().
Padding(1, 4, 0, 1)
s.HeaderText = lg.NewStyle().
Foreground(indigo).
s.HeaderText = lipgloss.NewStyle().
Foreground(s.Indigo).
Bold(true).
Padding(0, 1, 0, 2)
s.Status = lg.NewStyle().
s.Status = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(indigo).
BorderForeground(s.Indigo).
PaddingLeft(1).
MarginTop(1)
s.StatusHeader = lg.NewStyle().
Foreground(green).
s.StatusHeader = lipgloss.NewStyle().
Foreground(s.Green).
Bold(true)
s.Highlight = lg.NewStyle().
s.Highlight = lipgloss.NewStyle().
Foreground(lipgloss.Color("212"))
s.ErrorHeaderText = s.HeaderText.
Foreground(red)
s.Help = lg.NewStyle().
Foreground(s.Red)
s.Help = lipgloss.NewStyle().
Foreground(lipgloss.Color("240"))
return &s
}
Expand All @@ -61,17 +65,18 @@ const (
)

type Model struct {
state state
lg *lipgloss.Renderer
styles *Styles
form *huh.Form
width int
state state
styles func(bool) *Styles
form *huh.Form
hasDarkBg bool
width int
}

func NewModel() Model {
m := Model{width: maxWidth}
m.lg = lipgloss.DefaultRenderer()
m.styles = NewStyles(m.lg)
m := Model{
width: maxWidth,
styles: NewStyles,
}

m.form = huh.NewForm(
huh.NewGroup(
Expand Down Expand Up @@ -118,9 +123,12 @@ func min(x, y int) int {
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
styles := m.styles(m.hasDarkBg)
switch msg := msg.(type) {
case tea.BackgroundColorMsg:
m.hasDarkBg = msg.IsDark()
case tea.WindowSizeMsg:
m.width = min(msg.Width, maxWidth) - m.styles.Base.GetHorizontalFrameSize()
m.width = min(msg.Width, maxWidth) - styles.Base.GetHorizontalFrameSize()
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
Expand Down Expand Up @@ -148,7 +156,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m Model) View() string {
s := m.styles
s := m.styles(m.hasDarkBg)

switch m.form.State {
case huh.StateCompleted:
Expand All @@ -167,7 +175,7 @@ func (m Model) View() string {

// Form (left side)
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := m.lg.NewStyle().Margin(1, 0).Render(v)
form := lipgloss.NewStyle().Margin(1, 0).Render(v)

// Status (right side)
var status string
Expand Down Expand Up @@ -226,22 +234,24 @@ func (m Model) errorView() string {
}

func (m Model) appBoundaryView(text string) string {
s := m.styles(m.hasDarkBg)
return lipgloss.PlaceHorizontal(
m.width,
lipgloss.Left,
m.styles.HeaderText.Render(text),
s.HeaderText.Render(text),
lipgloss.WithWhitespaceChars("/"),
lipgloss.WithWhitespaceForeground(indigo),
lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(s.Indigo)),
)
}

func (m Model) appErrorBoundaryView(text string) string {
s := m.styles(m.hasDarkBg)
return lipgloss.PlaceHorizontal(
m.width,
lipgloss.Left,
m.styles.ErrorHeaderText.Render(text),
s.ErrorHeaderText.Render(text),
lipgloss.WithWhitespaceChars("/"),
lipgloss.WithWhitespaceForeground(red),
lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(s.Red)),
)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/burger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"
"time"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/huh/v2"
"github.com/charmbracelet/huh/v2/spinner"
"github.com/charmbracelet/lipgloss/v2"
xstrings "github.com/charmbracelet/x/exp/strings"
)

Expand Down
91 changes: 44 additions & 47 deletions examples/conditional/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/v2"
)

type consumable int
Expand All @@ -20,60 +20,57 @@ func (c consumable) String() string {
}

func main() {

var category consumable
type opts []huh.Option[string]

var choice string

// Then ask for a specific food item based on the previous answer.
err :=
huh.NewForm(
huh.NewGroup(
huh.NewSelect[consumable]().
Title("What are you in the mood for?").
Value(&category).
Options(
huh.NewOption("Some fruit", fruits),
huh.NewOption("A vegetable", vegetables),
huh.NewOption("A drink", drinks),
),
err := huh.NewForm(
huh.NewGroup(
huh.NewSelect[consumable]().
Title("What are you in the mood for?").
Value(&category).
Options(
huh.NewOption("Some fruit", fruits),
huh.NewOption("A vegetable", vegetables),
huh.NewOption("A drink", drinks),
),

huh.NewSelect[string]().
Value(&choice).
Height(7).
TitleFunc(func() string {
return fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)
}, &category).
OptionsFunc(func() []huh.Option[string] {
switch category {
case fruits:
return []huh.Option[string]{
huh.NewOption("Tangerine", "tangerine"),
huh.NewOption("Canteloupe", "canteloupe"),
huh.NewOption("Pomelo", "pomelo"),
huh.NewOption("Grapefruit", "grapefruit"),
}
case vegetables:
return []huh.Option[string]{
huh.NewOption("Carrot", "carrot"),
huh.NewOption("Jicama", "jicama"),
huh.NewOption("Kohlrabi", "kohlrabi"),
huh.NewOption("Fennel", "fennel"),
huh.NewOption("Ginger", "ginger"),
}
default:
return []huh.Option[string]{
huh.NewOption("Coffee", "coffee"),
huh.NewOption("Tea", "tea"),
huh.NewOption("Bubble Tea", "bubble tea"),
huh.NewOption("Agua Fresca", "agua-fresca"),
}
huh.NewSelect[string]().
Value(&choice).
Height(7).
TitleFunc(func() string {
return fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)
}, &category).
OptionsFunc(func() []huh.Option[string] {
switch category {
case fruits:
return []huh.Option[string]{
huh.NewOption("Tangerine", "tangerine"),
huh.NewOption("Canteloupe", "canteloupe"),
huh.NewOption("Pomelo", "pomelo"),
huh.NewOption("Grapefruit", "grapefruit"),
}
}, &category),
),
).Run()

case vegetables:
return []huh.Option[string]{
huh.NewOption("Carrot", "carrot"),
huh.NewOption("Jicama", "jicama"),
huh.NewOption("Kohlrabi", "kohlrabi"),
huh.NewOption("Fennel", "fennel"),
huh.NewOption("Ginger", "ginger"),
}
default:
return []huh.Option[string]{
huh.NewOption("Coffee", "coffee"),
huh.NewOption("Tea", "tea"),
huh.NewOption("Bubble Tea", "bubble tea"),
huh.NewOption("Agua Fresca", "agua-fresca"),
}
}
}, &category),
),
).Run()
if err != nil {
fmt.Println("Trouble in food paradise:", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/dynamic/dynamic-all/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"strconv"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/v2"
)

func main() {
Expand Down
Loading
Loading