-
Notifications
You must be signed in to change notification settings - Fork 0
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
helix: write a script that grabs the current filename #61
Comments
Been working slowly through this. I started with what I assumed would be easy: running code within helix. Turns out it's not so simple. Well, running code is easy. There's a great example here for running a quick command via The problem is running code on the current file, like doing We were able to get around this previously, when working with Yazi to setup a file tree. This is because we never needed to get the helix filename, we just had to set the current Helix file to something else. And we could get that filename from Yazi easily. I could use a Do I know Ruby? No! But I'm sure it's possible to figure out. My goal is to see if I can replicate the Ruby script's functionality in Bash. |
Hi, it's future me. Let's see here. First thing that unfortunately needs to be documented: Next, we've moved everything into a nix file via We also put all of this in a branch, which is where it should really live. It's been pushed and can be checked on here. Way better for not having a constant Hmm... what else is there... Oh yeah, functionality! I've succeeded in my ambitions: this is the post about filtering it. The regex in the example code was actually fairly simple. It matches something like this:
It gives you access to each part of this: The mode, the filename, the current position, everything. I'm accessing each of these via file_name=$(echo "$status_line" | awk '{print $2}') For my own future reference, here's what each of them correspond to: And, here's the regex to grab all of these. It is very slightly modified to use PCRE2, since it was previously using ECMAScript which Ripgrep doesn't really support. (?:NOR|INS|SEL)\s+[\x{2800}-\x{28FF}]*\s+\S*\s[^│]* \d+:*.* How does it work? Not my problem. What matters is that it works. So, we've now actually captured the current file. Now, the world is our oyster. Hey future me: I don't know what you'll be doing with this. But you'd better impress me. |
I hope I've impressed you, past me. In terms of functionality, we now have the exact path to the current file being edited. Before, we only had the name of the file, not the full filepath. I considered using a Helix setting that would've put the absolute filepath into the statusbar, but I realized there was an easier solution. See, Yazi automatically sets the title of the current tab. This title looks something like this:
Since we'll always be launching Helix via Yazi, we can just reuse this title for finding the path of the file we're currently editing. Now, what I spent most of my time on: fixing a niche Kitty problem. I'm very tired of dealing with this bug, so I actually won't go in depth into it here. Instead, I'll just tell you the fix: you need to set
Now, we've implemented the base functionality. We can now get the full path to the current file, and do anything we want with it. So, are we done? Well, we could be. But I'm getting big into making languishing PRs, and I have a few things I'd like to get working first. First of all, the script could easily be multipurpose. Dooming it to a life of running python files feels short-sighted. Our wonderful Ruby reference script passed a string as a positional parameter, to choose what to do. I'd prefer to do something a bit more intelligent. I was previously planning on keeping this as a I also want a better method for seeing the actual output. What if the output is longer than can fit in the little Helix popup for the output of a shell command? I think a classic Kitty Oh, and there is a (minor) bug. Getting the folder path from the kitty title via Yazi only works in most cases. When we do Anyways, I think we've gotten around the interactions with Helix. Everything else will be classic Kitty stuff. Good luck with that, future me! |
Thinking about it now: I think splitting into multiple scripts is still a good idea. We'll just have two scripts. Script 1 will returns the name of the current file being edited in Helix, using If we want to get real abstract, we could split it into three scripts. Script2 could use script3, which generates a new split window and runs whatever command you pass into it in the new window. Here's a little pseudocode: current_file=$(script1)
extension=get_file_extension($current_file)
if extension == ".py" then
script3 "python $current_file"
elif extension == ".md" then
script3 "pandoc $current_file"
else
echo "I don't know how to run this kind of file, sorry."
fi |
With that in mind, we could get this PR merged pretty soon, since all it's doing is accomplishing step 1. We do need to cleanup some things in terms of choosing where the script goes, properly naming the file, setting |
Just wanted to note that I love how you're documenting your journey here! Keep it up |
In the interest of documenting my journey further here, let's set a firm cap on scope for this PR. I want this to exclusively be a script that grabs the current tab name. This makes it more likely for me to get this done and get to implementing some actual functionality. And, because I'm a glutton for punishment and LOVE a languishing PR, I was also thinking about a better way to handle grabbing the current filename. See, the Yazi method is easily breakable. If we ever start using the Helix integrated buffer management, it will fail us. If we use the Helazitty functionality, it'll fail us. It's a great hack, but I think I have a better solution. See, I already have a Helix command that toggles a config option, done like this: space.x = ":toggle whitespace.render all none"; I think displaying the absolute path in the statusline all the time is super ugly. But what if, when grabbing the current name of the file, I do something like this: :set statusline.left ["file-absolute-path"]
:sh myScript
:config-reload This would give (Sidenote: it wouldn't have to flash if Helix just had a command that just returned the current filepath, and could've saved me most of the work seen in this issue. It knows the filepath, otherwise it wouldn't be able to put it in the statusbar. It's just very stingy about giving it out.) Anyways, I think I have a good setup here. When I next get time to work on this, I want to cleanup the script, implement this smarter way of grabbing the file, and then I think we're ready to merge! I can then split additional functionality out into several PRs, for all the ideas I have that utilize the current filename. |
Did you see the trick to get the filename in helix-editor/helix#11164 (comment) ? |
I did not. That's gorgeous, and I'm already using the macro commit for other things, so that's perfect. Thanks for sharing it! |
I'm tempted to wait for #61 to implement this, given the amount of work that's gone into it, but I can't reject a genuinely easy solution. I still believe that doing #61 will be better in the long term. That's definitely coping, but let me cope, okay? Just check the date on this commit message and you'll understand.
I'm tempted to wait for #61 to implement this, given the amount of work that's gone into it, but I can't reject a genuinely easy solution. I still believe that doing #61 will be better in the long term. That's definitely coping, but let me cope, okay? Just check the date on this commit message and you'll understand.
I'm tempted to wait for #61 to implement this, given the amount of work that's gone into it, but I can't reject a genuinely easy solution. I still believe that doing #61 will be better in the long term. That's definitely coping, but let me cope, okay? Just check the date on this commit message and you'll understand.
I should be doing more productive things right now, so I'll just leave a link for myself, and write this issue description out in detail later.
https://quantonganh.com/2023/08/19/turn-helix-into-ide
The text was updated successfully, but these errors were encountered: