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

Code before:
				OnPress: func() error {
					if !self.c.Helpers().WorkingTree.IsWorkingTreeDirty() {
						return self.c.ErrorMsg(self.c.Tr.NoFilesToStash)
					}
					if self.c.Helpers().WorkingTree.AnyStagedFiles() {
						return self.handleStashSave(self.c.Git().Stash.StashUnstagedChanges, self.c.Tr.Actions.StashUnstagedChanges)
					}
					// ordinary stash
					return self.handleStashSave(self.c.Git().Stash.Push, self.c.Tr.Actions.StashUnstagedChanges)
				},
				Key: 'u',
			},
		},
	})
}

func (self *FilesController) openCopyMenu() error {
	copyNameNoItemSelectionErr := utils.ResolvePlaceholderString(
		self.c.Tr.FileCopyNoItemSelectedError,
		map[string]string{"target": "name"},
	)
	copyNameItem := &types.MenuItem{
		Label: self.c.Tr.CopyFileName,
		OnPress: func() error {
			name := self.context().GetSelectedItemId()
			if name == "" {
				return self.c.ErrorMsg(copyNameNoItemSelectionErr)
			}

			return self.copyToClipboard(name)
		},
		Key: 'n',
	}

	copyDiffNoItemSelectionErr := utils.ResolvePlaceholderString(
		self.c.Tr.FileCopyNoItemSelectedError,
		map[string]string{"target": "diff"},
	)
	copyFileDiffItem := &types.MenuItem{
		Label:   self.c.Tr.CopySelectedDiff,
		Tooltip: self.c.Tr.CopyFileDiffTooltip,
		OnPress: func() error {
			path := self.context().GetSelectedPath()
			if path == "" {
				return self.c.ErrorMsg(copyDiffNoItemSelectionErr)
			}
			diff, err := self.c.Git().Diff.GetPathDiff(path, self.hasPathStagedChanges())
			if err != nil {
				return self.c.Error(err)
			}
			return self.copyToClipboard(diff)

Review comment (suggestion):
This shouldn't be possible. We should set disabledReasons for all commands in all cases where a command isn't applicable, so that we never have to return errors from handlers.

Code after:
					if !self.c.Helpers().WorkingTree.IsWorkingTreeDirty() {
						return self.c.ErrorMsg(self.c.Tr.NoFilesToStash)
					}
					if self.c.Helpers().WorkingTree.AnyStagedFiles() {
						return self.handleStashSave(self.c.Git().Stash.StashUnstagedChanges, self.c.Tr.Actions.StashUnstagedChanges)
					}
					// ordinary stash
					return self.handleStashSave(self.c.Git().Stash.Push, self.c.Tr.Actions.StashUnstagedChanges)
				},
				Key: 'u',
			},
		},
	})
}

func (self *FilesController) openCopyMenu() error {
	node := self.context().GetSelected()

	copyNameItem := &types.MenuItem{
		Label: self.c.Tr.CopyFileName,
		OnPress: func() error {
			if err := self.c.OS().CopyToClipboard(node.Name()); err != nil {
				return self.c.Error(err)
			}
			self.c.Toast(self.c.Tr.FileNameCopiedToast)
			return nil
		},
		Key: 'n',
	}
	copyPathItem := &types.MenuItem{
		Label: self.c.Tr.CopyFilePath,
		OnPress: func() error {
			if err := self.c.OS().CopyToClipboard(node.Path); err != nil {
				return self.c.Error(err)
			}
			self.c.Toast(self.c.Tr.FilePathCopiedToast)
			return nil
		},
		Key: 'p',
	}
	copyFileDiffItem := &types.MenuItem{
		Label:   self.c.Tr.CopySelectedDiff,
		Tooltip: self.c.Tr.CopyFileDiffTooltip,
		OnPress: func() error {
			path := self.context().GetSelectedPath()
			hasStaged := self.hasPathStagedChanges(node)
			diff, err := self.c.Git().Diff.GetPathDiff(path, hasStaged)
			if err != nil {
				return self.c.Error(err)
			}
			if err := self.c.OS().CopyToClipboard(diff); err != nil {