1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-21 00:30:00 +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 PromptResponses []string
} }
type CommandMenuEntry struct { type commandMenuEntry struct {
label string label string
value string value string
} }
@ -123,30 +123,30 @@ func (gui *Gui) menuPrompt(prompt config.CustomCommandPrompt, promptResponses []
return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true}) return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
} }
func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]CommandMenuEntry, error) { func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]commandMenuEntry, error) {
candidates := []CommandMenuEntry{}
reg, err := regexp.Compile(filter) reg, err := regexp.Compile(filter)
if err != nil { 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) valueTemp, err := template.New("format").Parse(valueFormat)
if err != nil { 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) descTemp, err := template.New("format").Parse(labelFormat)
if err != nil { 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") { for _, str := range strings.Split(string(commandOutput), "\n") {
if str == "" { if str == "" {
continue continue
} }
tmplData := map[string]string{} tmplData := map[string]string{}
out := reg.FindAllStringSubmatch(str, -1) out := reg.FindAllStringSubmatch(str, -1)
if len(out) > 0 { 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 { if err != nil {
return candidates, gui.surfaceError(err) return candidates, gui.surfaceError(err)
} }
entry := commandMenuEntry{
value: strings.TrimSpace(buff.String()),
}
if labelFormat != "" { if labelFormat != "" {
err = descTemp.Execute(descBuff, tmplData) buff.Reset()
err = descTemp.Execute(buff, tmplData)
if err != nil { if err != nil {
return candidates, gui.surfaceError(err) return candidates, gui.surfaceError(err)
} }
entry.label = strings.TrimSpace(buff.String())
} else { } else {
descBuff.Write(valueBuff.Bytes()) entry.label = entry.value
} }
entry := CommandMenuEntry{
strings.TrimSpace(descBuff.String()),
strings.TrimSpace(valueBuff.String()),
}
candidates = append(candidates, entry) candidates = append(candidates, entry)
valueBuff.Reset() buff.Reset()
descBuff.Reset()
} }
return candidates, err return candidates, err
} }

View File

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