Skip to content

Commit

Permalink
Implement list UI for showing repos
Browse files Browse the repository at this point in the history
  • Loading branch information
carhartl committed Oct 21, 2024
1 parent 19c334a commit 87cf33b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 23 deletions.
111 changes: 88 additions & 23 deletions cmd/git-unsaved/main.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,125 @@
package main

import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"

"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/urfave/cli/v2"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)

type repoMsg struct {
repo repo
}
type doneMsg struct{}
type errMsg error

type repo struct {
path string
}

func (r repo) Title() string {
return r.path
}

func (r repo) Description() string {
return "Foo" // TODO: Gather Git status here, separated by \n
}

func (r repo) FilterValue() string {
return r.path
}

type model struct {
spinner spinner.Model
list list.Model
sub chan repoMsg
quitting bool
err error
}

func initialModel() model {
s := spinner.New()
s.Spinner = spinner.MiniDot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("86"))
return model{spinner: s}
}

func (m model) Init() tea.Cmd {
return m.spinner.Tick
return tea.Batch(
m.list.StartSpinner(),
getRepos(m.sub),
waitForRepoStatus(m.sub),
)
}

// TODO: upon selection + enter, cd to directory!
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case "ctrl+c", "q":
m.quitting = true
return m, tea.Quit
default:
return m, nil
}

case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
case repoMsg:
return m, tea.Sequence(
m.list.InsertItem(len(m.list.Items()), msg.repo),
waitForRepoStatus(m.sub), // wait for next found repo
)
case doneMsg:
m.list.StopSpinner()
return m, nil
case errMsg:
m.err = msg
return m, nil

default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}

var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

func (m model) View() string {
if m.err != nil {
return m.err.Error()
}
str := fmt.Sprintf("\n\n%s Scanning Git repositories...\n\n", m.spinner.View())
s := m.list.View()
if m.quitting {
return str + "\n"
s += "\n"
}
return s
}

func getRepos(sub chan repoMsg) tea.Cmd {
return func() tea.Msg {
err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
// TODO: filter for git repos
abspath, err := filepath.Abs(path)
if err != nil {
return err
}
sub <- repoMsg{repo: repo{path: abspath}}
}
return nil
})
if err != nil {
return err
}
return doneMsg{}
}
}

func waitForRepoStatus(sub chan repoMsg) tea.Cmd {
return func() tea.Msg {
return repoMsg(<-sub)
}
return str
}

func main() {
Expand All @@ -83,7 +142,13 @@ Flags:
Usage: "Find all your dirty Git repositories",
Version: "v0.0.1",
Action: func(*cli.Context) error {
p := tea.NewProgram(initialModel())
l := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
l.Title = "Repositories"
l.SetSpinner(spinner.MiniDot)
l.ToggleSpinner()
m := model{list: l, sub: make(chan repoMsg)}
// TODO: how to pass along path argument?
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
)

Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
Expand All @@ -14,6 +16,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
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.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand All @@ -33,6 +37,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
Expand Down

0 comments on commit 87cf33b

Please sign in to comment.