diff options
Diffstat (limited to 'ranger/gui/widgets/browsercolumn.py')
-rw-r--r-- | ranger/gui/widgets/browsercolumn.py | 107 |
1 files changed, 28 insertions, 79 deletions
diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py index fbacbccd..d617e64e 100644 --- a/ranger/gui/widgets/browsercolumn.py +++ b/ranger/gui/widgets/browsercolumn.py @@ -14,7 +14,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. """The BrowserColumn widget displays the contents of a directory or file.""" -import re import stat from time import time @@ -22,36 +21,6 @@ from . import Widget from .pager import Pager from ranger.fsobject import BAD_INFO -# Don't even try to preview files which mach this regular expression: -PREVIEW_BLACKLIST = re.compile(r""" - # look at the extension: - \.( - # one character extensions: - [oa] - # media formats: - | avi | [mj]pe?g | mp\d | og[gmv] | wm[av] | mkv | flv - | png | bmp | vob | wav | mpc | flac | divx? | xcf | pdf - # binary files: - | torrent | class | so | img | py[co] | dmg - # containers: - | iso | rar | zip | 7z | tar | gz | bz2 | tgz - ) - # ignore filetype-independent suffixes: - (\.part|\.bak|~)? - # ignore fully numerical file extensions: - (\.\d+)*? - $ -""", re.VERBOSE | re.IGNORECASE) - -PREVIEW_WHITELIST = re.compile(r""" - \.( - txt | py | c - ) - # ignore filetype-independent suffixes: - (\.part|\.bak|~)? - $ -""", re.VERBOSE | re.IGNORECASE) - class BrowserColumn(Pager): main_column = False display_infostring = False @@ -127,7 +96,7 @@ class BrowserColumn(Pager): return False if self.target.is_file: - if not self._preview_this_file(self.target): + if not self.target.has_preview(): return False if self.target.is_directory: @@ -173,43 +142,28 @@ class BrowserColumn(Pager): self.need_redraw = False self.last_redraw_time = time() - def _preview_this_file(self, target): - if not (target \ - and self.settings.preview_files \ - and target.is_file \ - and target.accessible \ - and target.stat \ - and not target.is_device \ - and not target.stat.st_mode & stat.S_IFIFO): - return False - - if PREVIEW_WHITELIST.search(target.basename): - return True - if PREVIEW_BLACKLIST.search(target.basename): - return False - if target.is_binary(): - return False - return True - def _draw_file(self): """Draw a preview of the file, if the settings allow it""" self.win.move(0, 0) if not self.target.accessible: - self.win.addnstr("not accessible", self.wid) + self.addnstr("not accessible", self.wid) Pager.close(self) return - if not self._preview_this_file(self.target): + if self.target is None or not self.target.has_preview(): Pager.close(self) return try: - f = open(self.target.path, 'r') + f = self.target.get_preview_source() except: Pager.close(self) else: - self.set_source(f) - Pager.draw(self) + if f is None: + Pager.close(self) + else: + self.set_source(f) + Pager.draw(self) def _draw_directory(self): """Draw the contents of a directory""" @@ -223,7 +177,7 @@ class BrowserColumn(Pager): if not self.target.content_loaded: self.color(base_color) - self.win.addnstr("...", self.wid) + self.addnstr("...", self.wid) self.color_reset() return @@ -232,13 +186,13 @@ class BrowserColumn(Pager): if not self.target.accessible: self.color(base_color, 'error') - self.win.addnstr("not accessible", self.wid) + self.addnstr("not accessible", self.wid) self.color_reset() return if self.target.empty(): self.color(base_color, 'empty') - self.win.addnstr("empty", self.wid) + self.addnstr("empty", self.wid) self.color_reset() return @@ -295,27 +249,22 @@ class BrowserColumn(Pager): this_color.append(drawn.exists and 'good' or 'bad') string = drawn.basename - try: - if self.main_column: - if tagged: - self.win.addnstr(line, 0, text, self.wid - 2) - elif self.wid > 1: - self.win.addnstr(line, 1, text, self.wid - 2) - else: - self.win.addnstr(line, 0, text, self.wid) - - if self.display_infostring and drawn.infostring \ - and self.settings.display_size_in_main_column: - info = drawn.infostring - x = self.wid - 1 - len(info) - if info is BAD_INFO: - bad_info_color = (x, len(str(info))) - if x > 0: - self.win.addstr(line, x, str(info) + ' ') - except: - # the drawing of the last string will cause an error - # because ncurses tries to move out of the bounds - pass + if self.main_column: + if tagged: + self.addnstr(line, 0, text, self.wid - 2) + elif self.wid > 1: + self.addnstr(line, 1, text, self.wid - 2) + else: + self.addnstr(line, 0, text, self.wid) + + if self.display_infostring and drawn.infostring \ + and self.settings.display_size_in_main_column: + info = drawn.infostring + x = self.wid - 1 - len(info) + if info is BAD_INFO: + bad_info_color = (x, len(str(info))) + if x > 0: + self.addstr(line, x, str(info) + ' ') self.color_at(line, 0, self.wid, this_color) if bad_info_color: |