I was experimenting with creack/pty while learning about pseudo terminals.
I spawned grep and hooked it up with a pseudo terminal device as follows:
package main
import (
"fmt"
"log"
"os/exec"
"github.com/creack/pty"
)
func main() {
// Command to run
cmd := exec.Command("grep", "--color=always", "-E", "pattern") // replace "pattern" with your regex
// Start the command with a pseudo-terminal
ptmx, err := pty.Start(cmd)
if err != nil {
log.Fatal(err)
}
defer func() { _ = ptmx.Close() }() // close pty when done
// Input text to grep
input := "this is a test line\npattern matched here\nanother line\n\004"
// Write input to the pty
_, err = ptmx.Write([]byte(input))
if err != nil {
log.Fatal(err)
}
// Read the output from grep
buf := make([]byte, 1024)
n, err := ptmx.Read(buf)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", string(buf[:n]))
}
However, I could not find the ASCII sequences for highlighting in the output. I even used debugger to see if anything was wrong with my terminal. But no, even the buf buffer does not contain the ASCII sequences.
Isn't grep supposed to output highlighting ASCII sequences when --color=always is used?
What might I be missing here?
grepstill detecting terminal capabilities). Waiting a bit right before reading, e.g. usingtime.Sleep(10 * time.Millisecond)orerr = cmd.Wait()both seem to work.