Skip to content

Commit

Permalink
add target clipboard argument, fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
theimpostor committed Nov 23, 2024
1 parent d8bb64e commit c8e1e2f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ Available Commands:
version Outputs version information
Flags:
-d, --device string select device (default "/dev/tty")
-h, --help help for osc
-l, --log string write logs to file
-t, --timeout float tty read timeout in seconds (default 5)
-v, --verbose verbose logging
-c, --clipboard string target clipboard, can be empty or one or more of c, p, q, s, or 0-7 (default "c")
-d, --device string use specific tty device
-h, --help help for osc
-l, --log string write logs to file
-t, --timeout float tty read timeout in seconds (default 5)
-v, --verbose verbose logging
Use "osc [command] --help" for more information about a command.
```
Expand Down
57 changes: 38 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
Expand All @@ -22,26 +23,28 @@ import (
)

const (
ESC = '\x1b'
BEL = '\a'
BS = '\\'
OSC = string(ESC) + "]52;"
DCS_OPEN = string(ESC) + "P"
DCS_CLOSE = string(ESC) + string(BS)
ESC = '\x1b'
BEL = '\a'
BS = '\\'
OSC = string(ESC) + "]52;"
DCS_OPEN = string(ESC) + "P"
DCS_CLOSE = string(ESC) + string(BS)
CLIPBOARD_REGEX = `^[cpqs0-7]*$`
)

var (
oscOpen string = OSC + "c;"
oscClose string = string(ESC) + string(BS)
isScreen bool
isTmux bool
isZellij bool
verboseFlag bool
logfileFlag string
deviceFlag string
timeoutFlag float64
debugLog *log.Logger
errorLog *log.Logger
oscOpen string
oscClose string
isScreen bool
isTmux bool
isZellij bool
verboseFlag bool
logfileFlag string
deviceFlag string
clipboardFlag string
timeoutFlag float64
debugLog *log.Logger
errorLog *log.Logger
)

type debugWriter struct {
Expand Down Expand Up @@ -130,6 +133,9 @@ func identifyTerm() error {
}
}

oscOpen = OSC + clipboardFlag + ";"
oscClose = string(ESC) + string(BS)

if isScreen {
debugLog.Println("Setting screen dcs passthrough")
oscOpen = DCS_OPEN + oscOpen
Expand Down Expand Up @@ -426,7 +432,12 @@ var copyCmd = &cobra.Command{
osc copy [file1 [...fileN]]
With no arguments, will read from stdin.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if matched, err := regexp.MatchString(CLIPBOARD_REGEX, clipboardFlag); err != nil {
return fmt.Errorf("Invalid clipboard flag: %w", err)
} else if !matched {
return fmt.Errorf("Invalid clipboard flag: %s", clipboardFlag)
}
rc := func() int {
if logfile, err := initLogging(); err != nil {
errorLog.Println(err)
Expand All @@ -448,6 +459,7 @@ With no arguments, will read from stdin.`,
return 0
}()
os.Exit(rc)
return nil
},
}

Expand All @@ -458,7 +470,12 @@ var pasteCmd = &cobra.Command{
osc paste`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if matched, err := regexp.MatchString(CLIPBOARD_REGEX, clipboardFlag); err != nil {
return fmt.Errorf("Invalid clipboard flag: %w", err)
} else if !matched {
return fmt.Errorf("Invalid clipboard flag: %s", clipboardFlag)
}
rc := func() int {
if logfile, err := initLogging(); err != nil {
errorLog.Println(err)
Expand All @@ -480,6 +497,7 @@ osc paste`,
return 0
}()
os.Exit(rc)
return nil
},
}

Expand Down Expand Up @@ -520,6 +538,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&logfileFlag, "log", "l", "", "write logs to file")
rootCmd.PersistentFlags().StringVarP(&deviceFlag, "device", "d", "", "use specific tty device")
rootCmd.PersistentFlags().Float64VarP(&timeoutFlag, "timeout", "t", 5, "tty read timeout in seconds")
rootCmd.PersistentFlags().StringVarP(&clipboardFlag, "clipboard", "c", "c", "target clipboard, can be empty or one or more of c, p, q, s, or 0-7")

rootCmd.AddCommand(copyCmd)
rootCmd.AddCommand(pasteCmd)
Expand Down

0 comments on commit c8e1e2f

Please sign in to comment.