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 more control over cleaner #626

Closed
build2stone opened this issue May 15, 2021 · 5 comments · Fixed by #729
Closed

Allow more control over cleaner #626

build2stone opened this issue May 15, 2021 · 5 comments · Fixed by #729

Comments

@build2stone
Copy link

I'd find it useful if the cleaner script was not run when the next file requires a preview (ie has no preview cached).
This would allow selectively running it from within the preview script.

Example: A directory full of images, previewed using the ueberzug scripts from the wiki
Right now, the cleaner script is always run, causing the previews to flicker when switching from image to image.
My suggestion would enable skipping the cleaner when the next file is also an image.
(In this case ueberzug smoothly replaces the preview)

draw_img.sh could look like this:

#!/bin/sh
draw() {
  ~/.config/lf/draw_img.sh "$@"
  exit 1
}

hash() {
  printf '%s/.cache/lf/%s' "$HOME" \
    "$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
}

cache() {
  if [ -f "$1" ]; then
    draw "$@"
  fi
}

file="$1"
shift

if [ -n "$FIFO_UEBERZUG" ]; then
  case "$(file -Lb --mime-type -- "$file")" in
    image/*)
      orientation="$(identify -format '%[EXIF:Orientation]\n' -- "$file")"
      if [ -n "$orientation" ] && [ "$orientation" != 1 ]; then
        cache="$(hash "$file").jpg"
        cache "$cache" "$@"
        convert -- "$file" -auto-orient "$cache"
        draw "$cache" "$@"
      else
        draw "$file" "$@"
      fi
      ;;
    video/*)
      cache="$(hash "$file").jpg"
      cache "$cache" "$@"
      ffmpegthumbnailer -i "$file" -o "$cache" -s 0
      draw "$cache" "$@"
      ;;
  esac

  ~/.config/lf/clear_img.sh
fi

file -Lb -- "$1" | fold -s -w "$width"
exit 0

Of course, this will sometimes uselessly clear images, but that shouldn't be much of a problem.

Alternatively, is there some way to tell whether a file will trigger the preview script?
Maybe a variable with a list of files with cached previews?

@gokcehan
Copy link
Owner

@build2stone To be honest, I don't use image previewing myself and I'm not aware if there is a way to do this currently or not. Maybe we could export $f variable and you could check whether if it's an image or not and selectively skip cleaning.

@build2stone
Copy link
Author

Exporting $f sounds great

@gokcehan
Copy link
Owner

I have reverted dirtyfiles patch for the while so reopening this issue for further discussion.

@gokcehan
Copy link
Owner

@build2stone @lucas-mior I have now pushed a commit to export files (i.e. $f, $fs, $fx, $PWD) and options before previewer and cleaner calls. Skipping image cleaning can now be used by adding the following to the beginning of the cleaner script:

if [ "$1" != "$f" ]; then
  case "$(file -Lb --mime-type -- "$f")" in
    image/*) exit;;
  esac
fi

The first check "$1" != "$f" is to avoid skipping cleaning when the selection does not change but cleaning is required (e.g. ! shell commands) with a similar purpose for forceClear in #729 . This is not necessarily simpler than the previous approach but the complexity is pushed to the script instead. I think this is better for the codebase. Hopefully you are ok with this approach as well. I have pushed the commit to make it available for testing so feel free to leave feedback.

@build2stone
Copy link
Author

Thanks for the fix!
No complaints from my end, seems to work well.
I don't mind the script ending up being more complex.
I think most people would probably only want to customize the preview script, so this extra check in cleaner doesn't really matter (ie they'll just use the examples given in the wiki, which could just include this extra check).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants