Skip to content

Commit

Permalink
Merge pull request #9 from masonkmeyer/type-to-search
Browse files Browse the repository at this point in the history
adds ability to filter branches by name
  • Loading branch information
masonkmeyer authored Nov 16, 2024
2 parents 1964fe2 + 546faa8 commit 031986b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 10 additions & 0 deletions jet/runtime/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func Run(exitChannel chan string) {

g.SetManager(controller)

// wire up all the type keys
for _, c := range ".-_/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" {
g.SetKeybinding("", c, gocui.ModNone, controller.OnType(c))
}

// wire up all the delete keys
g.SetKeybinding("", gocui.KeyBackspace2, gocui.ModNone, controller.OnBackspace)
g.SetKeybinding("", gocui.KeyBackspace, gocui.ModNone, controller.OnBackspace)
g.SetKeybinding("", gocui.KeyDelete, gocui.ModNone, controller.OnBackspace)

if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, controller.Quit); err != nil {
log.Panicln(err)
}
Expand Down
35 changes: 33 additions & 2 deletions jet/ui/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ const (
BRANCHES = "branches"
LOGS = "logs"
RECENT_LOGS = "recent_logs"
FILTER = "filter"
)

// Controller is the main controller for the UI
type Controller struct {
filter string
g *gocui.Gui
git *jet.Git
recentCommitMessage string
Expand All @@ -29,6 +31,7 @@ type Controller struct {
// NewController creates a new UI controller
func NewController(g *gocui.Gui, exitChannel chan string) *Controller {
c := &Controller{
filter: "",
g: g,
git: &jet.Git{},
recentCommitMessage: "",
Expand All @@ -39,6 +42,26 @@ func NewController(g *gocui.Gui, exitChannel chan string) *Controller {
return c
}

func (c *Controller) OnBackspace(g *gocui.Gui, v *gocui.View) error {
if len(c.filter) == 0 {
return nil
}

c.filter = c.filter[:len(c.filter)-1]
g.DeleteView(BRANCHES)
g.DeleteView(FILTER)
return nil
}

func (c *Controller) OnType(char rune) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
c.filter += string(char)
g.DeleteView(BRANCHES)
g.DeleteView(FILTER)
return nil
}
}

// Quit is the handler for the quit keybinding
func (c *Controller) Quit(g *gocui.Gui, v *gocui.View) error {
go func() { c.exitChannel <- "" }()
Expand All @@ -59,6 +82,10 @@ func (c *Controller) Layout(g *gocui.Gui) error {
}

for _, branch := range branches {
if c.filter != "" && !strings.Contains(branch, c.filter) {
continue
}

parts := strings.Split(branch, " ")
value := parts[len(parts)-1]

Expand Down Expand Up @@ -88,11 +115,16 @@ func (c *Controller) Layout(g *gocui.Gui) error {
textView.Render(v, view.WithWrap(true), view.WithTitle("Recent Commit Message"), view.WithFgColor(gocui.ColorYellow))
}

if v, err := g.SetView(RECENT_LOGS, 0, maxY/2, maxX-1, maxY-1); err != nil {
if v, err := g.SetView(RECENT_LOGS, 0, maxY/2-3, maxX-1, maxY-3); err != nil {
textView := view.NewText(g, viewmodel.Text{Value: c.gitGraph}, LOGS)
textView.Render(v, view.WithWrap(true), view.WithTitle("Recent Commits"))
}

if v, err := g.SetView(FILTER, 0, maxY-3, maxX-1, maxY-1); err != nil {
textView := view.NewText(g, viewmodel.Text{Value: c.filter}, FILTER)
textView.Render(v, view.WithTitle("Filter"))
}

g.SetCurrentView(BRANCHES)

return nil
Expand All @@ -109,7 +141,6 @@ func (c *Controller) onSelected(item *viewmodel.MenuItem) error {

// onChange is the handler for when the selected branch changes
func (c *Controller) onChange(item *viewmodel.MenuItem) error {

results := c.git.Logs(item.Value, "-1")

c.recentCommitMessage = results
Expand Down

0 comments on commit 031986b

Please sign in to comment.