-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now you can change between users and groups views pressing the tab key. Using the sub-level methods (see charmbracelet/bubbletea#13) Still, the way the list and viewport size is set between changes seems a bit too hacky.
- Loading branch information
Showing
14 changed files
with
245 additions
and
47 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
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,7 @@ | ||
package group | ||
|
||
import tea "github.com/charmbracelet/bubbletea" | ||
|
||
func (bg BubbleGroup) Init() tea.Cmd { | ||
return nil | ||
} |
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,32 @@ | ||
package group | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/ariasmn/ugm/internal/tui/common" | ||
"github.com/charmbracelet/bubbles/list" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type itemDelegate struct{} | ||
|
||
func (d itemDelegate) Height() int { return 1 } | ||
func (d itemDelegate) Spacing() int { return 0 } | ||
func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil } | ||
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { | ||
group, ok := listItem.(item) | ||
if !ok { | ||
return | ||
} | ||
|
||
line := group.Details.Name | ||
|
||
if index == m.Index() { | ||
line = common.ListSelectedListItemStyle.Render("> " + line) | ||
} else { | ||
line = common.ListItemStyle.Render(line) | ||
} | ||
|
||
fmt.Fprint(w, line) | ||
} |
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,35 @@ | ||
package group | ||
|
||
import ( | ||
"github.com/ariasmn/ugm/groupparser" | ||
"github.com/charmbracelet/bubbles/list" | ||
"github.com/charmbracelet/bubbles/viewport" | ||
) | ||
|
||
type item groupparser.Group | ||
|
||
func (i item) FilterValue() string { return i.Details.Name } | ||
|
||
type BubbleGroup struct { | ||
list list.Model | ||
viewport viewport.Model | ||
} | ||
|
||
func InitialModel() BubbleGroup { | ||
items := groupToItem(groupparser.GetGroups()) | ||
l := list.New(items, itemDelegate{}, 0, 0) | ||
l.Title = "Groups" | ||
l.SetShowHelp(false) | ||
|
||
return BubbleGroup{list: l} | ||
} | ||
|
||
func groupToItem(groups []groupparser.Group) []list.Item { | ||
items := make([]list.Item, len(groups)) | ||
for i,v := range groups { | ||
items[i] = item(v) | ||
} | ||
|
||
return items | ||
|
||
} |
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,24 @@ | ||
package group | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func (bg BubbleGroup) Update (msg tea.Msg) (BubbleGroup, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
if msg.String() == "ctrl+c" { | ||
return bg, tea.Quit | ||
} | ||
case tea.WindowSizeMsg: | ||
bg.list.SetSize(msg.Width, msg.Height) | ||
bg.viewport = viewport.New(msg.Width, msg.Height) | ||
bg.viewport.SetContent(bg.detailView()) | ||
} | ||
|
||
var cmd tea.Cmd | ||
bg.list, cmd = bg.list.Update(msg) | ||
|
||
return bg, cmd | ||
} |
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,23 @@ | ||
package group | ||
|
||
import ( | ||
"github.com/ariasmn/ugm/internal/tui/common" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
func (bg BubbleGroup) View() string { | ||
bg.viewport.SetContent(bg.detailView()) | ||
|
||
return lipgloss.JoinHorizontal( | ||
lipgloss.Top, bg.listView(), bg.viewport.View()) | ||
} | ||
|
||
func (bg BubbleGroup) listView() string { | ||
bg.list.Styles.Title = common.ListColorStyle | ||
|
||
return common.ListStyle.Render(bg.list.View()) | ||
} | ||
|
||
func (bg BubbleGroup) detailView() string { | ||
return "TODO" | ||
} |
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,88 @@ | ||
package tui | ||
|
||
import ( | ||
"github.com/ariasmn/ugm/internal/tui/group" | ||
"github.com/ariasmn/ugm/internal/tui/user" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type state int | ||
|
||
const ( | ||
showUserView state = iota | ||
showGroupView | ||
) | ||
|
||
type model struct { | ||
state state | ||
bu user.BubbleUser | ||
bg group.BubbleGroup | ||
width, height int | ||
} | ||
|
||
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.WindowSizeMsg: | ||
m.width = msg.Width | ||
m.height = msg.Height | ||
case tea.KeyMsg: | ||
if msg.String() == "ctrl+c" { | ||
return m, tea.Quit | ||
} | ||
if msg.String() == "tab" { | ||
return updateByState(m) | ||
} | ||
} | ||
|
||
switch m.state { | ||
case showUserView: | ||
m.bu, cmd = m.bu.Update(msg) | ||
return m, cmd | ||
case showGroupView: | ||
m.bg, cmd = m.bg.Update(msg) | ||
return m, cmd | ||
default: | ||
return m, nil | ||
} | ||
} | ||
|
||
func (m model) View() string { | ||
switch m.state { | ||
case showGroupView: | ||
return m.bg.View() | ||
default: | ||
return m.bu.View() | ||
} | ||
} | ||
|
||
func InitialModel() model { | ||
return model{ | ||
state: showUserView, | ||
bu: user.InitialModel(), | ||
bg: group.InitialModel(), | ||
} | ||
} | ||
|
||
func updateByState(m model) (model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
windowSizeMsg := tea.WindowSizeMsg { | ||
Width: m.width, | ||
Height: m.height, | ||
} | ||
|
||
if m.state == showUserView { | ||
m.state = showGroupView | ||
m.bg, cmd = m.bg.Update(windowSizeMsg) | ||
} else { | ||
m.state = showUserView | ||
m.bu, cmd = m.bu.Update(windowSizeMsg) | ||
} | ||
|
||
return m, cmd | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package tui | ||
package user | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package tui | ||
package user | ||
|
||
import ( | ||
"github.com/ariasmn/ugm/userparser" | ||
|
Oops, something went wrong.