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

Execute program with file name(s) as argument #695

Open
oyarsa opened this issue Apr 5, 2023 · 14 comments
Open

Execute program with file name(s) as argument #695

oyarsa opened this issue Apr 5, 2023 · 14 comments
Labels
enhancement New feature or request

Comments

@oyarsa
Copy link

oyarsa commented Apr 5, 2023

This seems like an obvious thing, so I apologise if it's duplicated somewhere.

We can define verbs that execute arbitrary commands, but there doesn't seem to be a way to run commands from within broot itself. I don't want to define every single command I might use as a verb; it should be possible to run something like !vim % to run Vim with the current file being highlighted passed as an argument.

This should also work with the staging area: if multiple files are selected, they should expand into the arguments. E.g., if the staging area contains files a.txt and b.txt, !vim % would execute vim a.txt b.txt.

@oyarsa oyarsa added the enhancement New feature or request label Apr 5, 2023
@Canop
Copy link
Owner

Canop commented Apr 22, 2023

Today, it's possible to define a verb like this:

    {
        invocation: "run {exec}"
        shortcut: r
        execution: "{exec} {file}"
        leave_broot: false
    }

With such a verb you can type :r vim and it launches vim on the selected file.

Allowing :!vim is probably doable.

@oyarsa
Copy link
Author

oyarsa commented Apr 22, 2023

This works great with single files, thank you. However, this doesn't behave as expected with the staging area. If I stage a few files and run r vim, I get individual instances of vim for each file instead of one vim instance with all files. I think instead of doing vim file1 file2, it's doing vim file1; vim file2.

@Canop
Copy link
Owner

Canop commented Apr 22, 2023

Yes, the behavior of verbs on the staging area today is to execute sequentially.
I'm pondering the best way to manage that. Either a {files} argument for the execution or a verb parameter describing the behavior to use for multiple selections.

@thealio
Copy link

thealio commented Sep 7, 2023

Today, it's possible to define a verb like this:

    {
        invocation: "run {exec}"
        shortcut: r
        execution: "{exec} {file}"
        leave_broot: false
    }

With such a verb you can type :r vim and it launches vim on the selected file.

Allowing :!vim is probably doable.

Hi,
I'm trying to use this for launch my scripts as following


 # This verb lets you run an executable with ctrl-r
   {
        invocation: "run {exec}"
        shortcut: r
        key: ctrl-r
        execution: "{exec} {file}"
        leave_broot: false
    }

and , on broot , I select the script , then I write
:r exec

But the command is not successful . On bash exec myscript.sh works
I'd like to be able to do the equivalent of ./myscript.sh on the shell

@Canop
Copy link
Owner

Canop commented Sep 7, 2023

It's a little more complex for a shell script than for an executable. What's your shell script doing ? Can you post it here (or a shortened variant) ?

@thealio
Copy link

thealio commented Sep 8, 2023

It's a little more complex for a shell script than for an executable. What's your shell script doing ? Can you post it here (or a shortened variant) ?

Every bash script I run don't work
Even a simple test script


#!/bin/bash

echo "This is a test"
sleep 1
echo "It should work well"

@Canop
Copy link
Owner

Canop commented Sep 9, 2023

You have to call an executable, to run your script. For example:

{
    invocation: hello
    external: "sh -e /path/to/hello.sh"
    leave_broot: false
}

If your script takes arguments:

{
    invocation: hello
    external: "sh -e /path/to/do_something.sh {file}"
    leave_broot: false
}

@Canop
Copy link
Owner

Canop commented Sep 9, 2023

As I realize this may not be obvious, I'm adding a section in the doc: https://dystroy.org/broot/conf_verbs/#call-shell-scripts

@thealio
Copy link

thealio commented Sep 9, 2023

You have to call an executable, to run your script. For example:

{
    invocation: hello
    external: "sh -e /path/to/hello.sh"
    leave_broot: false
}

If your script takes arguments:

{
    invocation: hello
    external: "sh -e /path/to/do_something.sh {file}"
    leave_broot: false
}

Hi,
thank you for your efforts about my request.
Following your guide, I was able to archive scripts launching as following

# This verb lets you run an executable shell script with ctrl-r
# (on exit you'll be back in broot)
  {
      invocation: "run"
      shortcut: ru
      key: ctrl-r
      external: "sh -e {file}"
      leave_broot: false
  }

I have another question, for making script launching even better.
Is it possible to launch scripts with their flags?
For example , supposing the script hello.sh can be launched with flag -h for help as hello.sh -h .
Practically a way to add " -h" or every other word at the end of the script name, typing it in broot

@Canop
Copy link
Owner

Canop commented Sep 9, 2023

You mean type the flag in broot ? Yes:


  {
      invocation: "r"
      external: ["sh" "-e" "{file}"]
      leave_broot: false
  }
  {
      invocation: "rf {flag}"
      external: ["sh" "-e" "{file}" "-{flag}"]
      leave_broot: false
  }

Select your executable then type 'r' and enter to execute it without flag, or 'rf h' to execute it with '-h'.

@thealio
Copy link

thealio commented Sep 9, 2023

invocation: "r"
external:

Hi
it works perfectly! Really nice!

my final code is the following

  # This verb lets you run an executable shell script with ctrl-r
   # (on exit you'll be back in broot)
   {
       invocation: "run"
       shortcut: ru
       key: ctrl-r
       external: "sh -e {file}"
       leave_broot: false
   }

   # Run shell script with flag --> rf [flag]
   {
       invocation: "runf {flag}"
       shortcut: rf
       external: "sh -e {file} -{flag}"
       leave_broot: false
   }
   

Many thanks!

@Martin1887
Copy link

Martin1887 commented Mar 16, 2024

This is very interesting and it has a lot of potential applications. I think it should be highlighted in the web or another place (maybe another blog post?). Related to #152.

In my opinion, it would be more flexible if you could type any command and use placeholders to indicate file and (parent if it is a file) directory. That would be a great addition to broot!

In a future stage, commands could be suggested (à la vim commands) in relation to the MIME type of the file, this would be related to #823.

Thanks.

@Canop
Copy link
Owner

Canop commented Mar 19, 2024

and use placeholders to indicate file and (parent if it is a file) directory. That would be a great addition to broot!

I'm sorry but I don't follow you here. Could you give some examples ?

@Martin1887
Copy link

Of course. It could be like the syntax for creating verbs in hjson/TOML files but directly into broot for one-time commands that you will probably not execute anymore (so configuring the verb does not pay off). Some examples:

 tar -xzvf {file}
 mv {file} {file-name}.old
 okular {file} (supposing your default PDF reader is evince for instance)
 codium {directory}

We could name them something like 'live verbs'.

For the commands recommendation, it could be triggered when typing and optionally using and special shortcut (probably Tab), and it could be based on the MIME type of the selected file for a better user experience. But this is only a nice addition that could be added in the future.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants