Debian Codegraph @

src/zellij.go

raw
1 package fzf
2
3 import (
4 "fmt"
5 "os/exec"
6 )
7
8 const zellijBorderLabelEnv = internalEnvPrefix + "ZELLIJ_LABEL_PANE"
9
10 // Errors are ignored. The pane may be gone
11 func setZellijBorderLabel(pane string, label string) {
12 // Strip ANSI sequences fzf would otherwise render itself
13 label, _, _ = extractColor(label, nil, nil)
14 // '--' so that a label starting with a hyphen is not parsed as a flag
15 exec.Command("zellij", "action", "rename-pane", "-p", pane, "--", label).Run()
16 }
17
18 func runZellij(args []string, opts *Options) (int, error) {
19 // Use the native Zellij border by default, consistent with tmux, so that
20 // the pane can be moved and resized with the mouse. Set before
21 // popupArgStr so that it does not inject an fzf border. fzf draws its own
22 // border instead when a border style is explicitly specified.
23 if nativeBorder(opts) {
24 opts.Tmux.border = true
25 }
26 argStr, dir := popupArgStr(args, opts)
27
28 zellijArgs := []string{
29 "run", "--floating", "--close-on-exit", "--block-until-exit",
30 "--cwd", dir,
31 }
32 ownsLabel := false
33 if !opts.Tmux.border {
34 zellijArgs = append(zellijArgs, "--borderless", "true")
35 } else {
36 // Set --border-label as the name of the pane, displayed on the
37 // native border. The label is left to fzf when it draws its own
38 // border with the label on it (border-native with an explicit
39 // --border style). Empty otherwise, to override the default name
40 // (the running command). Passed as a single argument in the
41 // --name=label form; the detached form fails to parse when the
42 // label starts with a hyphen. No escaping is needed beyond
43 // stripping ANSI sequences fzf would otherwise render itself.
44 // --border-label-pos is ignored.
45 label := ""
46 ownsLabel = noBorderSpecified(opts)
47 if ownsLabel {
48 label, _, _ = extractColor(opts.BorderLabel.label, nil, nil)
49 }
50 zellijArgs = append(zellijArgs, "--name="+label)
51 }
52 switch opts.Tmux.position {
53 case posUp:
54 zellijArgs = append(zellijArgs, "-y", "0")
55 case posDown:
56 zellijArgs = append(zellijArgs, "-y", "9999")
57 case posLeft:
58 zellijArgs = append(zellijArgs, "-x", "0")
59 case posRight:
60 zellijArgs = append(zellijArgs, "-x", "9999")
61 case posCenter:
62 // Zellij centers floating panes by default
63 }
64 zellijArgs = append(zellijArgs, "--width", opts.Tmux.width.String())
65 zellijArgs = append(zellijArgs, "--height", opts.Tmux.height.String())
66 zellijArgs = append(zellijArgs, "--")
67
68 return runProxy(argStr, func(temp string, needBash bool) (*exec.Cmd, error) {
69 sh, err := sh(needBash)
70 if err != nil {
71 return nil, err
72 }
73 if ownsLabel {
74 // 'zellij run' cannot set the new pane's environment, so the
75 // command exports it itself. exec avoids a lingering shell
76 zellijArgs = append(zellijArgs, sh, "-c", fmt.Sprintf(`export %s="$ZELLIJ_PANE_ID"; exec %s %s`,
77 zellijBorderLabelEnv, escapeSingleQuote(sh), escapeSingleQuote(temp)))
78 } else {
79 zellijArgs = append(zellijArgs, sh, temp)
80 }
81 return exec.Command("zellij", zellijArgs...), nil
82 }, opts, true)
83 }