sxyazi / yazi

💥 Blazing fast terminal file manager written in Rust, based on async I/O.
https://yazi-rs.github.io
MIT License
17.09k stars 399 forks source link

Check if Path Exists Before cd Command #1871

Closed sohanemon closed 1 month ago

sohanemon commented 1 month ago

yazi --debug output

Yazi
    Version: 0.3.3 (Arch Linux 2024-09-05)
    Debug  : false
    OS     : linux-x86_64 (unix)

Ya
    Version: 0.3.3 (Arch Linux 2024-09-05)

Emulator
    Emulator.via_env: ("xterm-kitty", "tmux")
    Emulator.via_csi: Ok(Kitty)
    Emulator.detect : Kitty

Adapter
    Adapter.matches: Kitty

Desktop
    XDG_SESSION_TYPE           : Some("wayland")
    WAYLAND_DISPLAY            : Some("wayland-1")
    DISPLAY                    : Some(":1")
    SWAYSOCK                   : None
    HYPRLAND_INSTANCE_SIGNATURE: Some("0f594732b063a90d44df8c5d402d658f27471dfe_1730340949_700113242")
    WAYFIRE_SOCKET             : None

SSH
    shared.in_ssh_connection: false

WSL
    WSL: false

Variables
    SHELL              : Some("/usr/bin/zsh")
    EDITOR             : Some("nvim")
    VISUAL             : Some("nvim")
    YAZI_FILE_ONE      : None
    YAZI_CONFIG_HOME   : None

Text Opener
    default: Some(Opener { run: "nvim \"$@\"", block: true, orphan: false, desc: "Neovim", for_: None, spread: true })
    block  : Some(Opener { run: "nvim \"$@\"", block: true, orphan: false, desc: "Neovim", for_: None, spread: true })

Multiplexers
    TMUX               : true
    tmux version       : 3.5
    ZELLIJ_SESSION_NAME: None
    Zellij version     : No such file or directory (os error 2)

Dependencies
    file             : 5.45
    ueberzugpp       : 2.9.6
    ffmpegthumbnailer: 2.2.3
    magick           : 7.1.1-38
    fzf              : 0.55.0
    fd               : 10.2.0
    rg               : 14.1.1
    chafa            : 1.14.2
    zoxide           : 0.9.6
    7z               : 17.05
    7zz              : No such file or directory (os error 2)
    jq               : 1.7.1

--------------------------------------------------
When reporting a bug, please also upload the `yazi.log` log file - only upload the most recent content by time.
You can find it in the "/home/oneman/.local/state/yazi" directory.

Please describe the problem you're trying to solve

I want to check if a directory is available or not using its pathname before I cd into it.

Would you be willing to contribute this feature?

Describe the solution you'd like

It would be helpful if there were a built-in function to do this. Or a code snippets.

Additional context

No response

Validations

sxyazi commented 1 month ago

It would be helpful if there were a built-in function to do this

There is fs.cha() function you can use to check if the file exists, see https://yazi-rs.github.io/docs/plugins/utils#fs.cha

Closing as already exists

sohanemon commented 1 month ago

Could you please help me identify why this code is not working? I would like to know if there are any syntax errors present.

return {
    entry = function(self, args)
        for _, path in ipairs(args) do
            local cha, err = fs.cha(path, true)
            if cha ~= nil then
                ya.manager_emit("cd", { path })
                return
            end
        end
    end,
}

And the corresponding keymap configuration:

  { on = [
    "g",
    "u",
  ], run = "plugin cdx --args='$CWD/components/ui $CWD/components'", desc = "Go UI Components" },

Objective: I am attempting to use a plugin to provide a list of path names. If a path exists, the program should cd into that path and then exit the loop. If the path does not exist, it should continue to the next path in the list.

Could you please review this and suggest any corrections? Thank you.

sxyazi commented 1 month ago

fs.cha() needs a Url, not a string path, https://yazi-rs.github.io/docs/plugins/types#shared.url.

Also make sure you have a directory with the name $CWD, otherwise you may need to replace it to a actual name

github-actions[bot] commented 3 days ago

I'm going to lock this issue because it has been closed for 30 days. ⏳ This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please file a new issue and complete the issue template so we can capture all the details necessary to investigate further.