diff --git a/ui/components/reposection/commands.go b/ui/components/reposection/commands.go index 822980bc..4a592aca 100644 --- a/ui/components/reposection/commands.go +++ b/ui/components/reposection/commands.go @@ -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, + } + }) +} diff --git a/ui/components/reposection/delete.go b/ui/components/reposection/delete.go index 478fd0c9..604e090f 100644 --- a/ui/components/reposection/delete.go +++ b/ui/components/reposection/delete.go @@ -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 -} diff --git a/ui/components/reposection/reposection.go b/ui/components/reposection/reposection.go index 172d2628..26f021a4 100644 --- a/ui/components/reposection/reposection.go +++ b/ui/components/reposection/reposection.go @@ -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" @@ -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() @@ -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())