wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
17.62k stars 788 forks source link

Executable File Existence Check #5963

Open frostbyte-ninja opened 2 months ago

frostbyte-ninja commented 2 months ago

Hi,

I am encountering the same issue as described here. The suggested solution there does not appear to be optimal. To determine the availability of executables in the path, I implemented the following function:

local function is_executable_in_path(executable)
  local command
  if wezterm.target_triple:find("linux") ~= nil then
    command = "command -v " .. executable
  elseif wezterm.target_triple:find("windows") ~= nil then
    command = "where " .. executable
  else
    return false
  end
  return os.execute(command)
end

While this performs as expected on Linux, on Windows each call results in the spawning of a shell window. This not only disrupts the visual flow but also significantly increases startup time when multiple checks are performed.

My goal is to determine which shells are available on the system to dynamically create launch_menu entries.

I would greatly appreciate a more efficient approach to address this issue.

Thank you.

kenchou commented 2 months ago

If you just want to know which shells are available on the system, you can check /etc/shells for Linux/Mac. There doesn't seem to be a good way to do this on Windows.

prabirshrestha commented 2 months ago

Ran into similar issue where I wanted to use fish if it exists.

I have used homebrew so it install fish at /opt/homebrew/bin/fish which might not be in the path.

So I have added the following to my western config.

local function file_exists(path)
    local f = io.open(path, "r")
    if f~=nil then io.close(f) return true else return false end
end

if file_exists("/opt/homebrew/bin/fish") then
    config.default_prog = { '/opt/homebrew/bin/fish', '-l' }
else
    config.default_prog = { '/bin/bash', '-l' }
end

If I want to add it to launch menu I would also need an if else here.

{ label = "fish", args = {"/opt/homebrew/bin/fish", "-l"}

It almost seems like there should also be an easy way to add /opt/homebrew/bin to the path and have a function to see if there executable is available so one can set the args as just {"fish", "-l"}

wez commented 1 month ago

Another option for this is use wezterm.glob to check if a specific path exists, here I'm in the debug overlay on a system that doesn't have homebrew or fish, but does have zsh at the paths I'm checking:

Debug Overlay
wezterm version: 20240812-215703-30345b36 x86_64-unknown-linux-gnu
Enter lua statements or expressions and hit Enter.
Press ESC or CTRL-D to exit
> wezterm.glob('/opt/homebrew/bin/fish')
[]
> wezterm.glob('/bin/zsh')
[
    "/bin/zsh",
]
>

Glob expressions can include multiple candidates, so you can check for multiple paths at the same time. Here I'm checking for zsh, bash, fish and csh:

> wezterm.glob('/bin/{zsh,bash,fish,csh}')
[
    "/bin/bash",
    "/bin/zsh",
]