From 0a7efaf82c2407816ada5fa68131fad39f1ba9ad Mon Sep 17 00:00:00 2001 From: JNLei Date: Sun, 7 Apr 2024 11:43:19 -0400 Subject: [PATCH] feat(ui_commit_selector.go): Dynamic commit type list detect current branch name and if it match a available commit type, replace the commit type to the top of selections Signed-off-by: JNLei --- ui_commit_selector.go | 44 ++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/ui_commit_selector.go b/ui_commit_selector.go index e4fe664..c7c70b4 100644 --- a/ui_commit_selector.go +++ b/ui_commit_selector.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io" + "strings" "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/list" @@ -106,17 +107,22 @@ func (m selectorModel) View() string { } func newSelectorModel() selectorModel { - l := list.NewModel([]list.Item{ - selectorItem{ct: feat, title: featDesc}, - selectorItem{ct: fix, title: fixDesc}, - selectorItem{ct: docs, title: docsDesc}, - selectorItem{ct: style, title: styleDesc}, - selectorItem{ct: refactor, title: refactorDesc}, - selectorItem{ct: test, title: testDesc}, - selectorItem{ct: chore, title: choreDesc}, - selectorItem{ct: perf, title: perfDesc}, - selectorItem{ct: hotfix, title: hotfixDesc}, - }, selectorDelegate{}, 20, 12) + prioritized := prioritizeCommitType([]selectorItem{ + {ct: feat, title: featDesc}, + {ct: fix, title: fixDesc}, + {ct: docs, title: docsDesc}, + {ct: style, title: styleDesc}, + {ct: refactor, title: refactorDesc}, + {ct: test, title: testDesc}, + {ct: chore, title: choreDesc}, + {ct: perf, title: perfDesc}, + {ct: hotfix, title: hotfixDesc}, + }) + listItems := []list.Item{} + for _, commitType := range prioritized { + listItems = append(listItems, commitType) + } + l := list.NewModel(listItems, selectorDelegate{}, 20, 12) l.Title = "Select Commit Type" l.SetShowStatusBar(false) @@ -131,3 +137,19 @@ func newSelectorModel() selectorModel { return selectorModel{list: l} } + +func prioritizeCommitType(items []selectorItem) []selectorItem { + branch, err := currentBranch() + if err != nil { + return items + } + + currentBranchType := strings.Split(branch, "/")[0] + for ind, branchType := range items { + if currentBranchType == branchType.ct { + return append(append([]selectorItem{branchType}, items[:ind]...), items[ind+1:]...) + } + } + + return items +}