Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executable File Existence Check #5963

Open
frostbyte-ninja opened this issue Aug 14, 2024 · 3 comments
Open

Executable File Existence Check #5963

frostbyte-ninja opened this issue Aug 14, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@frostbyte-ninja
Copy link

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.

@frostbyte-ninja frostbyte-ninja added the enhancement New feature or request label Aug 14, 2024
@kenchou
Copy link
Contributor

kenchou commented Aug 21, 2024

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
Copy link

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
Copy link
Owner

wez commented Sep 15, 2024

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",
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants