From d99291d8395042ade1337d025a69eaccd191bb54 Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 3 Dec 2011 00:27:10 +0100 Subject: fsobject.fsobject: Properly escape control characters --- ranger/fsobject/fsobject.py | 11 +++++++++++ ranger/gui/widgets/browsercolumn.py | 2 +- ranger/gui/widgets/titlebar.py | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ranger/fsobject/fsobject.py b/ranger/fsobject/fsobject.py index 51e49808..1d1f202d 100644 --- a/ranger/fsobject/fsobject.py +++ b/ranger/fsobject/fsobject.py @@ -26,6 +26,12 @@ from ranger.ext.spawn import spawn from ranger.ext.lazy_property import lazy_property from ranger.ext.human_readable import human_readable +if hasattr(str, 'maketrans'): + maketrans = str.maketrans +else: + from string import maketrans +unsafe_chars = '\n' + ''.join(map(chr, range(32))) + ''.join(map(chr, range(128, 256))) +safe_string_table = maketrans(unsafe_chars, '?' * len(unsafe_chars)) _extract_number_re = re.compile(r'([^0-9]?)(\d*)') class FileSystemObject(FileManagerAware): @@ -106,6 +112,11 @@ class FileSystemObject(FileManagerAware): return [c if i % 3 == 1 else (int(c) if c else 0) for i, c in \ enumerate(_extract_number_re.split(self.basename_lower))] + @lazy_property + def safe_basename(self): + return self.basename.translate(safe_string_table) + + for attr in ('video', 'audio', 'image', 'media', 'document', 'container'): exec("%s = lazy_property(" "lambda self: self.set_mimetype() or self.%s)" % (attr, attr)) diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py index b6b745aa..91633d4e 100644 --- a/ranger/gui/widgets/browsercolumn.py +++ b/ranger/gui/widgets/browsercolumn.py @@ -220,7 +220,7 @@ class BrowserColumn(Pager): bad_info_color = None this_color = base_color + list(drawn.mimetype_tuple) - text = drawn.basename + text = drawn.safe_basename tagged = self.fm.tags and drawn.realpath in self.fm.tags if tagged: diff --git a/ranger/gui/widgets/titlebar.py b/ranger/gui/widgets/titlebar.py index 3ca3f80d..ddb27f2c 100644 --- a/ranger/gui/widgets/titlebar.py +++ b/ranger/gui/widgets/titlebar.py @@ -124,11 +124,11 @@ class TitleBar(Widget): else: clr = 'directory' - bar.add(path.basename, clr, directory=path) + bar.add(path.safe_basename, clr, directory=path) bar.add('/', clr, fixed=True, directory=path) if self.env.cf is not None: - bar.add(self.env.cf.basename, 'file') + bar.add(self.env.cf.safe_basename, 'file') def _get_right_part(self, bar): # TODO: fix that pressed keys are cut off when chaining CTRL keys -- cgit 1.4.1-2-gfad0