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

Add tree-of-models bubble component #29

Merged
merged 3 commits into from
Nov 15, 2023
Merged
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
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