Yazi Terminal File Manager

repository·main·Indexed 12 days ago

https://github.com/sxyazi/yazi

A high-performance, asynchronous terminal file manager written in Rust. It features a Lua-based plugin system, a virtual filesystem (VFS) for remote management, and extensive image preview support across various terminal protocols including Kitty, Sixel, and ITerm2. Yazi includes a client-server architecture via its Data Distribution Service (DDS) and integrates with tools like ripgrep, fd, fzf, and zoxide.

Tokens
9.5K
Snippets
43
Records
61
Agent score
98%

What's inside Yazi

  1. Overview of Yazi Terminal File Manager

    main

    Yazi (means "duck") is a blazing fast terminal file manager written in Rust, built on non-blocking async I/O. It is designed for efficiency, customizability, and high performance through asynchronous task scheduling and multi-threaded CPU tasks.

    Key Features

    • Asynchronous Core: Full async support for I/O and task management (cancellation, priority, progress updates).
    • Extensible Plugin System: Concurrent Lua-based plugins for UI, functional logic, previewers, preloaders, spotters, and fetchers.
    • Virtual Filesystem (VFS): Supports remote file management, custom VFS providers, and custom search engines.
    • Data Distribution Service (DDS): A client-server architecture with a Lua-based publish-subscribe model for cross-instance communication and state persistence.
    • Rich UI & Integration: Vim-like input components, multi-tab support, scrollable previews (PDF, video, etc.), and integration with tools like ripgrep, fd, fzf, and zoxide.
    • Package Manager: Built-in support to install, update, or pin plugins and themes.
  2. Overview of yazi-sftp

    main

    yazi-sftp is a specialized fork of russh-sftp optimized for use within the Yazi file manager. It provides an idiomatic Rust API and includes several performance and compatibility enhancements over the original library.

    Key features include:

    • UTF-8 Robustness: Supports file paths containing invalid UTF-8 sequences.
    • Extended Metadata: Supports retrieving file nlink, username, and group information.
    • Performance Optimizations:
      • Copy-on-write (CoW) for all packets to minimize memory allocations.
      • Precomputed packet lengths to avoid secondary allocations.
      • Zero-clone buffer management in AsyncRead and AsyncWrite implementations.
    • Idiomatic API: Uses generic return parameters for cleaner code integration.
  3. Customize Yazi configuration, keybindings, and themes

    main

    Yazi comes with default configuration files included in the installation. To customize behavior, you should create override files in your Yazi configuration directory. The specific files you create will override the defaults:

    • General Settings: Create yazi.toml to override yazi-default.toml.
    • Keybindings: Create keymap.toml to override keymap-default.toml.
    • Themes: Create theme.toml to override theme-light.toml and theme-dark.toml.

    Configuration Directory Paths:

    • Unix-like systems: ~/.config/yazi/
    • Windows: %AppData%\yazi\config\
  4. Configure light and dark themes in theme.toml

    main

    When creating a theme.toml to override default themes, you can apply a single color scheme to both light and dark modes.

    For granular control (different colors for light and dark modes), use the [flavor] section in your theme.toml to specify two different flavors, and then provide overrides within those respective flavor files.

  5. How image drivers are selected in Yazi

    main

    Yazi automatically selects an image driver based on your terminal emulator's capabilities and your display server environment. The selection logic follows this priority:

    1. Terminal Capabilities: It checks the terminal Brand (e.g., Kitty, WezTerm, Foot) and specific protocol support like sixel or kgp (Kitty Graphics Protocol).
    2. Multiplexer Support: If a multiplexer (like Tmux or Zellij) is detected, Yazi adjusts the available drivers (e.g., removing KgpOld if a multiplexer is present).
    3. Display Server (Fallback): If the terminal doesn't provide sufficient information, Yazi inspects environment variables:
      • If XDG_SESSION_TYPE is x11, it uses the X11 driver.
      • If XDG_SESSION_TYPE is wayland and a supported compositor is detected, it uses the Wayland driver.
      • If XDG_SESSION_TYPE is wayland but no supported compositor is found, it falls back to Chafa.
      • It also checks WAYLAND_DISPLAY and DISPLAY to attempt identification.
    4. Final Fallback: If no other method works, it defaults to the Chafa driver.
  6. Use the idiomatic API in yazi-sftp

    main

    The yazi-sftp library uses generic return parameters to provide a more idiomatic Rust experience. When sending requests via a session, you can specify the expected response type directly.

    let attrs: responses::Attrs = session.send(requests::Stat::new(path)).await?
  7. Configure Überzug++ preview scaling and offset

    main

    The Überzug++ driver applies scaling and offsets to image preview rectangles based on the YAZI configuration. This is used to adjust the position and size of the image overlay relative to the terminal cell grid.

    Key configuration parameters used by the driver:

    • YAZI.preview.ueberzug_scale: A multiplier applied to the rectangle coordinates and dimensions.
    • YAZI.preview.ueberzug_offset: An (x, y, width, height) tuple used to shift and resize the preview area.

    The driver calculates the final position using: new_coord = max(0, old_coord * scale + offset)

  8. Supported Terminal Image Preview Protocols

    main

    Yazi supports a wide range of terminal protocols for image previews. Depending on your terminal emulator, Yazi will automatically use the best available method.

    Built-in Support (No extra dependencies)

    • Kitty (>= 0.28.0): Kitty unicode placeholders
    • iTerm2: Inline images protocol
    • WezTerm: Inline images protocol
    • Konsole: Kitty old protocol
    • foot: Sixel graphics format
    • Ghostty: Kitty unicode placeholders
    • Windows Terminal (>= v1.22.10352.0): Sixel graphics format
    • st (with Sixel patch): Sixel graphics format
    • Warp (macOS/Linux only): Inline images protocol
    • Tabby: Inline images protocol
    • VSCode: Inline images protocol
    • Rio (>= 0.3.9): Kitty unicode placeholders
    • Black Box: Sixel graphics format
    • Bobcat: Inline images protocol

    External Dependencies Required

    • X11 / Wayland: Requires Überzug++.
    • Fallback: If no protocol is supported, Yazi can use Chafa (>= 1.16.0) to render ASCII art (Unicode block).
  9. Use colors in Yazi Lua scripts

    main

    In Yazi's Lua environment, colors can be represented as Color objects. You can create or manipulate colors using several formats:

    1. Strings: Pass a color name or hex string (e.g., `
  10. Initialize the Yazi runner with `init()`

    main

    The init function is used to set up the global RUNNER instance. It requires a setter function that accepts a Lua state (&mlua::Lua) and returns a mlua::Result<()>. This function also triggers the initialization of the loader module.

    This is typically called during the startup sequence of the Yazi process to bridge the Rust runner logic with the Lua environment.

    pub fn init(setter: fn(&mlua::Lua) -> mlua::Result<()>) {
    	crate::loader::init();
    	RUNNER.init(Runner { setter });
    }
  11. Create and compose colors in Lua

    main

    Yazi provides a way to compose colors in Lua. A color object can be treated as a function (callable) that returns itself, which is useful for chaining or specific UI logic within Yazi's plugin system. When working with the Lua API, a Color can be initialized from a string or a nil value (which defaults to Reset).

    -- Example of how colors are handled in Yazi Lua
    local my_color = "red"
    local reset_color = nil
    
    -- Colors can be passed to UI functions expecting a Color type
    -- Strings like "#ff0000" or "blue" are automatically parsed