kamiyaa / joshuto

ranger-like terminal file manager written in Rust
https://crates.io/crates/joshuto
GNU Lesser General Public License v3.0
3.35k stars 151 forks source link

Add `capture` and `stdout` commands #495

Closed DLFW closed 4 months ago

DLFW commented 4 months ago

This adds two new commands as a base to enable users to use the output of scripts to do certain actions in Joshuto.

The first command this adds is a third command to start a sub-process beside shell and spawn, called capture. Like shell, capture is running blocking but unlike shell, it does not release the terminal but captures the stdout of the sub-process and stores it in an AppContext attribute.

The second command added by this commit is stdout. This command takes the output from the last capture run, stored in the AppContext attribute, and uses it for some action. The action has to be specified as a sub-command. As of now, only stdout cd is implemented. This checks that the last output of capture is a single line of an existing file or directory and then changes the working directory to that.

To get significant value from these new commands, capture needs to be equipped with more variables to feed more information about Joshuto's state into external scripts, and stdout needs to get some more sub-commands.

DLFW commented 4 months ago

This is a first step for #456. Reasons why have chosen this solution are outlines in my comment there.

As mentioned in the commit message, this is currently not sufficient to solve the original requirement, but I thought this is a good first commit, also to have some agreement that this concept is a feasible way to go. I will provide ~two follow-up commits to get some of the other missing pieces and I will also provide some documentation then.

To test the single working “stdout-command”, one can add something like

  { keys = ["e", "t"], commands = ["capture joshtest.sh", "stdout cd"] },

into keymap.toml, a script joshtest.sh like

#!/usr/bin/env bash
echo $(cat /home/dude/test-bookmarks | dmenu)

somewhere in the path, and a list of directories in /home/dude/test-bookmarks like

/home/dude/Downloads
~/music
~/sandbox/joshuto

Then, e-t will open a dmenu where one can choose a directory from the list, and Joshuto will jump to there.

It's just a first, simple example for these two new commands.

DLFW commented 4 months ago

Just did a rebase and fmt

kamiyaa commented 4 months ago

Thanks! Might take a while for me go through the PR

DLFW commented 4 months ago

Just rebased and resolved conflicts

kamiyaa commented 4 months ago

Interesting design. It seems a little messy to have these commands do all this, but I can't think of a better solution at the moment and I'm curious to see where this goes.

DLFW commented 4 months ago

Interesting design. It seems a little messy to have these commands do all this, but I can't think of a better solution at the moment and I'm curious to see where this goes.

“A little messy” is a good description. 😇 In the end, I saw only two alternatives: Complete refactoring of the “command processing” in Joshuto and introducing something like clap to have a real command/option/argument parser (which would have enabled me to implement my initial idea), or introducing something like a socket from where Joshuto can read commands (in parallel to stdout) to allow “remote controlling” of Joshuto. That could have also been used by shell-commands to issue certain reaction. Both these alternatives were too much effort/risky for me right now.

Thanks for merging!