luccahuguet / yazelix

zellij, yazi and nushell adding a File Tree to Helix & helix-friendly keybindigs for zellij
Apache License 2.0
214 stars 11 forks source link

Open files from yazi in a helix buffer instead of a zellij new pane #8

Closed luccahuguet closed 3 months ago

luccahuguet commented 4 months ago

No idea how to do that, but things I think of, I'll document here

adriangalilea commented 4 months ago

This is what I'm looking for

luccahuguet commented 4 months ago

This is what I'm looking for

This should be unblocked once helix's plugin system is first released (in six to 9 months), so someone could create a plugin that enables that

but at the same time, we should be getting a file tree plugin, so it is not clear whether having a "yazi sidebar" would still be of value... Why would we not use helix's own file tree plugin?

🤔

luccahuguet commented 4 months ago

if this is possible sooner, i shall implement it!

adriangalilea commented 4 months ago

https://github.com/user-attachments/assets/8530cec6-9cd1-400d-8bd3-869a3ef30143

Initially I show my config.toml to show the line that triggers the script, then the script and then I show the flow in action.

Running this on a pi zero through ssh, hence why it's struggling.

As seen in this github issue you can make it load in the current buffer, you can even select multiple files.

I think the ideal workflow is:

That way the file-tree can be inspected easily with a keybind that you can easily close again, so you can use it both for having a view as well as selecting the files.

I believe this is possible to do as of now, but I won't bother since my current workflow is good enough for me, and I rather work that continue tinkering atm :)

~/.config/helix/config.toml ~/.config/helix/open_in_helix_from_yazi.zsh

luccahuguet commented 4 months ago

Awesome! Looks very cool.

I will probably point to your solution in the readme, I bet it will help many people

I might take a stab at doing this with nushell

thank you for taking the time

luccahuguet commented 3 months ago

I give in, let's roll with a bash script

luccahuguet commented 3 months ago

this is the dumb version:

open_file.sh

#!/bin/sh

# Move focus to the next pane
zellij action focus-next-pane
zellij action write 27 # press esc
zellij action write-chars ":open $1"
zellij action write 13 # press enter

yazi.toml

# yazi.toml

[manager]
ratio = [0, 4, 0]

[opener]
# yazelix v4
edit = [
  { exec = '~/.config/zellij/yazi/open_file.sh $1', desc = "Open File in a new pane" },
]

# yazelix v3
# edit = [{ exec = 'zellij edit "$1"', desc = "Open File in a new pane" }]
luccahuguet commented 3 months ago

the limitation here is that it does not know whether helix is in the next pane or not.

so if you hit enter on a file in yazi, it will try to open it anyway, it will move focus to the next pane, it will hit ESC then type :open path_to_file and hit ENTER

so hopefully it does not cause any accident

luccahuguet commented 3 months ago

this is the slightly less dumb version, and it can only check wheter helix is open if helix was opened from pane directly

#!/bin/sh

# Move focus to the next pane
zellij action focus-next-pane

# Get the running command in the current pane
RUNNING_COMMAND=$(zellij action list-clients | awk 'NR==2 {print $3}')

# Check if the command running in the current pane is helix (hx)
if echo "$RUNNING_COMMAND" | grep -q "/hx$"; then
    # The current pane is running helix, use zellij actions to open the file
    zellij action write 27
    zellij action write-chars ":open $1"
    zellij action write 13
else
    # The current pane is not running helix, open helix with the file
    zellij action new-pane --name "WARNING" -- "echo" "please open helix in a pane right next to the sidebar (to the right of it) using 'hx path_to_file'"

    # some other test
    # zellij action new-pane --name "/hx running" -- "hx" $1
fi

if it was opened from the sidebar (with zelli edit or something then the output of zellij action list-clients changes to something else:

CLIENT_ID ZELLIJ_PANE_ID RUNNING_COMMAND
1         terminal_2     nu -c zellij action list-clients

so in this case, another window would open, otherwise it would be possible opening helix either from yazi or from the terminal, which would be ideal

but for now, that will do, I believe

luccahuguet commented 3 months ago

yazelix v4 will probably be shipping with the slightly less dumb version unless a truly smart one gets developed... 👀

luccahuguet commented 3 months ago

ok, I fixed it.

list-clients now works on both cases because if helix is not open, it will open helix step by step, first creating the new pane, then typing "hx $1", then sleeping for 0.3 seconds, and pressing enter

this way the hx appears in the running_command from list-clients

#!/bin/sh

# Move focus to the next pane
zellij action focus-next-pane

# Get the running command in the current pane
RUNNING_COMMAND=$(zellij action list-clients | awk 'NR==2 {print $3}')

# Check if the command running in the current pane is helix (hx)
if echo "$RUNNING_COMMAND" | grep -q "/hx$"; then
    # The current pane is running helix, use zellij actions to open the file
    zellij action write 27
    zellij action write-chars ":open $1"
    zellij action write 13
else
    # The current pane is not running helix, so open helix in a new pane
    zellij action new-pane
    sleep 0.3
    zellij action write-chars "hx $1"
    zellij action write 13

    # warning from before I got it working
    # zellij action new-pane --name "WARNING" -- "echo" "please open helix in a pane right next to the sidebar (to the right of it) using 'hx path_to_file'"
fi

this will detect the helix pane next to the sidebar, whether it was opened from the pane or from the sidebar

so this is the smart version

luccahuguet commented 3 months ago

I should probably merge this tomorrow, possibly in a v4 branch

luccahuguet commented 3 months ago

solved by #16