willeccles / cfm

🌵 Simple and fast TUI file manager with no dependencies.
https://cfm.atinycact.us
Mozilla Public License 2.0
105 stars 7 forks source link

Shell script $EDITOR handling #32

Closed zuixalias closed 1 year ago

zuixalias commented 1 year ago

I have a shell script that i use to run my editor which i have used in $EDITOR. When a file/ folder is created using cfm, the editor opens and exits quickly i believe, so i am unable to name/ rename them.

willeccles commented 1 year ago

Can you share the script?

zuixalias commented 1 year ago
#!/usr/bin/env bash

REL_PATH=path/to/editor/editor_name
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
$SCRIPT_DIR/$REL_PATH $@

Probably running the sript as an arg to some terminal would keep it open (idk)

willeccles commented 1 year ago

Without specifics on the editor in question, I have no idea why this wouldn't work. Does it work if you set EDITOR to something like vim, nano, etc.?

willeccles commented 1 year ago

I am curious: what is the purpose of this script?

zuixalias commented 1 year ago

Yes, the behaviour on the editor is same as any other editor (ex: vi) The issue i think is that it's a script, as putting this in the script leads to the same behaviour:

vi

Whereas specifying the editor directly in $EDITOR (i.e., no script) it's works good...including the editor that i am using.

willeccles commented 1 year ago

Did you make sure the script is executable? I just did a quick test with this script:

#!/bin/sh -e

exec nvim "$@"

If the script is not executable, it immediately exits. If it is executable, it works just how I'd expect it to.

willeccles commented 1 year ago

It also causes really broken behavior when I try to reproduce #34.

zuixalias commented 1 year ago

I see, i am not sure what else is broken but for me cfm crashes and keeps running in bg with 99% cpu usage.

willeccles commented 1 year ago

That sounds like an issue with your script. Without knowing what's actually in it or what the point of it is, I am not sure I can help you very much, unfortunately.

zuixalias commented 1 year ago

Yes, i jus tried it.

exec /home/user/path/to/editor/editor_binary_name "$@"


It's still the same...
willeccles commented 1 year ago

Do you get any sort of error when you try to rename a file, for example?

zuixalias commented 1 year ago

No, it creates the file with the default name. Then if i try to recreate a file/ folder...it tell me that File/ Directory already exists Could it be related to the shell? Like you using a different shell so it works for you but on bash it doesn't. (I am a linux newbie so don't know much)

Renaming the file gives: Target file already exists

willeccles commented 1 year ago

cfm does not use your shell. What editor are you using?

zuixalias commented 1 year ago

I am using hx (Helix)

willeccles commented 1 year ago

Does EDITOR=hx cfm work properly?

zuixalias commented 1 year ago

Hmm, there is the issue i think. EDITOR=vi cfm works but EDITOR=path/to/editor/editor_binary_name cfm does not...(the editor_binary is marked as executable btw)

zuixalias commented 1 year ago

No no no, i am sorry, it works...just needed to specify path from root (/home/user/...) or with ~/......

willeccles commented 1 year ago

When cfm prompts you for input, the working directory is set to /tmp as it is editing a file located there. If you are trying to do anything with a relative path, unless it's relative to /tmp it will not work.

zuixalias commented 1 year ago

I see, ok...but the script that i first posted is meant for such use cases, i.e., it will always use the correct path, irrespective of the current working directory/ directory from which it is called. So that is most probably not the issue (i just checked the paths to confirm this)

zuixalias commented 1 year ago

STEPS TO REPRODUCE MY SETUP IN THIS ISSUE (Leaving here just in case) :

REL_PATH=files/helix/hx SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) $SCRIPT_DIR/$REL_PATH $@


Now you can run helix form anywhere by typing `hx`.

---------------------------------------------------------------------------

Now, to reproduce the issue:
- Run `cfm` and create a dir/ file.

But if you run `cfm` by specifying the path directly, it works:
```bash
EDITOR=~/ware/files/helix/hx cfm
willeccles commented 1 year ago

Try using $HOME instead of ~ in your PATH.

zuixalias commented 1 year ago

What sorcery is this xD. It is solved now, thanks a lot!

willeccles commented 1 year ago

Programs do not expand tildes in path usually. Your shell is expanding the one when you manually set EDITOR like that, but the exec family of syscalls will not expand tildes.