Skip to content

Commit

Permalink
feat: radio buttons (#6)
Browse files Browse the repository at this point in the history
* feat: radio buttons

* feat: radio pillbox style

* feat: radio grouped style

* refactor: rearranging radio contents

* docs: add radio to README

* refactor: rename radio pillbox to pill
  • Loading branch information
shawalli authored Sep 10, 2024
1 parent 0c12f45 commit 8da8c6c
Show file tree
Hide file tree
Showing 18 changed files with 826 additions and 2 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@

bubbles is a collection of TUI elements for [Bubble Tea](https://github.com/charmbracelet/bubbletea) applications.

## Radio

![Simple radio button demo](assets/radio-simple.gif)

![Grouped radio button demo](assets/radio-grouped.gif)

`radio` simplifies the management of radio buttons, which may be presented vertically or horizontally.

* [Example code, basic radio buttons](examples/radio/simple/main.go)
* [Example code, pill-style buttons](examples/radio/pill/main.go)
* [Example code, grouped buttons](examples/radio/resizeable/main.go)

## Tabs

![Wraparound tab demo](assets/wraparound.gif)
![Wraparound tab demo](assets/tabs-wraparound.gif)

`tabs` is a remix on the [Bubble Tea tabs example](https://github.com/charmbracelet/bubbletea/tree/main/examples/tabs).
It abstracts away the tab logic into a separate model and provides management of tab-content.
Expand Down
Binary file added assets/radio-grouped.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions assets/radio-grouped.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Set Shell zsh
Set Height 650
Sleep 1s
Type "./radio-grouped"
Enter
Sleep 3s
Right 1
Sleep 800ms
Right 1
Sleep 900ms
Right 1
Sleep 1500ms
Down 1
Sleep 800ms
Down 1
Sleep 700ms
Down 1
Sleep 800ms
Left 1
Sleep 700ms
Up 1
Sleep 600ms
Left 1
Sleep 700ms
Up 1
Sleep 3s
Binary file added assets/radio-pill.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions assets/radio-pill.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Set Shell zsh
Set Height 750
Sleep 1s
Type "./radio-pill"
Enter
Sleep 3s
Right 1
Sleep 800ms
Right 1
Sleep 900ms
Right 1
Sleep 1500ms
Down 1
Sleep 800ms
Down 1
Sleep 700ms
Down 1
Sleep 800ms
Left 1
Sleep 700ms
Up 1
Sleep 600ms
Left 1
Sleep 700ms
Up 1
Sleep 3s
Binary file added assets/radio-simple.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions assets/radio-simple.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Set Shell zsh
Sleep 1s
Type "./radio-simple"
Enter
Sleep 3s
Right 1
Sleep 800ms
Right 1
Sleep 900ms
Right 1
Sleep 1500ms
Down 1
Sleep 800ms
Down 1
Sleep 700ms
Down 1
Sleep 800ms
Left 1
Sleep 700ms
Up 1
Sleep 600ms
Left 1
Sleep 700ms
Up 1
Sleep 3s
File renamed without changes
2 changes: 1 addition & 1 deletion assets/resizeable.tape → assets/tabs-resizeable.tape
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Set Shell zsh
Sleep 1s
Type "./demo"
Type "./tabs-wraparound"
Enter
Sleep 3s
Left 1
Expand Down
File renamed without changes
File renamed without changes.
75 changes: 75 additions & 0 deletions examples/radio/grouped/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss"
"github.com/shawalli/bubbles/radio"
)

type Model struct {
vRadio radio.Model
hRadio radio.Model
}

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

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
return m, tea.Quit
}
}

t, _ := m.hRadio.Update(msg)
m.hRadio = t.(radio.Model)

t, _ = m.vRadio.Update(msg)
m.vRadio = t.(radio.Model)

return m, nil
}

func (m Model) View() string {
return gloss.JoinVertical(
gloss.Top,
m.vRadio.View(),
m.hRadio.View(),
)
}

func main() {
vButtons := []tea.Model{
radio.NewButton(" 10% "),
radio.NewButton(" 15% "),
radio.NewButton(" 20% "),
radio.NewButton(" 22% "),
radio.NewButton(" 25% "),
radio.NewButton("None "),
}

hButtons := []tea.Model{
radio.NewButton(" Pay Cash "),
radio.NewButton("Pay Credit "),
radio.NewButton(" PayBuddy "),
radio.NewButton(" BNPL "),
}

m := Model{
vRadio: radio.New(true, vButtons...).
Styles(radio.DefaultGroupedStyles(true)).
Wraparound(true),
hRadio: radio.New(false, hButtons...).
Styles(radio.DefaultGroupedStyles(false)).
Wraparound(true),
}

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Printf("could not run program: %v", err)
os.Exit(1)
}
}
71 changes: 71 additions & 0 deletions examples/radio/pill/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss"
"github.com/shawalli/bubbles/radio"
)

type Model struct {
vRadio radio.Model
hRadio radio.Model
}

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

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
return m, tea.Quit
}
}

t, _ := m.hRadio.Update(msg)
m.hRadio = t.(radio.Model)

t, _ = m.vRadio.Update(msg)
m.vRadio = t.(radio.Model)

return m, nil
}

func (m Model) View() string {
return gloss.JoinVertical(
gloss.Top,
m.vRadio.View(),
m.hRadio.View(),
)
}

func main() {
vButtons := []tea.Model{
radio.NewButton(" 10% "),
radio.NewButton(" 15% "),
radio.NewButton(" 20% "),
radio.NewButton(" 22% "),
radio.NewButton(" 25% "),
radio.NewButton("None "),
}

hButtons := []tea.Model{
radio.NewButton(" Pay Cash "),
radio.NewButton("Pay Credit "),
radio.NewButton(" PayBuddy "),
radio.NewButton(" BNPL "),
}

m := Model{
vRadio: radio.New(true, vButtons...).Styles(radio.DefaultPillStyles(true)),
hRadio: radio.New(false, hButtons...).Styles(radio.DefaultPillStyles(false)),
}

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Printf("could not run program: %v", err)
os.Exit(1)
}
}
71 changes: 71 additions & 0 deletions examples/radio/simple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss"
"github.com/shawalli/bubbles/radio"
)

type Model struct {
vRadio radio.Model
hRadio radio.Model
}

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

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
return m, tea.Quit
}
}

t, _ := m.hRadio.Update(msg)
m.hRadio = t.(radio.Model)

t, _ = m.vRadio.Update(msg)
m.vRadio = t.(radio.Model)

return m, nil
}

func (m Model) View() string {
return gloss.JoinVertical(
gloss.Top,
m.vRadio.View(),
m.hRadio.View(),
)
}

func main() {
vButtons := []tea.Model{
radio.NewButton("10%"),
radio.NewButton("15%"),
radio.NewButton("20%"),
radio.NewButton("22%"),
radio.NewButton("25%"),
radio.NewButton("None"),
}

hButtons := []tea.Model{
radio.NewButton("Pay Cash"),
radio.NewButton("Pay Credit"),
radio.NewButton("PayBuddy"),
radio.NewButton("BNPL"),
}

m := Model{
vRadio: radio.New(true, vButtons...),
hRadio: radio.New(false, hButtons...),
}

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Printf("could not run program: %v", err)
os.Exit(1)
}
}
Loading

0 comments on commit 8da8c6c

Please sign in to comment.