Skip to content

Commit

Permalink
refactor commit reset menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Feb 14, 2020
1 parent 665fdde commit c971460
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
49 changes: 49 additions & 0 deletions pkg/gui/commit_reset_menu_panel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gui

import (
"fmt"

"github.com/fatih/color"
"github.com/jesseduffield/gocui"
)

func (gui *Gui) handleCreateCommitResetMenu(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit(g)
if commit == nil {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoCommitsThisBranch"))
}

strengths := []string{"soft", "mixed", "hard"}
menuItems := make([]*menuItem, len(strengths))
for i, strength := range strengths {
innerStrength := strength
menuItems[i] = &menuItem{
displayStrings: []string{
fmt.Sprintf("%s reset", strength),
color.New(color.FgRed).Sprint(
fmt.Sprintf("reset --%s %s", strength, commit.Sha),
),
},
onPress: func() error {
if err := gui.GitCommand.ResetToCommit(commit.Sha, innerStrength); err != nil {
return err
}

if err := gui.refreshCommits(g); err != nil {
return err
}
if err := gui.refreshFiles(); err != nil {
return err
}
if err := gui.resetOrigin(gui.getCommitsView()); err != nil {
return err
}

gui.State.Panels.Commits.SelectedLine = 0
return gui.handleCommitSelect(g, gui.getCommitsView())
},
}
}

return gui.createMenuNew(fmt.Sprintf("%s %s", gui.Tr.SLocalize("resetTo"), commit.Sha), menuItems, createMenuOptions{showCancel: true})
}
49 changes: 0 additions & 49 deletions pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package gui

import (
"fmt"
"strconv"

"github.com/fatih/color"
"github.com/go-errors/errors"

"github.com/jesseduffield/gocui"
Expand Down Expand Up @@ -560,53 +558,6 @@ func (gui *Gui) handleSquashAllAboveFixupCommits(g *gocui.Gui, v *gocui.View) er
}, nil)
}

type resetOption struct {
description string
command string
}

// GetDisplayStrings is a function.
func (r *resetOption) GetDisplayStrings(isFocused bool) []string {
return []string{r.description, color.New(color.FgRed).Sprint(r.command)}
}

func (gui *Gui) handleCreateCommitResetMenu(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit(g)
if commit == nil {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoCommitsThisBranch"))
}

strengths := []string{"soft", "mixed", "hard"}
options := make([]*resetOption, len(strengths))
for i, strength := range strengths {
options[i] = &resetOption{
description: fmt.Sprintf("%s reset", strength),
command: fmt.Sprintf("reset --%s %s", strength, commit.Sha),
}
}

handleMenuPress := func(index int) error {
if err := gui.GitCommand.ResetToCommit(commit.Sha, strengths[index]); err != nil {
return err
}

if err := gui.refreshCommits(g); err != nil {
return err
}
if err := gui.refreshFiles(); err != nil {
return err
}
if err := gui.resetOrigin(gui.getCommitsView()); err != nil {
return err
}

gui.State.Panels.Commits.SelectedLine = 0
return gui.handleCommitSelect(g, gui.getCommitsView())
}

return gui.createMenu(fmt.Sprintf("%s %s", gui.Tr.SLocalize("resetTo"), commit.Sha), options, len(options), handleMenuPress)
}

func (gui *Gui) handleTagCommit(g *gocui.Gui, v *gocui.View) error {
// TODO: bring up menu asking if you want to make a lightweight or annotated tag
// if annotated, switch to a subprocess to create the message
Expand Down

0 comments on commit c971460

Please sign in to comment.