Skip to content

Instantly share code, notes, and snippets.

@jasisk
Created October 24, 2018 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasisk/be34ae93b74a3d3e8f0a4daac1237966 to your computer and use it in GitHub Desktop.
Save jasisk/be34ae93b74a3d3e8f0a4daac1237966 to your computer and use it in GitHub Desktop.
/*
* $ 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