diff options
| author | theimpostor <sahirhoda@gmail.com> | 2022-02-20 09:56:06 -0600 |
|---|---|---|
| committer | theimpostor <sahirhoda@gmail.com> | 2022-02-20 09:56:06 -0600 |
| commit | 2b1414bee0e5e9c18705adc459e36491c08278b0 (patch) | |
| tree | a914747f555764feb4853e380ea790f17e9f0e6d | |
| parent | ee8a1434116c9c1ba8893f4a64930b09f69c8eb0 (diff) | |
initial implementation
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | go.mod | 3 | ||||
| -rw-r--r-- | main.go | 58 |
3 files changed, 63 insertions, 0 deletions
@@ -13,3 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +osc52 @@ -0,0 +1,3 @@ +module github.com/theimpostor/osc52 + +go 1.17 @@ -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") +} |
