diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-06-26 22:19:03 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-06-26 22:20:54 +0200 |
commit | 68edfa20b43848f337da6eeb9e0098c88205550a (patch) | |
tree | 9301ecf1c6a99af17be98d804e07b5a1f394b6fb | |
parent | bfe9f233accc31c8ffeecae34ddd9b24c0545d48 (diff) |
Add function os.PasteFromClipboard
And a user config to override it with a custom command.
-rw-r--r-- | docs/Config.md | 14 | ||||
-rw-r--r-- | pkg/commands/oscommands/os.go | 17 | ||||
-rw-r--r-- | pkg/config/user_config.go | 6 | ||||
-rw-r--r-- | schema/config.json | 6 |
4 files changed, 39 insertions, 4 deletions
diff --git a/docs/Config.md b/docs/Config.md index 67d73ebd4..88df40621 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -415,9 +415,13 @@ os: openLinkCommand: "" # CopyToClipboardCmd is the command for copying to clipboard. - # See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-clipboard + # See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-and-pasting-from-clipboard copyToClipboardCmd: "" + # ReadFromClipboardCmd is the command for reading the clipboard. + # See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-and-pasting-from-clipboard + readFromClipboardCmd: "" + # If true, don't display introductory popups upon opening Lazygit. disableStartupPopups: false @@ -620,7 +624,7 @@ os: open: 'open {{filename}}' ``` -## Custom Command for Copying to Clipboard +## Custom Command for Copying to and Pasting from Clipboard ```yaml os: copyToClipboardCmd: '' @@ -633,6 +637,12 @@ os: copyToClipboardCmd: printf "\033]52;c;$(printf {{text}} | base64)\a" > /dev/tty ``` +A custom command for reading from the clipboard can be set using +```yaml +os: + readFromClipboardCmd: '' +``` +It is used, for example, when pasting a commit message into the commit message panel. The command is supposed to output the clipboard content to stdout. ## Configuring File Editing diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 0a6bf7397..7771dffba 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -302,6 +302,23 @@ func (c *OSCommand) CopyToClipboard(str string) error { return clipboard.WriteAll(str) } +func (c *OSCommand) PasteFromClipboard() (string, error) { + var s string + var err error + if c.UserConfig.OS.CopyToClipboardCmd != "" { + cmdStr := c.UserConfig.OS.ReadFromClipboardCmd + s, err = c.Cmd.NewShell(cmdStr).RunWithOutput() + } else { + s, err = clipboard.ReadAll() + } + + if err != nil { + return "", err + } + + return strings.ReplaceAll(s, "\r\n", "\n"), nil +} + func (c *OSCommand) RemoveFile(path string) error { msg := utils.ResolvePlaceholderString( c.Tr.Log.RemoveFile, diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 7ab567fbe..26d10f73a 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -565,8 +565,12 @@ type OSConfig struct { OpenLinkCommand string `yaml:"openLinkCommand,omitempty"` // CopyToClipboardCmd is the command for copying to clipboard. - // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-clipboard + // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-and-pasting-from-clipboard CopyToClipboardCmd string `yaml:"copyToClipboardCmd,omitempty"` + + // ReadFromClipboardCmd is the command for reading the clipboard. + // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-and-pasting-from-clipboard + ReadFromClipboardCmd string `yaml:"readFromClipboardCmd,omitempty"` } type CustomCommandAfterHook struct { diff --git a/schema/config.json b/schema/config.json index 802069bed..daaf4ada6 100644 --- a/schema/config.json +++ b/schema/config.json @@ -796,7 +796,11 @@ }, "copyToClipboardCmd": { "type": "string", - "description": "CopyToClipboardCmd is the command for copying to clipboard.\nSee https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-clipboard" + "description": "CopyToClipboardCmd is the command for copying to clipboard.\nSee https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-and-pasting-from-clipboard" + }, + "readFromClipboardCmd": { + "type": "string", + "description": "ReadFromClipboardCmd is the command for reading the clipboard.\nSee https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-command-for-copying-to-and-pasting-from-clipboard" } }, "additionalProperties": false, |