-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Selection box, list repos, tab activate main boxes
- Loading branch information
Toby Padilla
committed
Aug 4, 2021
1 parent
d39936b
commit 9817bab
Showing
6 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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,70 @@ | ||
package selection | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
type SelectedMsg struct { | ||
Name string | ||
Index int | ||
} | ||
|
||
type Bubble struct { | ||
NormalStyle lipgloss.Style | ||
SelectedStyle lipgloss.Style | ||
Items []string | ||
selectedItem int | ||
} | ||
|
||
func NewBubble(items []string) *Bubble { | ||
return &Bubble{ | ||
NormalStyle: normalStyle, | ||
SelectedStyle: selectedStyle, | ||
Items: items, | ||
selectedItem: -1, | ||
} | ||
} | ||
|
||
func (b *Bubble) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (b *Bubble) View() string { | ||
s := "" | ||
for i, item := range b.Items { | ||
if i == b.selectedItem { | ||
s += b.SelectedStyle.Render(item) + "\n" | ||
} else { | ||
s += b.NormalStyle.Render(item) + "\n" | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
cmds := make([]tea.Cmd, 0) | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "k", "up": | ||
if b.selectedItem > 0 { | ||
b.selectedItem-- | ||
} | ||
case "j", "down": | ||
if b.selectedItem < len(b.Items)-1 { | ||
b.selectedItem++ | ||
} | ||
case "enter": | ||
cmds = append(cmds, b.sendMessage) | ||
} | ||
} | ||
return b, tea.Batch(cmds...) | ||
} | ||
|
||
func (b *Bubble) sendMessage() tea.Msg { | ||
return SelectedMsg{ | ||
Name: b.Items[b.selectedItem], | ||
Index: b.selectedItem, | ||
} | ||
} |
This file contains 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,11 @@ | ||
package selection | ||
|
||
import ( | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
var normalStyle = lipgloss.NewStyle(). | ||
Foreground(lipgloss.Color("#FFFFFF")) | ||
|
||
var selectedStyle = lipgloss.NewStyle(). | ||
Foreground(lipgloss.Color("#714C7B")) |
This file contains 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 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