Skip to content
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

Navstack: option to not quit on empty stack #2

Open
wants to merge 2 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
12 changes: 11 additions & 1 deletion .github/workflows/vhs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
go-version: 1.22.0

- name: Build Examples
run: go build -o ./bin/deeper ./examples/deeper/*.go && go build -o ./bin/simple ./examples/simple/*.go
run: go build -o ./bin/deeper ./examples/deeper/*.go && go build -o ./bin/simple ./examples/simple/*.go && go build -o ./bin/hosted ./examples/hosted/*.go

- name: Create deeper demo GIF
uses: charmbracelet/vhs-action@v2
Expand All @@ -42,6 +42,16 @@ jobs:
- name: Move simple demo GIF to examples/simple
run: mv demo.gif examples/simple/demo.gif

- name: Create hosted demo GIF
uses: charmbracelet/vhs-action@v2
env:
TERM: xterm-256color
with:
path: ./examples/hosted/demo.tape

- name: Move hosted demo GIF to examples/hosted
run: mv demo.gif examples/hosted/demo.gif

- name: Commit and push demo.gif
uses: stefanzweifel/git-auto-commit-action@v4
env:
Expand Down
13 changes: 12 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
"port": 2345,
"host": "127.0.0.1",
"preLaunchTask": "Run deeper headless dlv" // Here !
}
},
{
"name": "Attach to Hosted Example",
"type": "go",
"debugAdapter": "dlv-dap",
"request": "attach",
"mode": "remote",
"remotePath": "${workspaceFolder}/examples/hosted/",
"port": 2345,
"host": "127.0.0.1",
"preLaunchTask": "Run deeper headless dlv" // Here !
}
]
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"--headless",
"--listen=:2345",
"--api-version=2",
"${workspaceFolder}/examples/deeper/main.go"
"${workspaceFolder}/examples/hosted/main.go"
],
"isBackground": true,
"problemMatcher": {
Expand Down
20 changes: 10 additions & 10 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module github.com/kevm/bubbleo/examples
go 1.22.0

require (
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/kevm/bubbleo v0.0.0-20240222171125-3f793cc03950
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.2
github.com/charmbracelet/lipgloss v0.10.0
github.com/kevm/bubbleo v0.1.5
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/bubbles v0.18.0 // indirect
github.com/containerd/console v1.0.4 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -22,9 +22,9 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
)
38 changes: 19 additions & 19 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/kevm/bubbleo v0.0.0-20240222171125-3f793cc03950 h1:Yk7UJ5rVZiRECCR3gxql3HZLSvOOmvvt7buJ7DgxSK0=
github.com/kevm/bubbleo v0.0.0-20240222171125-3f793cc03950/go.mod h1:5O+ivBYuSP5/EzlyRVtrNSxHTG9PCwGFzNIz3OTIUzM=
github.com/charmbracelet/bubbletea v0.26.2 h1:Eeb+n75Om9gQ+I6YpbCXQRKHt5Pn4vMwusQpwLiEgJQ=
github.com/charmbracelet/bubbletea v0.26.2/go.mod h1:6I0nZ3YHUrQj7YHIHlM8RySX4ZIthTliMY+W8X8b+Gs=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/kevm/bubbleo v0.1.5 h1:lxeBK6jbFKId+ukm90N7N6MT0kzxYj9CDPTcrqY/kNs=
github.com/kevm/bubbleo v0.1.5/go.mod h1:5O+ivBYuSP5/EzlyRVtrNSxHTG9PCwGFzNIz3OTIUzM=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand All @@ -35,15 +35,15 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
8 changes: 8 additions & 0 deletions examples/hosted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Hosted

This example has a view which uses navstack to spawn child views such as a settings view.

<img src="demo.gif" alt="gif"/>



3 changes: 3 additions & 0 deletions examples/hosted/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions examples/hosted/demo.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Output demo.gif

Require echo

Set FontSize 36
Set Width 1800
Set Height 900
Set PlaybackSpeed 0.5

Set Shell bash
Sleep 1s
Type "./bin/hosted"
Enter
Sleep 500ms
Down
Sleep .5s
Down
Sleep .5s
Up
Sleep 1.0s
Enter
Sleep 3s
Type "s"
Sleep 1s
Down
Sleep 0.5s
Enter
Sleep 5s
Escape
Binary file added examples/hosted/hosted
Binary file not shown.
75 changes: 75 additions & 0 deletions examples/hosted/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"
"github.com/charmbracelet/lipgloss"
settings "github.com/kevm/bubbleo/examples/hosted/settings"
"github.com/kevm/bubbleo/navstack"
"github.com/kevm/bubbleo/utils"
"github.com/kevm/bubbleo/window"
)

type ShowSettings struct{}

type model struct {
SelectedColor string
navstack navstack.Model
}

func (m model) Init() tea.Cmd {
if m.SelectedColor == "" {
return utils.Cmdize(ShowSettings{})
}
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.navstack.Top() != nil {
return m, m.navstack.Update(msg)
}

switch msg := msg.(type) {
case ShowSettings:
item := navstack.NavigationItem{Model: settings.New(), Title: "Settings"}
cmd := m.navstack.Push(item)
return m, cmd
case settings.SettingsUpdated:
m.SelectedColor = msg.Color
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
return m, tea.Quit
case "s":
return m, utils.Cmdize(ShowSettings{})
}
}

return m, nil
}

func (m model) View() string {
if m.navstack.Top() != nil {
return m.navstack.View()
}

return "Color selected: " + lipgloss.NewStyle().Foreground(lipgloss.Color(m.SelectedColor)).Render(m.SelectedColor)
}

func main() {
window := window.New(100, 50, 0, 0)
ns := navstack.New(&window).QuitOnEmptyStack(false) // hosted users likely want to avoid quitting when the stack is empty
m := model{
navstack: ns,
}
p := tea.NewProgram(m, tea.WithAltScreen())

_, err := p.Run()
if err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
5 changes: 5 additions & 0 deletions examples/hosted/settings/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package color

type ColorSelected struct {
RGB string
}
81 changes: 81 additions & 0 deletions examples/hosted/settings/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package color

import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/kevm/bubbleo/navstack"
"github.com/kevm/bubbleo/utils"
)

type SettingsUpdated struct {
Color string
}

type Model struct {
list list.Model
}

type ColorItem struct {
title, desc string
}

func (i ColorItem) Title() string {
return lipgloss.NewStyle().Foreground(lipgloss.Color(i.desc)).Render(i.title)
}
func (i ColorItem) Description() string { return i.desc }
func (i ColorItem) FilterValue() string { return i.title + i.desc }

func New() Model {
items := []list.Item{
ColorItem{title: "Red", desc: "#ff0000"},
ColorItem{title: "Green", desc: "#00ff00"},
ColorItem{title: "Blue", desc: "#0000ff"},
ColorItem{title: "Yellow", desc: "#ffff00"},
ColorItem{title: "Cyan", desc: "#00ffff"},
ColorItem{title: "Magenta", desc: "#ff00ff"},
ColorItem{title: "White", desc: "#ffffff"},
ColorItem{title: "Black", desc: "#000000"},
}

return Model{
list: list.New(items, list.NewDefaultDelegate(), 0, 0),
}
}

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.WindowSizeMsg:
m.list.SetWidth(msg.Width)
m.list.SetHeight(msg.Height)
case tea.KeyMsg:
switch msg.String() {
case "q":
return m, tea.Quit
case "esc":
pop := utils.Cmdize(navstack.PopNavigation{})
return m, pop
case "enter":
selected := m.list.SelectedItem()
item, ok := selected.(ColorItem)
if !ok {
return m, nil
}
pop := utils.Cmdize(navstack.PopNavigation{})
cmd := utils.Cmdize(SettingsUpdated{Color: item.desc})
return m, tea.Sequence(pop, cmd)
}
}

var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

func (m Model) View() string {
return m.list.View()
}
Loading
Loading