Skip to content

Commit

Permalink
Big change and implementation.
Browse files Browse the repository at this point in the history
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
ariasmn committed Jul 19, 2022
1 parent 057c49f commit 5e8f357
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 47 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ go 1.18
require (
github.com/charmbracelet/bubbles v0.11.0
github.com/charmbracelet/bubbletea v0.21.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/evertras/bubble-table v0.14.4
github.com/muesli/reflow v0.3.0
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/charmbracelet/lipgloss v0.5.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/evertras/bubble-table v0.14.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
Expand Down
22 changes: 4 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/bubbles v0.10.3 h1:fKarbRaObLn/DCsZO4Y3vKCwRUzynQD9L+gGev1E/ho=
github.com/charmbracelet/bubbles v0.10.3/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPALKfpGVdJu8UiJsA=
github.com/charmbracelet/bubbles v0.11.0 h1:fBLyY0PvJnd56Vlu5L84JJH6f4axhgIJ9P3NET78f0Q=
github.com/charmbracelet/bubbles v0.11.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWoiNibae+1yCMtcc=
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
github.com/charmbracelet/bubbletea v0.20.0 h1:/b8LEPgCbNr7WWZ2LuE/BV1/r4t5PyYJtDb+J3vpwxc=
github.com/charmbracelet/bubbletea v0.20.0/go.mod h1:zpkze1Rioo4rJELjRyGlm9T2YNou1Fm4LIJQSa5QMEM=
github.com/charmbracelet/bubbletea v0.21.0 h1:f3y+kanzgev5PA916qxmDybSHU3N804uOnKnhRPXTcI=
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/evertras/bubble-table v0.14.4 h1:UHUiPfsJ+lqbPSHIM1n7O8Ie2tbK0r9ReicXFnLg44I=
github.com/evertras/bubble-table v0.14.4/go.mod h1:SPOZKbIpyYWPHBNki3fyNpiPBQkvkULAtOT7NTD5fKY=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand All @@ -38,27 +29,22 @@ github.com/muesli/cancelreader v0.2.0/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIf
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
20 changes: 10 additions & 10 deletions internal/tui/styles.go → internal/tui/common/styles.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package tui
package common

import "github.com/charmbracelet/lipgloss"

var (
listStyle = lipgloss.NewStyle().
ListStyle = lipgloss.NewStyle().
Width(35).
PaddingRight(3).
MarginRight(3).
Border(lipgloss.RoundedBorder(), false, true, false, false)
listColorStyle = lipgloss.NewStyle().
ListColorStyle = lipgloss.NewStyle().
Background(lipgloss.Color("#3d719c"))
listItemStyle = lipgloss.NewStyle().
ListItemStyle = lipgloss.NewStyle().
PaddingLeft(4)
listSelectedlistItemStyle = lipgloss.NewStyle().
ListSelectedListItemStyle = lipgloss.NewStyle().
PaddingLeft(2).
Foreground(lipgloss.Color("#569cd6"))
detailStyle = lipgloss.NewStyle().
DetailStyle = lipgloss.NewStyle().
PaddingTop(2)
dividerStyle = lipgloss.NewStyle().
DividerStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#9B9B9B", Dark: "#5C5C5C"}).
PaddingTop(1).
PaddingBottom(1)
tableMainStyle = lipgloss.NewStyle().
TableMainStyle = lipgloss.NewStyle().
Align(lipgloss.Center)
tableHeaderStyle = lipgloss.NewStyle().
TableHeaderStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#569cd6")).
Bold(true)
headerStyle = lipgloss.NewStyle().
HeaderStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#569cd6")).
PaddingBottom(1).
Bold(true).
Expand Down
7 changes: 7 additions & 0 deletions internal/tui/group/init.go
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
}
32 changes: 32 additions & 0 deletions internal/tui/group/list.go
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)
}
35 changes: 35 additions & 0 deletions internal/tui/group/model.go
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

}
24 changes: 24 additions & 0 deletions internal/tui/group/update.go
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
}
23 changes: 23 additions & 0 deletions internal/tui/group/view.go
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"
}
88 changes: 88 additions & 0 deletions internal/tui/tui.go
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
}
2 changes: 1 addition & 1 deletion internal/tui/init.go → internal/tui/user/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tui
package user

import (
tea "github.com/charmbracelet/bubbletea"
Expand Down
7 changes: 4 additions & 3 deletions internal/tui/list.go → internal/tui/user/list.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package tui
package user

import (
"fmt"
"io"

"github.com/charmbracelet/bubbles/list"
"github.com/ariasmn/ugm/internal/tui/common"
tea "github.com/charmbracelet/bubbletea"
)

Expand All @@ -22,9 +23,9 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
line := user.Details.Username

if index == m.Index() {
line = listSelectedlistItemStyle.Render("> " + line)
line = common.ListSelectedListItemStyle.Render("> " + line)
} else {
line = listItemStyle.Render(line)
line = common.ListItemStyle.Render(line)
}

fmt.Fprint(w, line)
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/model.go → internal/tui/user/model.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tui
package user

import (
"github.com/ariasmn/ugm/userparser"
Expand Down
Loading

0 comments on commit 5e8f357

Please sign in to comment.