summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authortheimpostor <sahirhoda@gmail.com>2022-02-20 09:56:06 -0600
committertheimpostor <sahirhoda@gmail.com>2022-02-20 09:56:06 -0600
commit2b1414bee0e5e9c18705adc459e36491c08278b0 (patch)
treea914747f555764feb4853e380ea790f17e9f0e6d /main.go
parentee8a1434116c9c1ba8893f4a64930b09f69c8eb0 (diff)
initial implementation
Diffstat (limited to 'main.go')
-rw-r--r--main.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..938fb84
--- /dev/null
+++ b/main.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+ "encoding/base64"
+ "flag"
+ "fmt"
+ "io"
+ "log"
+ "os"
+)
+
+func encode(fname string, encoder io.WriteCloser) {
+ var f *os.File
+ var err error
+
+ if fname == "-" {
+ f = os.Stdin
+ } else {
+ if f, err = os.Open(fname); err != nil {
+ log.Fatalf("Failed to open file %v: %v", fname, err)
+ } else {
+ defer f.Close()
+ }
+ }
+
+ if _, err = io.Copy(encoder, f); err != nil {
+ log.Fatal(err)
+ }
+}
+
+func main() {
+ var fnames []string
+
+ flag.Usage = func() {
+ fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [file1 [...fileN]]\n", os.Args[0])
+ fmt.Fprintf(flag.CommandLine.Output(), "Copies input to system clipboard using an OSC52 escape sequence.\n")
+ fmt.Fprintf(flag.CommandLine.Output(), "Multiple files will be concatenated.\n")
+ fmt.Fprintf(flag.CommandLine.Output(), "With no file arguments, will read from stdin\n")
+ }
+
+ flag.Parse()
+ if len(flag.Args()) > 0 {
+ fnames = flag.Args()
+ } else {
+ fnames = []string{"-"}
+ }
+
+ // Start OSC52
+ fmt.Print("\033]52;c;")
+ b64 := base64.NewEncoder(base64.StdEncoding, os.Stdout)
+ for _, fname := range fnames {
+ encode(fname, b64)
+ }
+ b64.Close()
+
+ // End OSC52
+ fmt.Print("\a")
+}