broot icon indicating copy to clipboard operation
broot copied to clipboard

Run custom command on selected file

Open blueray453 opened this issue 5 years ago • 10 comments

For example, I typed cat or ./blueray.pl in the terminal.

Now I want to start broot, select the file/directory so that the name appears after the half typed command. Is there any way to do that?

Or, suppose I am inside broot. Now I want to run a command on the selected file or directory. For example, I want to run for f in **/*.adoc; do echo "$(cat $f | grep "\S"| sed "s/[[:blank:]]*$//" | sort | uniq)" > $f ; done on a selected directory. Or, cat <filename> | grep "\S"| sed "s/[[:blank:]]*$//" | sort | uniq on a selected file. How can I do that?

blueray453 avatar Oct 19 '20 12:10 blueray453

I'm not sure of your question exactly. Or more probably your questions.

The first part may be doing

 cat "$(br)"

in bash, which launches broot, selecting the desired file, then doing :pp.

(and we could probably make this first part easier)

But I probably miss part of your question or of the real problem. Could you please come discuss in https://miaou.dystroy.org/3?Code_Croissants ?

Canop avatar Oct 19 '20 14:10 Canop

I loged in to the chatroom. I am not sure how to find you there.

blueray453 avatar Oct 19 '20 14:10 blueray453

I loged in to the chatroom. I am not sure how to find you there.

I just answered you

Canop avatar Oct 19 '20 14:10 Canop

I just tried

function _broot-return-filename() {
    LBUFFER+="$(br)"
}

zle -N _broot-return-filename
bindkey '^F' _broot-return-filename

It is throwing error Termimad Error : "Crossterm error: IO-error occurred". Any idea?

blueray453 avatar Oct 19 '20 18:10 blueray453

I'll have to try myself. I'll tell you.

Canop avatar Oct 19 '20 18:10 Canop

So whatever became of this? I see that three years later this issue is still not closed.

So if I define a function in my shell's rc file, ...

function sel() {
    echo "You selected file ${1}."
}

... is it possible to create a verb which invokes this function from within broot? This is what I interpreted @blueray453's question to be.

I just discovered broot so am perhaps misunderstanding something. But I attempted to implement the example described above, and broot couldn't find the function. I also scoured the documentation (e.g.), but didn't see any obvious discussion of this.

cohml avatar Jul 26 '23 17:07 cohml

@cohml This is a different question. You can call your function, but as it's a shell function and not a program, the verb is defined with from_shell: true and you must use the br function. The verb would be like this:

        {
            invocation: "sel"
            execution: "sel {file}"
            from_shell: true
        }

(remember: use br, not broot)

Canop avatar Jul 26 '23 17:07 Canop

So whatever became of this? I see that three years later this issue is still not closed.

I'm sorry. This issue is still in my TODO list.

But this list spans about 40 active projects on GitHub, about the same number of not yet shared projects. And as FOSS doesn't feed my family, I can only work on those projects during my spare time.

That's why some issues may take more time than ideal to be solved, even when they're good issues.

Canop avatar Jul 26 '23 17:07 Canop

Brilliant! Thank you for clarifying so quickly. I had missed from_shell in the documentation. That's exactly what I needed and it opens up a universe of possibilities.

This project is incredible btw. If I could feed it to my family, I would 😜

cohml avatar Jul 26 '23 17:07 cohml

As for the original post's first question, here's how I currently do it:

.zle_insert-path-broot () {
  echoti rmkx
  local location_space="${(q-)$(broot --color yes --conf "${HOME}/.config/broot/select.hjson;${HOME}/.config/broot/conf.hjson")} "
  BUFFER+=$location_space
  (( CURSOR+=$#location_space ))
}
zle -N .zle_insert-path-broot
bindkey '^_' .zle_insert-path-broot  # ctrl+/

~/.config/broot/select.hjson:

{
  default_flags: ig
  verbs: [
    {
      internal: ":print_path"
      key: "enter"
      leave_broot: true
    }
  ]
}

And for the OP's second goal, I open up a tmux pane in broot's current folder:

{
  external: "tmux splitw"
  set_working_dir: true
  key: "f4"
  shortcut: z
  leave_broot: false
}

But that doesn't do anything with broot's selected item. Maybe this ought to also copy the selected path to the clipboard...

AndydeCleyre avatar Jul 26 '23 20:07 AndydeCleyre