From a8f6b04ff3d477768d6ab06778bc252cf779a930 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 21 Mar 2025 08:25:12 +0100 Subject: [PATCH] Add "Absolute path" item to the file view's copy menu Rename the existing item to "Relative path". --- .../controllers/commits_files_controller.go | 20 ++++++++++++++--- pkg/gui/controllers/files_controller.go | 20 ++++++++++++++--- pkg/i18n/english.go | 6 +++-- .../tests/diff/copy_to_clipboard.go | 17 +++++++++++++- pkg/integration/tests/file/copy_menu.go | 22 +++++++++++++++++-- 5 files changed, 74 insertions(+), 11 deletions(-) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 68a826eb8..77037513e 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -2,6 +2,7 @@ package controllers import ( "errors" + "path/filepath" "strings" "github.com/jesseduffield/gocui" @@ -228,8 +229,8 @@ func (self *CommitFilesController) openCopyMenu() error { DisabledReason: self.require(self.singleItemSelected())(), Key: 'n', } - copyPathItem := &types.MenuItem{ - Label: self.c.Tr.CopyFilePath, + copyRelativePathItem := &types.MenuItem{ + Label: self.c.Tr.CopyRelativeFilePath, OnPress: func() error { if err := self.c.OS().CopyToClipboard(node.GetPath()); err != nil { return err @@ -240,6 +241,18 @@ func (self *CommitFilesController) openCopyMenu() error { DisabledReason: self.require(self.singleItemSelected())(), Key: 'p', } + copyAbsolutePathItem := &types.MenuItem{ + Label: self.c.Tr.CopyAbsoluteFilePath, + OnPress: func() error { + if err := self.c.OS().CopyToClipboard(filepath.Join(self.c.Git().RepoPaths.RepoPath(), node.GetPath())); err != nil { + return err + } + self.c.Toast(self.c.Tr.FilePathCopiedToast) + return nil + }, + DisabledReason: self.require(self.singleItemSelected())(), + Key: 'P', + } copyFileDiffItem := &types.MenuItem{ Label: self.c.Tr.CopySelectedDiff, OnPress: func() error { @@ -282,7 +295,8 @@ func (self *CommitFilesController) openCopyMenu() error { Title: self.c.Tr.CopyToClipboardMenu, Items: []*types.MenuItem{ copyNameItem, - copyPathItem, + copyRelativePathItem, + copyAbsolutePathItem, copyFileDiffItem, copyAllDiff, copyFileContentItem, diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 3c25543dd..5f4a3bae0 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -3,6 +3,7 @@ package controllers import ( "errors" "fmt" + "path/filepath" "strings" "github.com/jesseduffield/gocui" @@ -976,8 +977,8 @@ func (self *FilesController) openCopyMenu() error { DisabledReason: self.require(self.singleItemSelected())(), Key: 'n', } - copyPathItem := &types.MenuItem{ - Label: self.c.Tr.CopyFilePath, + copyRelativePathItem := &types.MenuItem{ + Label: self.c.Tr.CopyRelativeFilePath, OnPress: func() error { if err := self.c.OS().CopyToClipboard(node.GetPath()); err != nil { return err @@ -988,6 +989,18 @@ func (self *FilesController) openCopyMenu() error { DisabledReason: self.require(self.singleItemSelected())(), Key: 'p', } + copyAbsolutePathItem := &types.MenuItem{ + Label: self.c.Tr.CopyAbsoluteFilePath, + OnPress: func() error { + if err := self.c.OS().CopyToClipboard(filepath.Join(self.c.Git().RepoPaths.RepoPath(), node.GetPath())); err != nil { + return err + } + self.c.Toast(self.c.Tr.FilePathCopiedToast) + return nil + }, + DisabledReason: self.require(self.singleItemSelected())(), + Key: 'P', + } copyFileDiffItem := &types.MenuItem{ Label: self.c.Tr.CopySelectedDiff, Tooltip: self.c.Tr.CopyFileDiffTooltip, @@ -1044,7 +1057,8 @@ func (self *FilesController) openCopyMenu() error { Title: self.c.Tr.CopyToClipboardMenu, Items: []*types.MenuItem{ copyNameItem, - copyPathItem, + copyRelativePathItem, + copyAbsolutePathItem, copyFileDiffItem, copyAllDiff, }, diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index e4708becc..73571d7ee 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -76,7 +76,8 @@ type TranslationSet struct { FileFilter string CopyToClipboardMenu string CopyFileName string - CopyFilePath string + CopyRelativeFilePath string + CopyAbsoluteFilePath string CopyFileDiffTooltip string CopySelectedDiff string CopyAllFilesDiff string @@ -1120,7 +1121,8 @@ func EnglishTranslationSet() *TranslationSet { FileFilter: "Filter files by status", CopyToClipboardMenu: "Copy to clipboard", CopyFileName: "File name", - CopyFilePath: "Path", + CopyRelativeFilePath: "Relative path", + CopyAbsoluteFilePath: "Absolute path", CopyFileDiffTooltip: "If there are staged items, this command considers only them. Otherwise, it considers all the unstaged ones.", CopySelectedDiff: "Diff of selected file", CopyAllFilesDiff: "Diff of all files", diff --git a/pkg/integration/tests/diff/copy_to_clipboard.go b/pkg/integration/tests/diff/copy_to_clipboard.go index a1a6b4a9e..34f2bb95c 100644 --- a/pkg/integration/tests/diff/copy_to_clipboard.go +++ b/pkg/integration/tests/diff/copy_to_clipboard.go @@ -1,6 +1,8 @@ package diff import ( + "os" + "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" ) @@ -63,7 +65,7 @@ var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ Tap(func() { t.ExpectPopup().Menu(). Title(Equals("Copy to clipboard")). - Select(Contains("Path")). + Select(Contains("Relative path")). Confirm(). Tap(func() { t.ExpectToast(Equals("File path copied to clipboard")) @@ -71,6 +73,19 @@ var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ }) }). Press(keys.Files.CopyFileInfoToClipboard). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Copy to clipboard")). + Select(Contains("Absolute path")). + Confirm(). + Tap(func() { + t.ExpectToast(Equals("File path copied to clipboard")) + repoDir, _ := os.Getwd() + // On windows the following path would have backslashes, but we don't run integration tests on windows yet. + expectClipboard(t, Equals(repoDir+"/dir/file1")) + }) + }). + Press(keys.Files.CopyFileInfoToClipboard). Tap(func() { t.ExpectPopup().Menu(). Title(Equals("Copy to clipboard")). diff --git a/pkg/integration/tests/file/copy_menu.go b/pkg/integration/tests/file/copy_menu.go index 9b96f8486..8151cfa10 100644 --- a/pkg/integration/tests/file/copy_menu.go +++ b/pkg/integration/tests/file/copy_menu.go @@ -1,6 +1,8 @@ package file import ( + "os" + "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" ) @@ -103,13 +105,13 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{ expectClipboard(t, Equals("1-unstaged_file")) }) - // Copy file path + // Copy relative file path t.Views().Files(). Press(keys.Files.CopyFileInfoToClipboard). Tap(func() { t.ExpectPopup().Menu(). Title(Equals("Copy to clipboard")). - Select(Contains("Path")). + Select(Contains("Relative path")). Confirm() t.ExpectToast(Equals("File path copied to clipboard")) @@ -117,6 +119,22 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{ expectClipboard(t, Equals("dir/1-unstaged_file")) }) + // Copy absolute file path + t.Views().Files(). + Press(keys.Files.CopyFileInfoToClipboard). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Copy to clipboard")). + Select(Contains("Absolute path")). + Confirm() + + t.ExpectToast(Equals("File path copied to clipboard")) + + repoDir, _ := os.Getwd() + // On windows the following path would have backslashes, but we don't run integration tests on windows yet. + expectClipboard(t, Equals(repoDir+"/dir/1-unstaged_file")) + }) + // Selected path diff on a single (unstaged) file t.Views().Files(). Press(keys.Files.CopyFileInfoToClipboard).