Skip to content
Myr

zellij.go

This file

Path

src/zellij.go

83 lines · 2.9 KiB · Go

Last changed by

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

History of this file

Take it

1package fzf23import (4	"fmt"5	"os/exec"6)78const zellijBorderLabelEnv = internalEnvPrefix + "ZELLIJ_LABEL_PANE"910// Errors are ignored. The pane may be gone11func setZellijBorderLabel(pane string, label string) {12	// Strip ANSI sequences fzf would otherwise render itself13	label, _, _ = extractColor(label, nil, nil)14	// '--' so that a label starting with a hyphen is not parsed as a flag15	exec.Command("zellij", "action", "rename-pane", "-p", pane, "--", label).Run()16}1718func runZellij(args []string, opts *Options) (int, error) {19	// Use the native Zellij border by default, consistent with tmux, so that20	// the pane can be moved and resized with the mouse. Set before21	// popupArgStr so that it does not inject an fzf border. fzf draws its own22	// border instead when a border style is explicitly specified.23	if nativeBorder(opts) {24		opts.Tmux.border = true25	}26	argStr, dir := popupArgStr(args, opts)2728	zellijArgs := []string{29		"run", "--floating", "--close-on-exit", "--block-until-exit",30		"--cwd", dir,31	}32	ownsLabel := false33	if !opts.Tmux.border {34		zellijArgs = append(zellijArgs, "--borderless", "true")35	} else {36		// Set --border-label as the name of the pane, displayed on the37		// native border. The label is left to fzf when it draws its own38		// border with the label on it (border-native with an explicit39		// --border style). Empty otherwise, to override the default name40		// (the running command). Passed as a single argument in the41		// --name=label form; the detached form fails to parse when the42		// label starts with a hyphen. No escaping is needed beyond43		// 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 default63	}64	zellijArgs = append(zellijArgs, "--width", opts.Tmux.width.String())65	zellijArgs = append(zellijArgs, "--height", opts.Tmux.height.String())66	zellijArgs = append(zellijArgs, "--")6768	return runProxy(argStr, func(temp string, needBash bool) (*exec.Cmd, error) {69		sh, err := sh(needBash)70		if err != nil {71			return nil, err72		}73		if ownsLabel {74			// 'zellij run' cannot set the new pane's environment, so the75			// command exports it itself. exec avoids a lingering shell76			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...), nil82	}, opts, true)83}