Skip to content

Commit

Permalink
feat: add os.copyToClipboardCmd to allow for a custom command
Browse files Browse the repository at this point in the history
Issue #1055
  • Loading branch information
redstreet committed Jul 16, 2023
1 parent 7e9f669 commit 0a7fb14
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ git:
parseEmoji: false
diffContextSize: 3 # how many lines of context are shown around a change in diffs
os:
copyToClipboardCmd: '' # See 'Custom Command for Copying to Clipboard' section
editPreset: '' # see 'Configuring File Editing' section
edit: ''
editAtLine: ''
Expand Down Expand Up @@ -278,6 +279,19 @@ os:
open: 'open {{filename}}'
```
### Custom Command for Copying to Clipboard
```yaml
os:
copyToClipboardCmd: ''
```
Specify an external command to invoke when copying to clipboard is requested. `{{text}` will be replaced by text to be copied. Default is to copy to system clipboard.

If you are working on a terminal that supports OSC52, the following command will let you take advantage of it:
```
printf "\033]52;c;$(printf "{{text}}" | base64)\a"
```


### Configuring File Editing

There are two commands for opening files, `o` for "open" and `e` for "edit". `o`
Expand Down
7 changes: 7 additions & 0 deletions pkg/commands/oscommands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ func (c *OSCommand) CopyToClipboard(str string) error {
escaped := strings.Replace(str, "\n", "\\n", -1)
truncated := utils.TruncateWithEllipsis(escaped, 40)
c.LogCommand(fmt.Sprintf("Copying '%s' to clipboard", truncated), false)
if c.UserConfig.OS.CopyToClipboardCmd != "" {

Check failure on line 270 in pkg/commands/oscommands/os.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 270 in pkg/commands/oscommands/os.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
cmdStr := utils.ResolvePlaceholderString(c.UserConfig.OS.CopyToClipboardCmd, map[string]string{
"text": c.Cmd.Quote(str),
})
return c.Cmd.NewShell(cmdStr).Run()
}

return clipboard.WriteAll(str)
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ type OSConfig struct {
// OpenLinkCommand is the command for opening a link
// Deprecated: use OpenLink instead.
OpenLinkCommand string `yaml:"openLinkCommand,omitempty"`

// CopyToClipboardCmd is the command for copying to clipboard

Check failure on line 350 in pkg/config/user_config.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 350 in pkg/config/user_config.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
CopyToClipboardCmd string `yaml:"copyToClipboardCmd,omitempty"`
}

type CustomCommandAfterHook struct {
Expand Down

0 comments on commit 0a7fb14

Please sign in to comment.