def initialize(self): """If ui/bookmarks are None, they will be initialized here.""" if self.bookmarks is None: if ranger.arg.clean: bookmarkfile = None else: bookmarkfile = self.confpath('bookmarks') self.bookmarks = Bookmarks( bookmarkfile=bookmarkfile, bookmarktype=Directory, autosave=self.settings.autosave_bookmarks) self.bookmarks.load() else: self.bookmarks = bookmarks if not ranger.arg.clean and self.tags is None: self.tags = Tags(self.confpath('tagged')) if self.ui is None: self.ui = DefaultUI() self.ui.initialize() def mylogfunc(text): self.notify(text, bad=True) self.run = Runner(ui=self.ui, apps=self.apps, logfunc=mylogfunc) self.env.signal_bind('cd', self._update_current_tab)
def initialize(self): """If ui/bookmarks are None, they will be initialized here.""" self.tabs = dict( (n + 1, Tab(path)) for n, path in enumerate(self.start_paths)) tab_list = self._get_tab_list() if tab_list: self.current_tab = tab_list[0] self.thistab = self.tabs[self.current_tab] else: self.current_tab = 1 self.tabs[self.current_tab] = self.thistab = Tab('.') if not ranger.arg.clean and os.path.isfile( self.confpath('rifle.conf')): rifleconf = self.confpath('rifle.conf') else: rifleconf = self.relpath('config/rifle.conf') self.rifle = Rifle(rifleconf) self.rifle.reload_config() if self.bookmarks is None: if ranger.arg.clean: bookmarkfile = None else: bookmarkfile = self.confpath('bookmarks') self.bookmarks = Bookmarks( bookmarkfile=bookmarkfile, bookmarktype=Directory, autosave=self.settings.autosave_bookmarks) self.bookmarks.load() if not ranger.arg.clean and self.tags is None: self.tags = Tags(self.confpath('tagged')) self.ui.setup_curses() self.ui.initialize() self.rifle.hook_before_executing = lambda a, b, flags: \ self.ui.suspend() if 'f' not in flags else None self.rifle.hook_after_executing = lambda a, b, flags: \ self.ui.initialize() if 'f' not in flags else None self.rifle.hook_logger = self.notify # This hook allows image viewers to open all images in the current # directory, keeping the order of files the same as in ranger. # The requirements to use it are: # 1. set open_all_images to true # 2. ensure no files are marked # 3. call rifle with a command that starts with "sxiv " or "feh " def sxiv_workaround_hook(command): import re from ranger.ext.shell_escape import shell_quote if self.settings.open_all_images and \ len(self.thisdir.marked_items) == 0 and \ re.match(r'^(feh|sxiv) ', command): images = [f.basename for f in self.thisdir.files if f.image] escaped_filenames = " ".join(shell_quote(f) \ for f in images if "\x00" not in f) if images and self.thisfile.basename in images and \ "$@" in command: new_command = None if command[0:5] == 'sxiv ': number = images.index(self.thisfile.basename) + 1 new_command = command.replace("sxiv ", "sxiv -n %d " % number, 1) if command[0:4] == 'feh ': new_command = command.replace("feh ", "feh --start-at %s " % \ shell_quote(self.thisfile.basename), 1) if new_command: command = "set -- %s; %s" % (escaped_filenames, new_command) return command self.rifle.hook_command_preprocessing = sxiv_workaround_hook def mylogfunc(text): self.notify(text, bad=True) self.run = Runner(ui=self.ui, logfunc=mylogfunc, fm=self)
def initialize(self): """If ui/bookmarks are None, they will be initialized here.""" self.tabs = dict( (n + 1, Tab(path)) for n, path in enumerate(self.start_paths)) tab_list = self.get_tab_list() if tab_list: self.current_tab = tab_list[0] self.thistab = self.tabs[self.current_tab] else: self.current_tab = 1 self.tabs[self.current_tab] = self.thistab = Tab('.') if not ranger.args.clean and os.path.isfile( self.confpath('rifle.conf')): rifleconf = self.confpath('rifle.conf') else: rifleconf = self.relpath('config/rifle.conf') self.rifle = Rifle(rifleconf) self.rifle.reload_config() def set_image_displayer(): self.image_displayer = self._get_image_displayer() set_image_displayer() self.settings.signal_bind('setopt.preview_images_method', set_image_displayer, priority=settings.SIGNAL_PRIORITY_AFTER_SYNC) self.settings.signal_bind( 'setopt.preview_images', lambda signal: signal.fm.previews.clear(), ) if ranger.args.clean: self.tags = TagsDummy("") elif self.tags is None: self.tags = Tags(self.datapath('tagged')) if self.bookmarks is None: if ranger.args.clean: bookmarkfile = None else: bookmarkfile = self.datapath('bookmarks') self.bookmarks = Bookmarks( bookmarkfile=bookmarkfile, bookmarktype=Directory, autosave=self.settings.autosave_bookmarks) self.bookmarks.load() self.bookmarks.enable_saving_backtick_bookmark( self.settings.save_backtick_bookmark) self.ui.setup_curses() self.ui.initialize() self.rifle.hook_before_executing = lambda a, b, flags: \ self.ui.suspend() if 'f' not in flags else None self.rifle.hook_after_executing = lambda a, b, flags: \ self.ui.initialize() if 'f' not in flags else None self.rifle.hook_logger = self.notify old_preprocessing_hook = self.rifle.hook_command_preprocessing # This hook allows image viewers to open all images in the current # directory, keeping the order of files the same as in ranger. # The requirements to use it are: # 1. set open_all_images to true # 2. ensure no files are marked # 3. call rifle with a command that starts with "sxiv " or "feh " def sxiv_workaround_hook(command): import re from ranger.ext.shell_escape import shell_quote if self.settings.open_all_images and \ not self.thisdir.marked_items and \ re.match(r'^(feh|sxiv|imv|pqiv) ', command): images = [ f.relative_path for f in self.thisdir.files if f.image ] escaped_filenames = " ".join( shell_quote(f) for f in images if "\x00" not in f) if images and self.thisfile.relative_path in images and \ "$@" in command: new_command = None if command[0:5] == 'sxiv ': number = images.index(self.thisfile.relative_path) + 1 new_command = command.replace("sxiv ", "sxiv -n %d " % number, 1) if command[0:4] == 'feh ': new_command = command.replace( "feh ", "feh --start-at %s " % shell_quote(self.thisfile.relative_path), 1, ) if command[0:4] == 'imv ': number = images.index(self.thisfile.relative_path) + 1 new_command = command.replace("imv ", "imv -n %d " % number, 1) if command[0:5] == 'pqiv ': number = images.index(self.thisfile.relative_path) new_command = command.replace( "pqiv ", "pqiv --action \"goto_file_byindex(%d)\" " % number, 1) if new_command: command = "set -- %s; %s" % (escaped_filenames, new_command) return old_preprocessing_hook(command) self.rifle.hook_command_preprocessing = sxiv_workaround_hook def mylogfunc(text): self.notify(text, bad=True) self.run = Runner(ui=self.ui, logfunc=mylogfunc, fm=self) self.settings.signal_bind( 'setopt.metadata_deep_search', lambda signal: setattr( signal.fm.metadata, 'deep_search', signal.value)) self.settings.signal_bind( 'setopt.save_backtick_bookmark', lambda signal: signal.fm.bookmarks .enable_saving_backtick_bookmark(signal.value))
def main(): """initialize objects and run the filemanager""" import locale import os.path import ranger from ranger.core.shared import (EnvironmentAware, FileManagerAware, SettingsAware) from ranger.core.fm import FM try: locale.setlocale(locale.LC_ALL, '') except: print("Warning: Unable to set locale. Expect encoding problems.") if not 'SHELL' in os.environ: os.environ['SHELL'] = 'bash' ranger.arg = arg = parse_arguments() if arg.copy_config is not None: fm = FM() fm.copy_config_files(arg.copy_config) return 1 if arg.fail_unless_cd else 0 SettingsAware._setup(clean=arg.clean) targets = arg.targets or ['.'] target = targets[0] if arg.targets: if target.startswith('file://'): target = target[7:] if not os.access(target, os.F_OK): print("File or directory doesn't exist: %s" % target) return 1 elif os.path.isfile(target): def print_function(string): print(string) from ranger.core.runner import Runner from ranger.fsobject import File runner = Runner(logfunc=print_function) load_apps(runner, arg.clean) runner(files=[File(target)], mode=arg.mode, flags=arg.flags) return 1 if arg.fail_unless_cd else 0 crash_traceback = None try: # Initialize objects from ranger.core.environment import Environment fm = FM() FileManagerAware.fm = fm EnvironmentAware.env = Environment(target) fm.tabs = dict((n+1, os.path.abspath(path)) for n, path \ in enumerate(targets[:9])) load_settings(fm, arg.clean) if fm.env.username == 'root': fm.settings.preview_files = False fm.settings.use_preview_script = False if not arg.debug: from ranger.ext import curses_interrupt_handler curses_interrupt_handler.install_interrupt_handler() # Run the file manager fm.initialize() fm.ui.initialize() fm.loop() except Exception: import traceback crash_traceback = traceback.format_exc() except SystemExit as error: return error.args[0] finally: if crash_traceback: filepath = fm.env.cf.path if fm.env.cf else "None" try: fm.ui.destroy() except (AttributeError, NameError): pass if crash_traceback: print("Ranger version: %s, executed with python %s" % (ranger.__version__, sys.version.split()[0])) print("Locale: %s" % '.'.join(str(s) for s in locale.getlocale())) print("Current file: %s" % filepath) print(crash_traceback) print("Ranger crashed. " \ "Please report this traceback at:") print("http://savannah.nongnu.org/bugs/?group=ranger&func=additem") return 1 return 0