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

Allow lastdir/selection to be written to stdout #1399

Merged
merged 2 commits into from
Aug 28, 2023
Merged

Allow lastdir/selection to be written to stdout #1399

merged 2 commits into from
Aug 28, 2023

Conversation

joelim-work
Copy link
Collaborator

In order for lf to successfully write information to /dev/stdout when using the -last-dir-path/-selection-path options, the following changes needs to be made:

  • Writing the information needs to happen after the tcell screen is cleaned up, as pointed out in question: Is there a way i can output the last-dir-path to STDOUT #472 (comment).
  • The output of shell commands needs to be connected to stderr instead of stdout so as not to conflict with the actual information being written. It might be possible in future to change the output of shell commands with a setting, but I think hardcoding it to stderr should suffice for now.

This change makes scripting with lf much easier, as there is no longer any need to create a temporary file to store the information and read it afterwards. For example the lfcd command can be changed from this:

lfcd () {
    tmp="$(mktemp)"
    # `command` is needed in case `lfcd` is aliased to `lf`
    command lf -last-dir-path="$tmp" "$@"
    if [ -f "$tmp" ]; then
        dir="$(cat "$tmp")"
        rm -f "$tmp"
        if [ -d "$dir" ]; then
            if [ "$dir" != "$(pwd)" ]; then
                cd "$dir"
            fi
        fi
    fi
}

To this:

lfcd() {
    # `command` is needed in case `lfcd` is aliased to `lf`
    cd "$(command lf -last-dir-path /dev/stdout "$@")"
}

@gokcehan
Copy link
Owner

@joelim-work I wasn't aware of the issue with the order of cleanup. For me cd $(lf -last-dir-path /dev/stdout) already works when I don't use any commands, though I see now it does not print anything to the terminal when used outside of a subshell with lf -last-dir-path /dev/stdout. This patch makes it possible to see the output outside of subshells as well. Everything seems good to me.

@joelim-work joelim-work marked this pull request as ready for review August 28, 2023 01:13
@joelim-work joelim-work merged commit 63d21b5 into gokcehan:master Aug 28, 2023
4 checks passed
@joelim-work joelim-work deleted the stdout2 branch August 28, 2023 01:35
@nicolasdumitru
Copy link

Hello! Does this mean that starting with the next release lfcd will be usable without temporary files? Also, I'd just like to point out that /dev/stdout is not POSIX (they specifically mention it). I use Linux and /dev/stdout is available to me, but what about the other Unix-like operating systems?

@joelim-work
Copy link
Collaborator Author

I responded in #1393 (comment) (ticket reopened)

@ilyagr
Copy link
Collaborator

ilyagr commented Aug 28, 2023

This is a very interesting solution. I'm still worried that it might not work on some weird system (Windows?), but it works perfectly for me for now.

I'm wondering if this would allow some generalization beyond printing the last visited dir. I don't have any immediate specific applications in mind, and I don't propose implementing anything before we have a compelling usecase, but it would be straightforward to have a "print to actual stdout" lf command that would allow lf to be put into some elaborate pipelines.

@gokcehan gokcehan mentioned this pull request Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lfcd improvement proposal
4 participants