Repository: jesseduffield/lazygit
File: pkg/gui/controllers/helpers/refs_helper.go (Go)

Code before:
func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string) error {
	if err := self.c.Git().Commit.ResetToCommit(ref, strength, envVars); err != nil {
		return self.c.Error(err)
	}

	self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
	self.c.Contexts().ReflogCommits.SetSelectedLineIdx(0)
	// loading a heap of commits is slow so we limit them whenever doing a reset
	self.c.Contexts().LocalCommits.SetLimitCommits(true)

	if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES, types.BRANCHES, types.REFLOG, types.COMMITS}}); err != nil {
		return err
	}

	return nil
}

func (self *RefsHelper) CreateSortOrderMenu(onSelected func(sortOrder string)) error {
	type sortOrderWithKey struct {
		key   types.Key
		sort  string
		label string
	}
	sortKeys := []sortOrderWithKey{
		{sort: "refname", label: "refname", key: 'r'},
		{sort: "-committerdate", label: "-committerdate", key: 'c'},
	}

	menuItems := lo.Map(sortKeys, func(row sortOrderWithKey, _ int) *types.MenuItem {
		return &types.MenuItem{
			LabelColumns: []string{
				row.label,
				style.FgDefault.Sprintf("--sort=%s", row.sort),
			},
			OnPress: func() error {
				onSelected(row.sort)
				return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
			},
			Key: row.key,
		}
	})

	return self.c.Menu(types.CreateMenuOptions{
		Title: self.c.Tr.SortOptionsTitle,
		Items: menuItems,
	})
}

func (self *RefsHelper) CreateGitResetMenu(ref string) error {
	type strengthWithKey struct {
		strength string

Review comment (suggestion):
The labels sound way too technical to me. I'd prefer "alphabetical" (keybinding "a") and "date" (keybinding "d").

It's good that you're showing the actual sort key in the second column, this will give users enough information what they are choosing.

Code after:
func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string) error {
	if err := self.c.Git().Commit.ResetToCommit(ref, strength, envVars); err != nil {
		return self.c.Error(err)
	}

	self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
	self.c.Contexts().ReflogCommits.SetSelectedLineIdx(0)
	// loading a heap of commits is slow so we limit them whenever doing a reset
	self.c.Contexts().LocalCommits.SetLimitCommits(true)

	if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES, types.BRANCHES, types.REFLOG, types.COMMITS}}); err != nil {
		return err
	}

	return nil
}

func (self *RefsHelper) CreateSortOrderMenu(onSelected func(sortOrder string) error) error {
	type sortOrderWithKey struct {
		key       types.Key
		label     string
		sortKey   string
		sortOrder string
	}
	sortKeys := []sortOrderWithKey{
		{label: self.c.Tr.SortAlphabetical, sortKey: "refname", sortOrder: "alphabetical", key: 'a'},
		{label: self.c.Tr.SortByDate, sortKey: "-committerdate", sortOrder: "date", key: 'd'},
	}

	menuItems := lo.Map(sortKeys, func(row sortOrderWithKey, _ int) *types.MenuItem {
		return &types.MenuItem{
			LabelColumns: []string{
				row.label,
				style.FgYellow.Sprintf("--sort=%s", row.sortKey),
			},
			OnPress: func() error {
				return onSelected(row.sortOrder)
			},
			Key: row.key,
		}
	})

	return self.c.Menu(types.CreateMenuOptions{
		Title: self.c.Tr.SortOrder,
		Items: menuItems,
	})
}

func (self *RefsHelper) CreateGitResetMenu(ref string) error {
	type strengthWithKey struct {
		strength string