-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(examples): tree #1190
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
Closed
Closed
feat(examples): tree #1190
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/charmbracelet/bubbles/v2/tree" | ||
| tea "github.com/charmbracelet/bubbletea/v2" | ||
| ) | ||
|
|
||
| type model struct { | ||
| tree tree.Model | ||
| } | ||
|
|
||
| func (m model) Init() tea.Cmd { | ||
| return nil | ||
| } | ||
|
|
||
| func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| var cmd tea.Cmd | ||
| switch msg := msg.(type) { | ||
| case tea.KeyPressMsg: | ||
| switch msg.String() { | ||
| case "q", "ctrl+c": | ||
| return m, tea.Quit | ||
| } | ||
| } | ||
| m.tree, cmd = m.tree.Update(msg) | ||
| return m, cmd | ||
| } | ||
|
|
||
| func (m model) View() string { | ||
| return m.tree.View() | ||
| } | ||
|
|
||
| func main() { | ||
| t := tree.New(tree.Root("~/charm"). | ||
| Child( | ||
| "ayman", | ||
| tree.Root("bash"). | ||
| Child( | ||
| tree.Root("tools"). | ||
| Child("zsh", | ||
| "doom-emacs", | ||
| ), | ||
| ), | ||
| tree.Root("carlos"). | ||
| Child( | ||
| tree.Root("emotes"). | ||
| Child( | ||
| "chefkiss.png", | ||
| "kekw.png", | ||
| ), | ||
| ), | ||
| "maas", | ||
| ), 70, 13) | ||
|
|
||
| if _, err := tea.NewProgram(model{tree: t}).Run(); err != nil { | ||
| fmt.Println("Oh no:", err) | ||
| os.Exit(1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/charmbracelet/bubbles/v2/key" | ||
| "github.com/charmbracelet/bubbles/v2/tree" | ||
| tea "github.com/charmbracelet/bubbletea/v2" | ||
| "github.com/charmbracelet/lipgloss/v2" | ||
| ltree "github.com/charmbracelet/lipgloss/v2/tree" | ||
| "github.com/charmbracelet/x/ansi" | ||
| ) | ||
|
|
||
| type model struct { | ||
| tree tree.Model | ||
| choice *tree.Node | ||
| } | ||
|
|
||
| func (m model) Init() tea.Cmd { | ||
| return nil | ||
| } | ||
|
|
||
| func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| var cmd tea.Cmd | ||
| switch msg := msg.(type) { | ||
| case tea.KeyPressMsg: | ||
| switch msg.String() { | ||
| case "e": | ||
| m.choice = m.tree.NodeAtCurrentOffset() | ||
| return m, tea.Quit | ||
| case "q", "ctrl+c": | ||
| return m, tea.Quit | ||
| } | ||
| } | ||
| m.tree, cmd = m.tree.Update(msg) | ||
| m.updateStyles() | ||
|
|
||
| return m, cmd | ||
| } | ||
|
|
||
| func (m *model) updateStyles() { | ||
| dimmed := lipgloss.Color("239") | ||
| base := lipgloss.NewStyle() | ||
| m.tree.SetStyles(tree.Styles{ | ||
| TreeStyle: base. | ||
| Padding(1). | ||
| Border(lipgloss.RoundedBorder()). | ||
| BorderForeground(lipgloss.Color("236")). | ||
| BorderBackground(base.GetBackground()), | ||
| RootNodeStyle: base, | ||
| NodeStyle: base, | ||
| ParentNodeStyle: base, | ||
| OpenIndicatorStyle: base, | ||
| SelectedNodeStyle: base.Bold(true).Background(lipgloss.Color("8")), | ||
| HelpStyle: base.MarginTop(1), | ||
| EnumeratorStyle: base.Foreground(dimmed), | ||
| IndenterStyle: base.Foreground(dimmed), | ||
| }) | ||
| } | ||
|
|
||
| func (m model) View() string { | ||
| return m.tree.View() | ||
| } | ||
|
|
||
| type file struct { | ||
| name string | ||
| color string | ||
| } | ||
|
|
||
| func (f file) String() string { | ||
| return "⌯ " + lipgloss.NewStyle().Foreground(lipgloss.Color(f.color)).Render(f.name) | ||
| } | ||
|
|
||
| type dir struct { | ||
| name string | ||
| } | ||
|
|
||
| func (d dir) String() string { | ||
| return lipgloss.NewStyle().Foreground(lipgloss.Color("4")).Render(d.name) | ||
| } | ||
|
|
||
| const ( | ||
| width = 50 | ||
| height = 21 | ||
| enumeratorWidth = 3 | ||
| ) | ||
|
|
||
| func main() { | ||
| t := tree.New( | ||
| tree.Root(dir{"charmbracelet/lipgloss"}). | ||
| Indenter(func(_ ltree.Children, _ int) string { | ||
| return "│ " | ||
| }). | ||
| Enumerator(func(_ ltree.Children, _ int) string { | ||
| return "│ " | ||
| }). | ||
| Child( | ||
| tree.Root(dir{"tree"}). | ||
| Child(file{"tree.go", "6"}). | ||
| Child(file{"renderer.go", "6"}), | ||
| ). | ||
| Child( | ||
| tree.Root(dir{"table"}). | ||
| Child( | ||
| tree.Root(dir{"utils"}). | ||
| Child(file{"utils.go", "6"}), | ||
| ), | ||
| ). | ||
| Child(tree.Root(dir{"list"}).Child(lipgloss.NewStyle().Faint(true).Render("(empty)"))). | ||
| Child(file{"README.md", "3"}). | ||
| Child(file{"go.mod", "255"}). | ||
| Child(file{"go.sum", "255"}). | ||
| Child(file{".gitignore", "255"}), | ||
| width, | ||
| height, | ||
| ) | ||
| t.SetCursorCharacter("") | ||
| t.SetOpenCharacter("📂") | ||
| t.SetClosedCharacter("📁") | ||
| kb := []key.Binding{ | ||
| key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "select")), | ||
| } | ||
| t.AdditionalShortHelpKeys = func() []key.Binding { | ||
| return kb | ||
| } | ||
| t.AdditionalFullHelpKeys = func() []key.Binding { | ||
| return kb | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to update these two because of charmbracelet/bubbles#639 (comment) |
||
|
|
||
| p := tea.NewProgram(model{tree: t}) | ||
| m, err := p.Run() | ||
| if err != nil { | ||
| fmt.Println("Oh no:", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Assert the final tea.Model to our local model and print the choice. | ||
| if m, ok := m.(model); ok && m.choice != nil { | ||
| fmt.Printf("---\nYou chose %s!\n", ansi.Strip(m.choice.Value())) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/bubbles/v2/tree" | ||
| tea "github.com/charmbracelet/bubbletea/v2" | ||
| "github.com/charmbracelet/lipgloss/v2" | ||
| ) | ||
|
|
||
| type model struct { | ||
| tree tree.Model | ||
| } | ||
|
|
||
| func (m model) Init() tea.Cmd { | ||
| return nil | ||
| } | ||
|
|
||
| func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| var cmd tea.Cmd | ||
| switch msg := msg.(type) { | ||
| case tea.KeyPressMsg: | ||
| switch msg.String() { | ||
| case "q", "ctrl+c": | ||
| return m, tea.Quit | ||
| } | ||
| } | ||
| m.tree, cmd = m.tree.Update(msg) | ||
| return m, cmd | ||
| } | ||
|
|
||
| func (m model) View() string { | ||
| return m.tree.View() | ||
| } | ||
|
|
||
| func main() { | ||
| root := tree.Root("🛂 Passport expiration date") | ||
| thisYear := time.Now().Year() | ||
| for year := thisYear; year < thisYear+10; year++ { | ||
| yRoot := tree.Root(fmt.Sprintf("%d", year)).Close() | ||
| for month := 1; month <= 12; month++ { | ||
| mRoot := tree.Root(time.Month(month).String()).Close().RootStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("1"))) | ||
| for day := 1; day < daysIn(time.Month(month), year); day++ { | ||
| mRoot.Child(fmt.Sprintf("%d", day)) | ||
| } | ||
| yRoot.Child(mRoot) | ||
| } | ||
| root.Child(yRoot) | ||
| } | ||
|
|
||
| t := tree.New(root, 80, 30) | ||
|
|
||
| if _, err := tea.NewProgram(model{tree: t}).Run(); err != nil { | ||
| fmt.Println("Oh no:", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func daysIn(m time.Month, year int) int { | ||
| return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as charmbracelet/bubbles#639 (comment)