Skip to content

Commit

Permalink
feat: use go-osc52/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 1, 2023
1 parent 0eef7b7 commit f8efcaa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ Think of this as a tool like `xclip` or `pbcopy` but also works over SSH.
## Example

```sh
# Copy text to clipboard
shcopy "Hello World"

# Copy text to primary clipboard (X11)
shcopy -p "Hello World"

# Copy command output to clipboard
echo "Hello World" | shcopy
echo -n "Hello World" | shcopy

# Copy file content to clipboard
shcopy < file.txt

# Copy from stdin until EOF
shcopy # Ctrl+D to finish
# Ctrl+D to finish
shcopy

# Need help?
shcopy --help
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
// replace github.com/aymanbagabas/go-osc52 => ../go-osc52

require (
github.com/aymanbagabas/go-osc52 v1.2.2
github.com/aymanbagabas/go-osc52/v2 v2.0.0
github.com/muesli/mango v0.2.0
github.com/muesli/mango-pflag v0.1.0
github.com/muesli/roff v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aymanbagabas/go-osc52 v1.2.2 h1:NT7wkhEhPTcKnBCdPi9djmyy9L3JOL4+3SsfJyqptCo=
github.com/aymanbagabas/go-osc52 v1.2.2/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/aymanbagabas/go-osc52/v2 v2.0.0 h1:dQFJfwsEWDXyXqui4j24o4Zh3kAC0l2BuzgN4/0GtEc=
github.com/aymanbagabas/go-osc52/v2 v2.0.0/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/muesli/mango v0.2.0 h1:iNNc0c5VLQ6fsMgAqGQofByNUBH2Q2nEbD6TaI+5yyQ=
github.com/muesli/mango v0.2.0/go.mod h1:5XFpbC8jY5UUv89YQciiXNlbi+iJgt29VDC5xbzrLL4=
github.com/muesli/mango-pflag v0.1.0 h1:UADqbYgpUyRoBja3g6LUL+3LErjpsOwaC9ywvBWe7Sg=
Expand Down
61 changes: 35 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"strings"

"github.com/aymanbagabas/go-osc52"
"github.com/aymanbagabas/go-osc52/v2"
"github.com/muesli/mango"
mpflag "github.com/muesli/mango-pflag"
"github.com/muesli/roff"
Expand All @@ -20,8 +20,10 @@ var (
Version = "unknown"
CommitSHA = "build from source"

term = pflag.StringP("term", "t", os.Getenv("TERM"), "Terminal type: screen, tmux, etc.")
clear = pflag.BoolP("clear", "c", false, "Clear the clipboard.")
term = pflag.StringP("term", "t", "", "Terminal type: (default), tmux, screen.\n"+
"Note: starting with tmux 3.3a, tmux users won't need to set this option.\n"+
"Refer to the man page for more information.")
clear = pflag.BoolP("clear", "c", false, "Clear the clipboard and exit.")
primary = pflag.BoolP("primary", "p", false, "Use the primary clipboard instead system clipboard.")
version = pflag.BoolP("version", "v", false, "Print version and exit.")
help = pflag.BoolP("help", "h", false, "Print help and exit.")
Expand Down Expand Up @@ -56,23 +58,24 @@ func main() {

if *version {
fmt.Printf("%s version %s (%s)", ProjectName, Version, CommitSHA)
os.Exit(0)
return
}

if *help {
usage(false)
os.Exit(0)
return
}

if *man {
manPage := mango.NewManPage(1, ProjectName, "Copy text to the system clipboard from any supported terminal using ANSI OSC 52 sequence.").
WithLongDescription(ProjectName+" a utility that copies text to your clipboard from anywhere using ANSI OSC52 sequence.").
WithSection("Copyright", "(C) 2022 Ayman Bagabas.\n"+
WithSection("Bugs", "Report bugs to https://github.com/aymanbagabas/shcopy/issues").
WithSection("Copyright", "(C) 2023 Ayman Bagabas.\n"+
"Released under MIT license.")

pflag.VisitAll(mpflag.PFlagVisitor(manPage))
fmt.Println(manPage.Build(roff.NewDocument()))
os.Exit(0)
return
}

var str string
Expand All @@ -90,7 +93,7 @@ func main() {
}
_, err = b.WriteRune(r)
if err != nil {
fmt.Printf("Failed to write rune: %v", err)
fmt.Fprintf(os.Stderr, "Failed to write rune: %v", err)
os.Exit(1)
}
}
Expand All @@ -101,33 +104,39 @@ func main() {
str = strings.Join(args, " ")
}

clip := osc52.SystemClipboard
// the sequence string to be sent to the terminal
seq := osc52.New(str)

if *primary {
clip = osc52.PrimaryClipboard
}
if *debug {
log.Printf("Clipboard: %v", clip)
seq = seq.Primary()
}
term := strings.ToLower(*term)

if *debug {
log.Printf("Input: %q", str)
// Detect `screen` terminal type
if term := os.Getenv("TERM"); term != "" {
if strings.HasPrefix(term, "screen") {
seq = seq.Screen()
}
}
if strings.Contains(term, "kitty") {
// Flush the keyboard before copying, this is required for
// Kitty < 0.22.0.
clr := osc52.Clear(term, clip)
if *debug {
log.Printf("Clear: %q", clr)

if *term != "" {
switch strings.ToLower(*term) {
case "screen":
seq = seq.Screen()
case "tmux":
seq = seq.Tmux()
default:
seq = seq.Mode(osc52.DefaultMode)
}
os.Stderr.WriteString(clr)
}
// the sequence string to be sent to the terminal
seq := osc52.Sequence(str, term, clip)

if *clear {
seq = seq.Clear()
}

if *debug {
log.Printf("Sequence: %q", seq)
}

// send the sequence to the terminal
os.Stderr.WriteString(seq)
_, _ = seq.WriteTo(os.Stderr)
}

0 comments on commit f8efcaa

Please sign in to comment.