Skip to content

Commit

Permalink
Add breadcrumb to deeper example; Not rendering yet :/
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Miller committed Feb 21, 2024
1 parent 071576e commit 30e4991
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
44 changes: 44 additions & 0 deletions breadcrumb/model.go
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
package breadcrumb

import (
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/kevm/bubbleo/navstack"
"github.com/kevm/bubbleo/styles"
)

type Model struct {
Navstack *navstack.Model
FrameStyle lipgloss.Style
}

func New(n *navstack.Model) Model {
return Model{
Navstack: n,
FrameStyle: styles.BreadCrumbFrameStyle.Copy(),
}
}

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

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

func (m Model) View() string {
b := strings.Builder{}

for i, c := range m.Navstack.StackSummary() {
if i != 0 {
b.WriteString(" > ")
}
b.WriteString(c)
}
b.WriteString("\n")

return "\n\n" + b.String()
//return m.FrameStyle.Render(b.String())
}
26 changes: 19 additions & 7 deletions examples/deeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/kevm/bubbleo/breadcrumb"
"github.com/kevm/bubbleo/examples/deeper/artistcolors"
"github.com/kevm/bubbleo/examples/deeper/data"
"github.com/kevm/bubbleo/menu"
Expand All @@ -19,7 +20,9 @@ var docStyle = lipgloss.NewStyle()
type model struct {
SelectedArtist string
SelectedColor string
menu menu.Model

menu menu.Model
breadcrumb breadcrumb.Model
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -47,14 +50,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
return docStyle.Render(m.menu.View())
bc := m.breadcrumb.View()
menu := m.menu.View()
return docStyle.Render(bc, menu)
}

func main() {

top, side := docStyle.GetFrameSize()
w := window.New(120, 25, top, side)
ns := navstack.New(&w)
artists := data.GetArtists()
choices := make([]menu.Choice, len(artists))
for i, a := range artists {
Expand All @@ -65,9 +66,20 @@ func main() {
}
}

maintop, mainside := docStyle.GetFrameSize()
w := window.New(120, 25, maintop, mainside)
ns := navstack.New(&w)

// Add the breadcrumb height to the top offset
bc := breadcrumb.New(&ns)
bctop, bcside := bc.FrameStyle.GetFrameSize()
w.TopOffset += bctop
w.SideOffset += bcside

title := "Choose an Artist:"
m := model{
menu: menu.New(title, choices, nil),
menu: menu.New(title, choices, nil),
breadcrumb: bc,
}

ns.Push(navstack.NavigationItem{Model: m, Title: "main menu"})
Expand Down
37 changes: 27 additions & 10 deletions navstack/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (m Model) Init() tea.Cmd {

func (m *Model) Push(item NavigationItem) tea.Cmd {

nim, cmd := item.Model.Update(m.window.GetWindowSizeMsg())
wmsg := m.window.GetWindowSizeMsg()
nim, cmd := item.Model.Update(wmsg)
item.Model = nim

m.stack = append(m.stack, item)
Expand Down Expand Up @@ -74,12 +75,29 @@ func (m Model) Top() *NavigationItem {
return &top
}

func (m Model) StackSummary() []string {
summary := []string{}
for _, item := range m.stack {
summary = append(summary, item.Title)
}

return summary
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
top := m.Top()
switch msg := msg.(type) {
case tea.WindowSizeMsg:
case tea.WindowSizeMsg: // update the window size based on offsets
if top == nil {
return m, nil
}
m.window.Height = msg.Height
m.window.Width = msg.Width
msg.Width = m.window.Width - m.window.SideOffset
msg.Height = m.window.Height - m.window.TopOffset
um, cmd := top.Update(msg)
m.stack[len(m.stack)-1] = um.(NavigationItem)
return m, cmd
case ReloadCurrent:
if top == nil {
return m, nil
Expand All @@ -91,15 +109,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case PushNavigation:
cmd := m.Push(msg.Item)
return m, cmd
default:
if top == nil {
return m, nil
}
um, cmd := top.Update(msg)
m.stack[len(m.stack)-1] = um.(NavigationItem)
return m, cmd
}

if top == nil {
return m, nil
}

um, cmd := top.Update(msg)
m.stack[len(m.stack)-1] = um.(NavigationItem)
return m, cmd
}

func (m Model) View() string {
Expand Down
2 changes: 2 additions & 0 deletions styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ var (
ListStyle = lipgloss.NewStyle().Margin(1, 2)
ListItemStyle = lipgloss.NewStyle().PaddingLeft(4)
ListTitleStyle = lipgloss.NewStyle().MarginLeft(2).Background(backgroundColor).Foreground(foregroundColor).Bold(true)

BreadCrumbFrameStyle = lipgloss.NewStyle().Background(backgroundColor).Foreground(foregroundColor).Padding(1)
)

0 comments on commit 30e4991

Please sign in to comment.