diff options
| author | Saikari <[email protected]> | 2026-02-01 10:38:43 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-02-01 10:38:43 +0300 |
| commit | b4b656b1f6ddbe8003ec3a872b6f8d6e3ab0c42c (patch) | |
| tree | 1c3a5d02d41a7a9c7ff6903cd2bee796f445f0fa /xmake/core/base/tty.lua | |
| parent | b56818e7a2f96f1750545deda8a0e7bae53baf68 (diff) | |
refactor: improve process retrieval for Windows and Linux in tty module
Diffstat (limited to 'xmake/core/base/tty.lua')
| -rw-r--r-- | xmake/core/base/tty.lua | 99 |
1 files changed, 62 insertions, 37 deletions
diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 8d506336d..c3681cdd3 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -235,57 +235,68 @@ function tty.flush() return tty end --- find the shell from the parent process (linux) -function tty._find_shell_from_parent() +function tty._find_shell_from_parent_on_windows() local shell - if os.host() == "windows" then - local winos = require("base/winos") - if winos.processes then - local processes = winos.processes() - if processes then - local pid = os.getpid() - local processes_map = {} - for _, process in ipairs(processes) do - processes_map[process.pid] = process + local winos = require("base/winos") + if winos.processes then + local processes = winos.processes() + if processes then + local pid = os.getpid() + local processes_map = {} + for _, process in ipairs(processes) do + processes_map[process.pid] = process + end + local count = 0 + while pid and pid ~= 0 and count < 10 do + count = count + 1 + local process = processes_map[pid] + if not process then + break end - local count = 0 - while pid and pid ~= 0 and count < 10 do - count = count + 1 - local process = processes_map[pid] - if not process then - break + local name = process.name + if name then + name = name:lower() + if name:sub(-4) == ".exe" then + name = name:sub(1, #name - 4) end - local name = process.name - if name then - name = name:lower() - if name:sub(-4) == ".exe" then - name = name:sub(1, #name - 4) - end - for _, shellname in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "powershell", "cmd", "sh"}) do - if name == shellname then - shell = shellname - break - end + for _, shellname in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "powershell", "cmd", "sh"}) do + if name == shellname then + shell = shellname + break end end - if shell then - break - end - pid = process.ppid end + if shell then + break + end + pid = process.parent_pid or process.ppid -- for backward compatibility end end end - if shell then - return shell - end - if os.host() ~= "linux" or not os.isfile("/proc/self/stat") then - return + if not shell then + local subhost = xmake._SUBHOST + if subhost == "windows" then + if os.getenv("PROMPT") then + shell = "cmd" + else + local ok, result = os.iorun("pwsh -v") + if ok then + shell = "pwsh" + else + shell = "powershell" + end + end + end end + return shell +end + +function tty._find_shell_from_parent_on_linux() local pid = os.getpid() local count = 0 + local shell while pid ~= 0 and count < 4 do count = count + 1 local shell_name = nil @@ -330,6 +341,20 @@ function tty._find_shell_from_parent() return shell end +-- find the shell from the parent process +function tty._find_shell_from_parent() + + -- for windows + if os.host() == "windows" then + return tty._find_shell_from_parent_on_windows() + end + + -- for linux + if os.host() == "linux" and os.isfile("/proc/self/stat") then + return tty._find_shell_from_parent_on_linux() + end +end + -- get shell name function tty.shell() local shell = tty._SHELL |
