broot icon indicating copy to clipboard operation
broot copied to clipboard

Terminal opening from broot fails with nushell

Open alexherbo2 opened this issue 4 years ago • 8 comments

Dolphin has something like Open Terminal Here, I would like the same to open a shell here and back to Broot on exit.

alexherbo2 avatar Apr 18 '21 23:04 alexherbo2

But you already have it. It's behind ctrlT with default conf.

If you lost (or never had) this part of the conf, it's here: https://github.com/Canop/broot/blob/master/resources/default-conf.hjson#L181

Canop avatar Apr 19 '21 18:04 Canop

For some reason nu doesn’t work and blocks.

It looks to be when set_working_dir is true.

def br [...rest] {
  let path = $(broot --out /dev/stdout $rest | str trim)
  let is_empty = $(= $path | empty?)
  if $is_empty {} {
    cd $path
  }
}

alexherbo2 avatar Apr 19 '21 18:04 alexherbo2

Can we change this issue (and this title) to focus on the this specific incompatibility with nushell (might be related to to specific br function) ?

Canop avatar Apr 19 '21 18:04 Canop

Yep

alexherbo2 avatar Apr 19 '21 18:04 alexherbo2

I have a solution which doesn't seem to cause a hang but this is the first script I've written with nutshell so take it with a grain of salt. I'm not sure how to pass args through to broot because nu tries to parse the named arguments and finds they aren't explicitly defined in the function so it throws an error. You could put the args in quotes and pass them as a string but that's a bummer.

I'm not sure of the cause of the hang in the function from above but I noticed a similar hang when exiting broot after using the editor forked from broot (default e key). In that case, I noticed that $path variable was filled with the buffer from nano. That caused me to switch to the --outcmd method which is when the hang disappeared for me.

Tested on nu 0.35.

def br [ ] {
  # Make temporary file for broot to write to
  # Trim the cmd_file path otherwise open throws an error claiming the file DNE
  # I think this bug in nu is caused by it carrying a newline from the mktemp output
  #   and trying to parse as a path 
  let cmd_file = (mktemp | str trim);

  # run broot
  ^broot --outcmd $"($cmd_file)";

  # Trim out any white space broot may have added
  let cmd = ((open $cmd_file) | str trim);

  # Cleanup tmp file
  # Don't use nu's rm since output to std cant be suppressed?
  ^rm $cmd_file;

  # Make sure cmd is not empty
  # If it is empty, checking that it starts with cd will throw error
  if ($cmd | empty?) {} {

    # --outcmd implies there may be other cmds broot can execute but
    # im not sure what they may be or may be in the future
    # so check if cmd starts with cd
    if ($cmd | str starts-with "cd") {

      # remove cd and cd
      cd ($cmd | str find-replace "cd" "" | str trim);

    } {}
  }
}

I also spent a while trying to get nu to load the function from a script file (I called it ~/.nurc.nu) but the declaration didnt seem to persist past the execution of the script. So, a note to future readers of this comment:

To make this change permanent, compress your fn to a single line and surround in single quotes, then pass to config set startup [ ]

like so:

config set startup ['def br [ ] { let cmd_file = (mktemp | str trim); ^broot --outcmd $"($cmd_file)"; let cmd = ((open $cmd_file) | str trim); ^rm $cmd_file; if ($cmd | empty?) {} { if ($cmd | str starts-with "cd") { cd ($cmd | str find-replace "cd" "" | str trim); } {} }}']

Broot is wonderful, thank you!

sorenrade avatar Aug 21 '21 07:08 sorenrade

I have also experienced that NuShell can't change directory after execute custom command. https://github.com/nushell/nushell/issues/5218

Hipfox avatar Apr 17 '22 14:04 Hipfox

@Hipfox and what about @sorenrade 's solution ?

Canop avatar Apr 17 '22 14:04 Canop

@Hipfox and what about @sorenrade 's solution ?

As that is by design with Nushell's environment is scoped, I have no idea but try

C:\Users\harry〉def-env br [] {
:::   let cmd_file = (^mktemp | str trim);
:::   ^broot --outcmd $"($cmd_file)";
:::   let-env cmd = ((open $cmd_file) | str trim);
:::   ^rm $cmd_file;
::: }
C:\Users\harry〉br
C:\Users\harry〉echo $env.cmd
cd C:\Users\harry\vimfiles

After run br and then alt-Enter, catch the custom $env.cmd but still no idea implements to cd.

Hipfox avatar Apr 17 '22 16:04 Hipfox