Created
October 24, 2018 16:26
-
-
Save jasisk/be34ae93b74a3d3e8f0a4daac1237966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* $ echo "these are words" | confirm-pipe | xxd | |
* these are words | |
* | |
* type c to confirm, anything else to quit | |
* 00000000: 7468 6573 6520 6172 6520 776f 7264 730a these are words. | |
* | |
*/ | |
package main | |
import ( | |
"bytes" | |
"log" | |
"fmt" | |
"io" | |
"os" | |
"github.com/pkg/term" | |
) | |
func main() { | |
var buffer bytes.Buffer | |
rc := 0 | |
defer func () { os.Exit(rc) }() | |
fd, err := os.OpenFile("/dev/tty", os.O_RDWR, 0644) | |
defer fd.Close() | |
w := io.MultiWriter(&buffer, fd) | |
io.Copy(w, os.Stdin) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Fprintf(fd, "\ntype c to confirm, anything else to quit\033[?25l") | |
t, _ := term.Open("/dev/tty") | |
defer fmt.Fprintf(fd, "\n\033[?25h") | |
defer t.Close() | |
defer t.Restore() | |
term.RawMode(t) | |
bytes := make([]byte, 1) | |
num, _ := t.Read(bytes) | |
if num == 1 { | |
if bytes[0] == 99 { | |
fmt.Print(buffer.String()) | |
return | |
} | |
} | |
rc = 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment