1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-24 08:52:21 +02:00

Improve code quality

- Make CommandMenuEntry private
- create candidates only once we really need it
- Use only 1 buffer
- Clearify CommandMenuEntry creation fields
This commit is contained in:
mjarkk 2021-08-06 21:50:53 +02:00
parent dcd3b7c058
commit ea136e4e77
2 changed files with 22 additions and 22 deletions

View File

@ -33,7 +33,7 @@ type CustomCommandObjects struct {
PromptResponses []string
}
type CommandMenuEntry struct {
type commandMenuEntry struct {
label string
value string
}
@ -123,30 +123,30 @@ func (gui *Gui) menuPrompt(prompt config.CustomCommandPrompt, promptResponses []
return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
}
func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]CommandMenuEntry, error) {
candidates := []CommandMenuEntry{}
func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]commandMenuEntry, error) {
reg, err := regexp.Compile(filter)
if err != nil {
return candidates, gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
return nil, gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
}
valueBuff := bytes.NewBuffer(nil)
buff := bytes.NewBuffer(nil)
valueTemp, err := template.New("format").Parse(valueFormat)
if err != nil {
return candidates, gui.surfaceError(errors.New("unable to parse value format, error: " + err.Error()))
return nil, gui.surfaceError(errors.New("unable to parse value format, error: " + err.Error()))
}
descBuff := bytes.NewBuffer(nil)
descTemp, err := template.New("format").Parse(labelFormat)
if err != nil {
return candidates, gui.surfaceError(errors.New("unable to parse label format, error: " + err.Error()))
return nil, gui.surfaceError(errors.New("unable to parse label format, error: " + err.Error()))
}
candidates := []commandMenuEntry{}
for _, str := range strings.Split(string(commandOutput), "\n") {
if str == "" {
continue
}
tmplData := map[string]string{}
out := reg.FindAllStringSubmatch(str, -1)
if len(out) > 0 {
@ -161,28 +161,28 @@ func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, label
}
}
err = valueTemp.Execute(valueBuff, tmplData)
err = valueTemp.Execute(buff, tmplData)
if err != nil {
return candidates, gui.surfaceError(err)
}
entry := commandMenuEntry{
value: strings.TrimSpace(buff.String()),
}
if labelFormat != "" {
err = descTemp.Execute(descBuff, tmplData)
buff.Reset()
err = descTemp.Execute(buff, tmplData)
if err != nil {
return candidates, gui.surfaceError(err)
}
entry.label = strings.TrimSpace(buff.String())
} else {
descBuff.Write(valueBuff.Bytes())
entry.label = entry.value
}
entry := CommandMenuEntry{
strings.TrimSpace(descBuff.String()),
strings.TrimSpace(valueBuff.String()),
}
candidates = append(candidates, entry)
valueBuff.Reset()
descBuff.Reset()
buff.Reset()
}
return candidates, err
}

View File

@ -88,7 +88,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
filter string
valueFormat string
labelFormat string
test func([]CommandMenuEntry, error)
test func([]commandMenuEntry, error)
}
scenarios := []scenario{
@ -98,7 +98,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
"(?P<remote>[a-z_]+)/(?P<branch>.*)",
"{{ .branch }}",
"Remote: {{ .remote }}",
func(actualEntry []CommandMenuEntry, err error) {
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1", actualEntry[0].value)
assert.EqualValues(t, "Remote: upstream", actualEntry[0].label)
@ -110,7 +110,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .branch }}|{{ .remote }}",
"",
func(actualEntry []CommandMenuEntry, err error) {
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label)
@ -122,7 +122,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .group_2 }}|{{ .group_1 }}",
"Remote: {{ .group_1 }}",
func(actualEntry []CommandMenuEntry, err error) {
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "Remote: upstream", actualEntry[0].label)