|
| 1 | +// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net> |
| 2 | + |
| 3 | +package tui |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "kitty" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "golang.org/x/sys/unix" |
| 15 | + |
| 16 | + "kitty/tools/config" |
| 17 | + "kitty/tools/tty" |
| 18 | + "kitty/tools/tui/loop" |
| 19 | + "kitty/tools/tui/shell_integration" |
| 20 | + "kitty/tools/utils" |
| 21 | + "kitty/tools/utils/shlex" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = fmt.Print |
| 25 | + |
| 26 | +type KittyOpts struct { |
| 27 | + Shell, Shell_integration string |
| 28 | +} |
| 29 | + |
| 30 | +func read_relevant_kitty_opts(path string) KittyOpts { |
| 31 | + ans := KittyOpts{Shell: kitty.KittyConfigDefaults.Shell, Shell_integration: kitty.KittyConfigDefaults.Shell_integration} |
| 32 | + handle_line := func(key, val string) error { |
| 33 | + switch key { |
| 34 | + case "shell": |
| 35 | + ans.Shell = strings.TrimSpace(val) |
| 36 | + case "shell_integration": |
| 37 | + ans.Shell_integration = strings.TrimSpace(val) |
| 38 | + } |
| 39 | + return nil |
| 40 | + } |
| 41 | + cp := config.ConfigParser{LineHandler: handle_line} |
| 42 | + cp.ParseFiles(path) |
| 43 | + if ans.Shell == "" { |
| 44 | + ans.Shell = kitty.KittyConfigDefaults.Shell |
| 45 | + } |
| 46 | + return ans |
| 47 | +} |
| 48 | + |
| 49 | +func get_effective_ksi_env_var(x string) string { |
| 50 | + parts := strings.Split(strings.TrimSpace(strings.ToLower(x)), " ") |
| 51 | + current := utils.NewSetWithItems(parts...) |
| 52 | + if current.Has("disabled") { |
| 53 | + return "" |
| 54 | + } |
| 55 | + allowed := utils.NewSetWithItems(kitty.AllowedShellIntegrationValues...) |
| 56 | + if !current.IsSubsetOf(allowed) { |
| 57 | + return relevant_kitty_opts().Shell_integration |
| 58 | + } |
| 59 | + return x |
| 60 | +} |
| 61 | + |
| 62 | +var relevant_kitty_opts = utils.Once(func() KittyOpts { |
| 63 | + return read_relevant_kitty_opts(filepath.Join(utils.ConfigDir(), "kitty.conf")) |
| 64 | +}) |
| 65 | + |
| 66 | +func ResolveShell(shell string) []string { |
| 67 | + if shell == "" { |
| 68 | + shell = relevant_kitty_opts().Shell |
| 69 | + if shell == "." { |
| 70 | + s, e := utils.LoginShellForCurrentUser() |
| 71 | + if e != nil { |
| 72 | + shell = "/bin/sh" |
| 73 | + } else { |
| 74 | + shell = s |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + shell_cmd, err := shlex.Split(shell) |
| 79 | + if err != nil { |
| 80 | + shell_cmd = []string{shell} |
| 81 | + } |
| 82 | + exe := utils.FindExe(shell_cmd[0]) |
| 83 | + if unix.Access(exe, unix.X_OK) != nil { |
| 84 | + shell_cmd = []string{"/bin/sh"} |
| 85 | + } |
| 86 | + return shell_cmd |
| 87 | +} |
| 88 | + |
| 89 | +func ResolveShellIntegration(shell_integration string) string { |
| 90 | + if shell_integration == "" { |
| 91 | + shell_integration = relevant_kitty_opts().Shell_integration |
| 92 | + } |
| 93 | + return get_effective_ksi_env_var(shell_integration) |
| 94 | +} |
| 95 | + |
| 96 | +func get_shell_name(argv0 string) (ans string) { |
| 97 | + ans = filepath.Base(argv0) |
| 98 | + if strings.HasSuffix(strings.ToLower(ans), ".exe") { |
| 99 | + ans = ans[:len(ans)-4] |
| 100 | + } |
| 101 | + if strings.HasPrefix(ans, "-") { |
| 102 | + ans = ans[1:] |
| 103 | + } |
| 104 | + return |
| 105 | +} |
| 106 | + |
| 107 | +func rc_modification_allowed(ksi string) bool { |
| 108 | + for _, x := range strings.Split(ksi, " ") { |
| 109 | + switch x { |
| 110 | + case "disabled", "no-rc": |
| 111 | + return false |
| 112 | + } |
| 113 | + } |
| 114 | + return ksi != "" |
| 115 | +} |
| 116 | + |
| 117 | +func RunShell(shell_cmd []string, shell_integration_env_var_val string) (err error) { |
| 118 | + shell_name := get_shell_name(shell_cmd[0]) |
| 119 | + var shell_env map[string]string |
| 120 | + if rc_modification_allowed(shell_integration_env_var_val) && shell_integration.IsSupportedShell(shell_name) { |
| 121 | + oenv := os.Environ() |
| 122 | + env := make(map[string]string, len(oenv)) |
| 123 | + for _, x := range oenv { |
| 124 | + if k, v, found := strings.Cut(x, "="); found { |
| 125 | + env[k] = v |
| 126 | + } |
| 127 | + } |
| 128 | + argv, env, err := shell_integration.Setup(shell_name, shell_cmd, env) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + shell_cmd = argv |
| 133 | + shell_env = env |
| 134 | + } |
| 135 | + exe := shell_cmd[0] |
| 136 | + if runtime.GOOS == "darwin" { |
| 137 | + // ensure shell runs in login mode |
| 138 | + shell_cmd[0] = "-" + filepath.Base(shell_cmd[0]) |
| 139 | + } |
| 140 | + var env []string |
| 141 | + if shell_env != nil { |
| 142 | + env := make([]string, 0, len(shell_env)) |
| 143 | + for k, v := range shell_env { |
| 144 | + env = append(env, fmt.Sprintf("%s=%s", k, v)) |
| 145 | + } |
| 146 | + } else { |
| 147 | + env = os.Environ() |
| 148 | + } |
| 149 | + return unix.Exec(utils.FindExe(exe), shell_cmd, env) |
| 150 | +} |
| 151 | + |
| 152 | +func RunCommandRestoringTerminalToSaneStateAfter(cmd []string) { |
| 153 | + exe := utils.FindExe(cmd[0]) |
| 154 | + c := exec.Command(exe, cmd[1:]...) |
| 155 | + c.Stdout = os.Stdout |
| 156 | + c.Stdin = os.Stdin |
| 157 | + c.Stderr = os.Stderr |
| 158 | + term, err := tty.OpenControllingTerm() |
| 159 | + if err == nil { |
| 160 | + var state_before unix.Termios |
| 161 | + if term.Tcgetattr(&state_before) == nil { |
| 162 | + term.WriteString(loop.SAVE_PRIVATE_MODE_VALUES) |
| 163 | + defer func() { |
| 164 | + term.WriteString(strings.Join([]string{ |
| 165 | + loop.RESTORE_PRIVATE_MODE_VALUES, |
| 166 | + "\x1b[=u", // reset kitty keyboard protocol to legacy |
| 167 | + "\x1b[1 q", // blinking block cursor |
| 168 | + loop.DECTCEM.EscapeCodeToSet(), // cursor visible |
| 169 | + "\x1b]112\a", // reset cursor color |
| 170 | + }, "")) |
| 171 | + term.Tcsetattr(tty.TCSANOW, &state_before) |
| 172 | + term.Close() |
| 173 | + }() |
| 174 | + } else { |
| 175 | + defer term.Close() |
| 176 | + } |
| 177 | + } |
| 178 | + err = c.Run() |
| 179 | + if err != nil { |
| 180 | + fmt.Fprintln(os.Stderr, cmd[0], "failed with error:", err) |
| 181 | + } |
| 182 | +} |
0 commit comments