diff --git a/breadcrumb/model.go b/breadcrumb/model.go index 5f18e39..495c0e6 100644 --- a/breadcrumb/model.go +++ b/breadcrumb/model.go @@ -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()) +} diff --git a/examples/deeper/main.go b/examples/deeper/main.go index c2a1168..746fce6 100644 --- a/examples/deeper/main.go +++ b/examples/deeper/main.go @@ -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" @@ -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 { @@ -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 { @@ -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"}) diff --git a/navstack/model.go b/navstack/model.go index bc43ee9..fa89ed1 100644 --- a/navstack/model.go +++ b/navstack/model.go @@ -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) @@ -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 @@ -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 { diff --git a/styles/styles.go b/styles/styles.go index 69ee82c..9808609 100644 --- a/styles/styles.go +++ b/styles/styles.go @@ -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) )