Skip to content

Commit

Permalink
add a tree component (tree of models)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Nov 14, 2023
1 parent e57f711 commit 42e451a
Show file tree
Hide file tree
Showing 8 changed files with 683 additions and 6 deletions.
3 changes: 3 additions & 0 deletions bubbles/internal/testutil/run_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func RunModel(_ testing.TB, m tea.Model, iterations int, message tea.Msg) string
}

func flatten(p tea.Msg) (msgs []tea.Msg) {
if p == nil {
return nil
}
if reflect.TypeOf(p).Name() == "batchMsg" {
partials := extractBatchMessages(p)
for _, m := range partials {
Expand Down
24 changes: 18 additions & 6 deletions bubbles/taskprogress/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/wagoodman/go-progress"

"github.com/anchore/bubbly"
)

const (
checkMark = "✔"
xMark = "✘"
)

var _ bubbly.VisibleModel = (*Model)(nil)

type Model struct {
// ui components (view models)
Spinner spinner.Model
Expand Down Expand Up @@ -147,10 +151,6 @@ func (m Model) Init() tea.Cmd {
m.ProgressBar.Init(),
}

// if m.progressor != nil {
// cmds = append(cmds, m.ProgressBar.Init())
//}

return tea.Batch(
cmds...,
)
Expand All @@ -161,7 +161,7 @@ func (m Model) ID() int {
return m.id
}

// ID returns the spinner's unique ID.
// Sequence returns the spinner's current sequence number.
func (m Model) Sequence() int {
return m.sequence
}
Expand Down Expand Up @@ -234,9 +234,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

func (m Model) IsVisible() bool {
isDoneAndHidden := m.completed && m.HideOnSuccess
if isDoneAndHidden {
// it might be that the consumer will not invoke View() again based on
// this response, in which case we need to ensure that the done() function
// in invoked to release resources
m.done()
}

return !(isDoneAndHidden)
}

// View renders the model's view.
func (m Model) View() string {
if m.completed && m.HideOnSuccess {
if !m.IsVisible() {
m.done()
return ""
}
Expand Down
102 changes: 102 additions & 0 deletions bubbles/tree/__snapshots__/model_test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

[TestModel_View/gocase - 1]
└──a
└──a-a
---

[TestModel_View/sibling_branches_(one_extra_level) - 1]
├──a
│ ├──a-a
│ └──a-b
└──b
└──b-a
---

[TestModel_View/sibling_branches_(lots_of_extra_levels) - 1]
├──a
│ ├──a-a
│ ├──a-b
│ │ ├──a-b-a
│ │ ├──a-b-b
│ │ └──a-b-c
│ ├──a-c
│ │ └──a-c-a
│ └──a-d
└──b
├──b-a
│ ├──b-a-a
│ │ └──b-a-a-a
│ │ └──b-a-a-a-a
│ └──b-a-b
└──b-b
---

[TestModel_View/multiline_node - 1]
├──a
more a...
│ ├──a-a
│ │ a-a continued...
│ │ more a-a!
│ └──a-b
└──b
more b...
└──b-a
---

[TestModel_View/padded_multiline_node - 1]
├──a
more a...
│ │
│ ├──a-a
│ │ a-a continued...
│ │ more a-a!
│ │
│ ├──a-b
│ ├──a-c
│ └──a-d
└──b
more b...
└──b-a
---

[TestModel_View/hidden_nodes - 1]
└──a
└──a-a
---


[TestModel_View/margin - 1]
├──a
│ ├──a-a
│ └──a-b
└──b
└──b-a
---

[TestModel_View/roots_without_prefix - 1]
a
├──a-a
└──a-b
b
└──b-a
---

[TestModel_View/horizontal_padding - 1]
a
├── ✔ a-a
├── ✔ a-b
│ ├── ✔ a-b-a
│ ├── ✔ a-b-b
│ └── ✔ a-b-c
├── ⠼ a-c
│ └── ⠼ a-c-a
└── ⠼ a-d
b
├── ⠼ b-a
│ ├── ⠼ b-a-a
│ │ └── ⠼ b-a-a-a
│ │ └── ⠼ b-a-a-a-a
│ └── ⠼ b-a-b
└── ⠼ b-b
---
Loading

0 comments on commit 42e451a

Please sign in to comment.