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

keycalm Unable to Store Persistent State in Yazi Micro Plugins #2279

Closed
2 of 3 tasks
maarutan opened this issue Feb 2, 2025 · 2 comments
Closed
2 of 3 tasks

keycalm Unable to Store Persistent State in Yazi Micro Plugins #2279

maarutan opened this issue Feb 2, 2025 · 2 comments
Labels
invalid This doesn't seem right

Comments

@maarutan
Copy link

maarutan commented Feb 2, 2025

yazi --debug output

❯ /sbin/yazi  --debug

Yazi
    Version: 0.4.2 (Arch Linux 2025-01-12)
    Debug  : false
    Triple : x86_64-unknown-linux-gnu (linux-x86_64)
    Rustc  : 1.84.0 (9fc6b431 2025-01-07)

Ya
    Version: 0.4.2 (Arch Linux 2025-01-12)

Emulator
    Brand.from_env      : Some(Kitty)
    Emulator.detect     : Emulator { kind: Left(Kitty), light: false, cell_size: Some((14, 29)) }
    Emulator.detect_full: Ok(Emulator { kind: Left(Kitty), light: false, cell_size: Some((14, 29)) })

Adapter
    Adapter.matches: Kgp

Desktop
    XDG_SESSION_TYPE           : Some("x11")
    WAYLAND_DISPLAY            : None
    DISPLAY                    : Some(":0")
    SWAYSOCK                   : None
    HYPRLAND_INSTANCE_SIGNATURE: None
    WAYFIRE_SOCKET             : None

SSH
    shared.in_ssh_connection: false

WSL
    WSL: false

NVIM
    NVIM          : false
    Neovim version: 0.10.4

Variables
    SHELL           : Some("/usr/bin/zsh")
    EDITOR          : Some("nvim")
    VISUAL          : Some("nvim")
    YAZI_FILE_ONE   : None
    YAZI_CONFIG_HOME: None
    YAZI_ZOXIDE_OPTS: None
    FZF_DEFAULT_OPTS: Some("\n--reverse --ansi --no-multi\n--bind=ctrl-u:up,ctrl-e:down,ctrl-n:backward-char,ctrl-i:forward-char,ctrl-b:backward-word,ctrl-h:forward-word\n--border --margin=1,0\n")

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

Multiplexers
    TMUX               : 0
    tmux version       : tmux 3.5a
    tmux build flags   : enable-sixel=Unknown
    ZELLIJ_SESSION_NAME: None
    Zellij version     : No such file or directory (os error 2)

Dependencies
    file          : 5.46
    ueberzugpp    : No such file or directory (os error 2)
    ffmpeg/ffprobe: 7.1 / 7.1
    pdftoppm      : No such file or directory (os error 2)
    magick        : 7.1.1-43
    fzf           : 0.57.0
    fd/fdfind     : No such file or directory (os error 2) / No such file or directory (os error 2)
    rg            : 14.1.1
    chafa         : No such file or directory (os error 2)
    zoxide        : 0.9.6
    7z/7zz        : No such file or directory (os error 2) / No such file or directory (os error 2)
    jq            : No such file or directory (os error 2)

Clipboard
    wl-copy/paste: No such file or directory (os error 2) / No such file or directory (os error 2)
    xclip        : 0.13
    xsel         : 1.2.1

Please describe the problem you're trying to solve

I'm trying to develop a micro plugin for Yazi that tracks repeated key presses and temporarily blocks movement after 5 rapid presses of j or k. However, I encountered an issue where the plugin cannot persist state variables such as key counts or blocked state.

Expected Behavior:

  • The plugin should increment a counter each time j or k is pressed.
  • After 5 presses, the corresponding key should be blocked for 2 seconds.
  • Pressing <Esc> should reset the counter and unblock the keys immediately.

Problem:

  • Yazi does not seem to support persistent state storage for micro plugins.
  • Using ya.var() or ps.pub()/ps.sub() results in errors like attempt to call a nil value.
  • There appears to be no built-in way to maintain a counter across function calls.

Question:
Is there any recommended way in Yazi to store and persist state across function executions within a micro plugin?

Would you be willing to contribute this feature?

  • Yes, I'll give it a shot

Describe the solution you'd like

I would like a way to persist state within a Yazi micro plugin, such as maintaining a counter for key presses and a flag for blocking input. Ideally, Yazi should provide a mechanism like:

  1. A Built-in State Management API

    • A function like ya.state("key", value) to store and retrieve values across plugin executions.
    • This would allow plugins to track things like key press counts or timers without resetting on each function call.
  2. Support for Temporary Variables

    • A way to declare temporary variables within the plugin’s runtime that persist across calls.
    • For example, ya.session_var("key", value) for short-lived states that reset when Yazi restarts.
  3. Built-in Event System Enhancements

    • A more robust event system (ps.pub() / ps.sub()) that allows micro plugins to listen for state changes and react accordingly.
    • Currently, ps.pub() and ps.sub() seem to be unavailable in micro plugins.

With these features, I could store the key press count and blocked state reliably, allowing for smoother implementation of my "Cowboy" plugin that prevents spamming movement keys.

Additional context

I implemented this logic in nvim:

Image

Checklist

  • I have searched the existing issues/discussions
  • The latest nightly build doesn't already have this feature
@maarutan maarutan added the feature New feature request label Feb 2, 2025
@sxyazi
Copy link
Owner

sxyazi commented Feb 2, 2025

  • Yazi does not seem to support persistent state storage for micro plugins.

Each plugin has its own independent state (self), you can store any Lua type you need in self, see https://yazi-rs.github.io/docs/plugins/overview#sync-vs-async

  • Using ya.var() or ps.pub()/ps.sub() results in errors like attempt to call a nil value.

There's no ya.var(). ps.pub()/ps.sub() can only be used in a sync context, make sure your plugin is sync or it's called within a sync block (ya.sync()).

  • There appears to be no built-in way to maintain a counter across function calls.

https://yazi-rs.github.io/docs/plugins/overview#sync-context is exactly what you're looking for.

Closing as already supported

@sxyazi sxyazi closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2025
@sxyazi sxyazi added invalid This doesn't seem right and removed feature New feature request labels Feb 2, 2025
@maarutan
Copy link
Author

maarutan commented Feb 2, 2025

thanks this works 😃😃😃😃😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants