From ae783daf8062397021ee5fa4a480b972bd7ec569 Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Sun, 25 May 2025 00:06:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=20shell=20=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E9=BB=98=E8=AE=A4=20shell=20=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E6=80=A7=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- terminal/terminal_unix.go | 52 ++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/terminal/terminal_unix.go b/terminal/terminal_unix.go index 3b20a6d..cc69b4a 100644 --- a/terminal/terminal_unix.go +++ b/terminal/terminal_unix.go @@ -6,28 +6,62 @@ import ( "fmt" "os" "os/exec" + "strings" "syscall" "github.com/creack/pty" ) func newTerminalImpl() (*terminalImpl, error) { - // 查找可用 shell - defaultShells := []string{"zsh", "bash", "sh"} - shell := "" - for _, s := range defaultShells { - if _, err := exec.LookPath(s); err == nil { - shell = s - break + // 优先获取用户默认 shell + shell := os.Getenv("SHELL") + if shell == "" { + // 如果 SHELL 环境变量为空,尝试从 /etc/passwd 获取 + user, err := os.UserHomeDir() // 当前用户 + if err == nil { + passwd, err := os.ReadFile("/etc/passwd") + if err == nil { + for _, line := range strings.Split(string(passwd), "\n") { + if strings.Contains(line, user) { + parts := strings.Split(line, ":") + if len(parts) >= 7 && parts[6] != "" { + shell = parts[6] + break + } + } + } + } } } + + // 验证 shell 是否可用 + if shell != "" { + if _, err := exec.LookPath(shell); err != nil { + shell = "" // 默认 shell 不可用,清空以进入回退逻辑 + } + } + + // 回退到默认 shell 列表 + defaultShells := []string{"zsh", "bash", "sh"} + if shell == "" { + for _, s := range defaultShells { + if _, err := exec.LookPath(s); err == nil { + shell = s + break + } + } + } + if shell == "" { return nil, fmt.Errorf("no supported shell found") } - // 创建进程 cmd := exec.Command(shell) - cmd.Env = append(cmd.Env, "TERM=xterm-256color") + cmd.Env = append(os.Environ(), // 继承系统环境变量 + "TERM=xterm-256color", + "LANG=C.UTF-8", + "LC_ALL=C.UTF-8", + ) tty, err := pty.Start(cmd) if err != nil { return nil, fmt.Errorf("failed to start pty: %v", err)