Skip to content

Commit

Permalink
get parent pid from tmux client
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jan 13, 2024
1 parent 7fb6299 commit ca0b2b3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/stretchr/testify v1.8.4
github.com/trzsz/go-arg v1.5.3
github.com/trzsz/promptui v0.10.5
golang.org/x/sys v0.15.0
golang.org/x/term v0.15.0
golang.org/x/sys v0.16.0
golang.org/x/term v0.16.0
golang.org/x/text v0.14.0
)

Expand All @@ -24,6 +24,6 @@ require (
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/image v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ github.com/trzsz/go-arg v1.5.3/go.mod h1:IC6Z/FiVH7uYvcbp1/gJhDYCFPS/GkL0APYakVv
github.com/trzsz/promptui v0.10.5 h1:tlzJkx+JOeE0sqKWmqgaoToZiYqj5G1Mz+QDV97VFu8=
github.com/trzsz/promptui v0.10.5/go.mod h1:GMZtu6ZTzU73CBFkzGtmB4wnTROIAbv4GFA74fV8V8g=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
23 changes: 22 additions & 1 deletion trzsz/utils_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import (
"os"
"os/exec"
"strconv"
"strings"
"sync"

"golang.org/x/sys/unix"
)

func getParentWindowID() any {
pid := os.Getppid()
pid := getParentPid()
for i := 0; i < 1000; i++ {
kinfo, err := unix.SysctlKinfoProc("kern.proc.pid", pid)
if err != nil {
Expand All @@ -59,6 +60,26 @@ func getParentWindowID() any {
return 0
}

func getParentPid() int {
if _, tmux := os.LookupEnv("TMUX"); !tmux {
return os.Getppid()
}

cmd := exec.Command("tmux", "display-message", "-p", "#{client_pid}")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
return os.Getppid()
}

pid, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
return os.Getppid()
}

return pid
}

var (
warpOnce sync.Once
warpTerminal bool
Expand Down

0 comments on commit ca0b2b3

Please sign in to comment.