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

Changing preview_script on-the-fly fails #1483

Closed
slackhead opened this issue Feb 28, 2019 · 6 comments
Closed

Changing preview_script on-the-fly fails #1483

slackhead opened this issue Feb 28, 2019 · 6 comments

Comments

@slackhead
Copy link
Contributor

slackhead commented Feb 28, 2019

Runtime Environment

ranger version: ranger 1.9.2
Python version: 3.6.7 (default, Feb 4 2019, 07:50:16) [GCC 5.5.0]
Locale: en_GB.UTF-8

Current Behavior

When doing :set preview_script /path/to/script on first starting ranger, it is OK so long as no files have been previewed yet. It seems to have random behaviour if you have previewed any. That's the first problem, which may be releated to the greater problem.

After previewing some files (in my case I'm using a custom hex edit script) and then using :set to go back to the default ~/.config/ranger/scope.sh it doesn't not work. It carries on using the custom script.

There is no error at all. It just continues to show files in my hex script.

Expected Behavior

Ranger should be able to change preview scripts on-the-fly.

Context

I sometimes would like to hex view all the files in a directory. My custom script uses xxd to do this.

Possible Solutions

Does ranger cache preview info somewhere? Can it be cleared?

Steps to reproduce

  1. Set a preview script
  2. View some files
  3. Set a different preview script

This is my script if you want to test it. It's a zsh script. I'm not sure if sh/bash can do the calcualtions.

#!/bin/zsh

IN=$(xxd -g0 -c1 -u "$1" | head -n $2)

printf "%-9b %3b %3b %3b %3b %-8b\n" "OFFSET" "ASC" "HEX" "DEC" "OCT" "BIN"
while read -r OFF HEX ASC; do
DEC=$((0x$HEX))
OCT=$(([##8]0x$HEX))
BIN=$(([##2]0x$HEX))
[ ${#BIN} -lt 8 ] && BINFMT="%0$((8-${#BIN}))i%s" || BINFMT="%n%s"

printf "%9b %-3b %-3b %03i %03i $BINFMT\n" \
	"$OFF" "$ASC" "$HEX" "$DEC" "$OCT" 0 "$BIN"

done <<< "$IN"

@slackhead
Copy link
Contributor Author

It looks like doing a 'reset' after changing preview_script fixes this problem. I'm not sure if it affects other things though.

Is there a way of flushing the preview cache without doing a full reset?

@slackhead
Copy link
Contributor Author

I added a simple command that clears the preview cache. I've also tried it in actons.py (different syntax of course) so I'll make a very small PR for it:

class reset_previews(Command):
    """:reset_previews
    Reset the file previews
    """
    def execute(self):
        self.fm.previews = {}

map chain set preview_script ~/.config/ranger/hexview.zsh; reset_previews; redraw_window
map chain set preview_script ~/.config/ranger/scope.sh; reset_previews; redraw_window

This seems to work fine. For some reason adding the redraw command in the function doesn't work - i.e. self.fm.draw_window.

@toonn
Copy link
Member

toonn commented Mar 2, 2019

Hmm, @slackhead, could you investigate that issue further? Not ergonomic enough to include with ranger if you have to remember to redraw anyway imo : /

@slackhead
Copy link
Contributor Author

Yes, I'll take a look at it.

@slackhead
Copy link
Contributor Author

OK, so marking the window for redraw accomplishes it:

self.fm.previews = {}
self.fm.ui.need_redraw = True

I mailed a patch directly to Hut, rather than making a PR.

@toonn
Copy link
Member

toonn commented Mar 3, 2019

Hut's pretty busy so a PR might get merged faster but you do you.

@toonn toonn closed this as completed in aead73e Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants