0

I am trying to set up a pipe from a running process in tmux,
in order to handle its output line by line.

I have had a look at this guide to pipe the output of a tmux session to stdout
and this article about (named) pipes in go.

I have been trying around with that for quite a while now,
but still didn't get any noteworthy results.

I would really appreciate any thoughts on how to set that pipe up,
Ideally in a way so I can range over it linewise.

Thanks a lot

Hendrik Evert
  • 340
  • 2
  • 19

1 Answers1

1

Here is the solution which I found here (thank you Malcolm)

func Readln(r *bufio.Reader) (string, error) {
    var (
        isPrefix = true
        err      error
        line, ln []byte
    )
    for isPrefix && err == nil {
        line, isPrefix, err = r.ReadLine()
        ln = append(ln, line...)
    }
    return string(ln), err
}

func handle(s string) {
    //Do something with your string
}

func main() {
    c := exec.Command("sh", "./tmuxpipe.sh")
    err := c.Run()
    if err != nil {
        log.Fatal(err)
    }

    f, err := os.Open("/tmp/tmuxpipe")
    if err != nil {
        fmt.Printf("error opening file: %v\n", err)
        os.Exit(1)
    }
    r := bufio.NewReader(f)
    s, e := Readln(r)
    for e == nil {
        handle(s)
        log.Println(s)
        s, e = Readln(r)
    }
}

here is the tmuxpipe.sh:

mkfifo /tmp/tmuxpipe
tmux pipe-pane -o -t tmuxSession 'cat >> /tmp/tmuxpipe'

The reason I did not just use exec.Command() there, is because for some reason beyond my comprehension this:

c := exec.Command("tmux", "pipe-pane", "-o", "-t", "tmuxSession", 'cat >> /tmp/tmuxpipe'") 
err := c.Run()
handleError(err)

did not work (for me). There was no error occuring, but the output of the tmux session wasn't displayd either.

I hope this helps anybody

Hendrik Evert
  • 340
  • 2
  • 19