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

Using lf with cd on exit and show preview images? #1448

Closed
MarcCoquand opened this issue Sep 27, 2023 · 9 comments · Fixed by #1451
Closed

Using lf with cd on exit and show preview images? #1448

MarcCoquand opened this issue Sep 27, 2023 · 9 comments · Fixed by #1451

Comments

@MarcCoquand
Copy link

Hi!

I followed the tutorial provided for setting up lf together with image previews. That part works well. What does not work is using it together with lfcd. Instead of showing previews, it prints what I think is the source code for the image.

This is my script

alias l='cd "$(command lf -print-last-dir "$@")"'                                                                                                                                                                                 

My previewer script is

#!/bin/sh                                                                                                                                                                                                                         
case "$(printf "%s\n" "$1" | awk '{print tolower($0)}')" in                                                                                                                                                                       
   *.bmp|*.jpg|*.jpeg|*.png|*.xpm|*.webp|*.gif)                                                                                                                                                                                   
      chafa "$1" -f sixel -s "$(($2-2))x$(($3-2))"                                                                                                                                                                                
   ;;                                                                                                                                                                                                                             
   *.pdf)                                                                                                                                                                                                                         
      pdftoppm -jpeg -f 1 -singlefile "$1" | chafa -f sixel -s "$(($2-2))x$(($3-2))"                                                                                                                                              
   ;;                                                                                                                                                                                                                             
   *) cat "$1";;                                                                                                                                                                                                                  
esac                                                                                                                                                                                                                              
                                                                                                                                                                                                                                  
exit 0                                                                                                                                                                                                                            
@joelim-work
Copy link
Collaborator

This looks like an unforeseen issue when adding support for sixel previews in #1211, where previews are drawn by using fmt.Print to write the sixel data to the terminal:

lf/sixel.go

Lines 38 to 44 in dddf437

// XXX: workaround for bug where quitting lf might leave the terminal in bold
fmt.Print("\033[0m")
fmt.Print("\0337") // Save cursor position
fmt.Printf("\033[%d;%dH", sxs.yprev, sxs.xprev) // Move cursor to position
fmt.Print(*sxs.sixel) //
fmt.Print("\0338") // Restore cursor position

However fmt.Print prints to standard output (this is the kind of function you would use for a 'hello world' program), and while sixel previews happen to work when running lf directly, I guess this doesn't work inside command substitution (i.e. $(...)) as the sixel data output is being interpreted as a command to execute.

I'm not sure how to address this, hopefully someone else might have a better idea. But as a workaround you can try using the old lfcd approach, using -last-dir-path instead of -print-last-dir:

lf/etc/lfcd.sh

Lines 17 to 30 in 88b3c99

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
}

@joelim-work
Copy link
Collaborator

OK looks like printing the sixel data to stderr instead of stdout seems to work, while not interfering with command substitution:

diff --git a/sixel.go b/sixel.go
index 326187f..7fb3c9f 100644
--- a/sixel.go
+++ b/sixel.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"fmt"
+	"os"
 	"strings"
 
 	"github.com/gdamore/tcell/v2"
@@ -36,12 +37,12 @@ func (sxs *sixelScreen) showSixels() {
 	}
 
 	// XXX: workaround for bug where quitting lf might leave the terminal in bold
-	fmt.Print("\033[0m")
+	fmt.Fprint(os.Stderr, "\033[0m")
 
-	fmt.Print("\0337")                              // Save cursor position
-	fmt.Printf("\033[%d;%dH", sxs.yprev, sxs.xprev) // Move cursor to position
-	fmt.Print(*sxs.sixel)                           //
-	fmt.Print("\0338")                              // Restore cursor position
+	fmt.Fprint(os.Stderr, "\0337")                              // Save cursor position
+	fmt.Fprintf(os.Stderr, "\033[%d;%dH", sxs.yprev, sxs.xprev) // Move cursor to position
+	fmt.Fprint(os.Stderr, *sxs.sixel)                           //
+	fmt.Fprint(os.Stderr, "\0338")                              // Restore cursor position
 }
 
 func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {

Would you mind trying out the following branch to see if this approach works for you? https://github.com/joelim-work/lf/tree/fix-sixel-lfcd

@MarcCoquand
Copy link
Author

MarcCoquand commented Sep 28, 2023 via email

@joelim-work
Copy link
Collaborator

It's a standard Go project, so you can just clone the repo and run go build, which should build the lf binary. I think the official method is to run the gen/build.sh script, but I typically don't bother with that.

@MarcCoquand
Copy link
Author

MarcCoquand commented Sep 28, 2023 via email

@joelim-work
Copy link
Collaborator

joelim-work commented Sep 29, 2023

That's strange, I tested the changes on my own setup and the sixel previews showed up fine. Hopefully this fix would work for other users too.

Did you by any chance check out the correct branch before running go build? Don't use the master branch on my fork, I generally keep it synced with the upstream master. Also try removing command, and check your PATH and aliases - hopefully when you run lf, you are running the version you have built instead of the one that is installed on your system.

Apologies for asking you to go through all this, but I wanted to make sure that this actually works on someone else's setup and not just my own. If you still have trouble I can try tagging some other users to see if they have time to help test.

@noornee
Copy link

noornee commented Sep 29, 2023

Just tested it on mine and it works

Preview

lfcd.webm

@MarcCoquand
Copy link
Author

MarcCoquand commented Sep 29, 2023 via email

@joelim-work
Copy link
Collaborator

Thanks for testing it out, I have submitted PR #1451

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 a pull request may close this issue.

3 participants