Skip to content

Commit

Permalink
feat(branches): delete branch with prompt (#430)
Browse files Browse the repository at this point in the history
* feat(branches): delete branch with confirmation

* fix: remove log
  • Loading branch information
dlvhdr authored Aug 31, 2024
1 parent 2c887b3 commit 59bdd14
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 44 deletions.
32 changes: 32 additions & 0 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,35 @@ func (m *Model) OpenGithub() tea.Cmd {
b := m.getFilteredBranches()[row]
return tasks.OpenBranchPR(m.Ctx, tasks.SectionIdentifer{Id: 0, Type: SectionType}, b.Data.Name)
}

func (m *Model) deleteBranch() tea.Cmd {
b := m.getCurrBranch()

taskId := fmt.Sprintf("delete_%s_%d", b.Data.Name, time.Now().Unix())
task := context.Task{
Id: taskId,
StartText: fmt.Sprintf("Deleting branch %s", b.Data.Name),
FinishedText: fmt.Sprintf("Branch %s has been deleted", b.Data.Name),
State: context.TaskStart,
Error: nil,
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
err := gitm.DeleteBranch(*m.Ctx.RepoPath, b.Data.Name, gitm.DeleteBranchOptions{Force: true})
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}

return constants.TaskFinishedMsg{
SectionId: 0,
SectionType: SectionType,
TaskId: taskId,
Msg: repoMsg{repo: repo},
Err: err,
}
})
}
44 changes: 0 additions & 44 deletions ui/components/reposection/delete.go
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
package reposection

import (
"fmt"
"time"

gitm "github.com/aymanbagabas/git-module"
tea "github.com/charmbracelet/bubbletea"

"github.com/dlvhdr/gh-dash/v4/git"
"github.com/dlvhdr/gh-dash/v4/ui/constants"
"github.com/dlvhdr/gh-dash/v4/ui/context"
)

func (m *Model) delete() (tea.Cmd, error) {
b := m.getCurrBranch()

taskId := fmt.Sprintf("delete_%s_%d", b.Data.Name, time.Now().Unix())
task := context.Task{
Id: taskId,
StartText: fmt.Sprintf("Deleting branch %s", b.Data.Name),
FinishedText: fmt.Sprintf("Branch %s has been deleted", b.Data.Name),
State: context.TaskStart,
Error: nil,
}
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
err := gitm.DeleteBranch(*m.Ctx.RepoPath, b.Data.Name, gitm.DeleteBranchOptions{Force: true})
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
repo, err := git.GetRepo(*m.Ctx.RepoPath)
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}

return constants.TaskFinishedMsg{
SectionId: 0,
SectionType: SectionType,
TaskId: taskId,
Msg: repoMsg{repo: repo},
Err: err,
}
}), nil
}
42 changes: 42 additions & 0 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/dlvhdr/gh-dash/v4/ui/components/search"
"github.com/dlvhdr/gh-dash/v4/ui/components/section"
"github.com/dlvhdr/gh-dash/v4/ui/components/table"
"github.com/dlvhdr/gh-dash/v4/ui/components/tasks"
"github.com/dlvhdr/gh-dash/v4/ui/constants"
"github.com/dlvhdr/gh-dash/v4/ui/context"
"github.com/dlvhdr/gh-dash/v4/ui/keys"
Expand Down Expand Up @@ -90,6 +91,43 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
break
}

if m.IsPromptConfirmationFocused() {
switch {

case msg.Type == tea.KeyCtrlC, msg.Type == tea.KeyEsc:
m.PromptConfirmationBox.Reset()
cmd = m.SetIsPromptConfirmationShown(false)
return m, cmd

case msg.Type == tea.KeyEnter:
input := m.PromptConfirmationBox.Value()
action := m.GetPromptConfirmationAction()
pr := findPRForRef(m.Prs, m.getCurrBranch().Data.Name)
sid := tasks.SectionIdentifer{Id: m.Id, Type: SectionType}
if input == "Y" || input == "y" {
switch action {
case "delete":
cmd = m.deleteBranch()
case "close":
cmd = tasks.ClosePR(m.Ctx, sid, pr)
case "reopen":
cmd = tasks.ReopenPR(m.Ctx, sid, pr)
case "ready":
cmd = tasks.PRReady(m.Ctx, sid, pr)
case "merge":
cmd = tasks.MergePR(m.Ctx, sid, pr)
}
}

m.PromptConfirmationBox.Reset()
blinkCmd := m.SetIsPromptConfirmationShown(false)

return m, tea.Batch(cmd, blinkCmd)
}

break
}

switch {
case key.Matches(msg, keys.BranchKeys.Checkout):
cmd, err = m.checkout()
Expand Down Expand Up @@ -134,6 +172,10 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
cmds = append(cmds, searchCmd)
m.SearchBar = search

prompt, promptCmd := m.PromptConfirmationBox.Update(msg)
cmds = append(cmds, promptCmd)
m.PromptConfirmationBox = prompt

m.Table.SetRows(m.BuildRows())

m.Table.SetRows(m.BuildRows())
Expand Down

0 comments on commit 59bdd14

Please sign in to comment.