From e92fe17a449cad78c79a905fd53ec73f8a41051c Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 24 Dec 2009 14:34:36 +0100 Subject: implemented sorting --- TODO | 6 ++++-- ranger/actions.py | 7 +++++++ ranger/defaults/keys.py | 19 ++++++++++++++++++- ranger/defaults/options.py | 2 ++ ranger/fsobject/directory.py | 26 ++++++++++++++++++++++++-- ranger/shared/settings.py | 3 ++- 6 files changed, 57 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index 45d35acb..b1bad60a 100644 --- a/TODO +++ b/TODO @@ -11,9 +11,11 @@ General (X) #5 09/12/06 move code from fm into objects (X) #6 09/12/06 move main to __init__ - ( ) #7 09/12/06 cooler titlebar + (X) #7 09/12/06 cooler titlebar + ( ) #9 09/12/24 add a widget for managing running operations + (X) #10 09/12/24 sorting Filesystem Modification Operations - ( ) #8 09/12/17 Add operations to modify files/directories + (X) #8 09/12/17 Add operations to modify files/directories diff --git a/ranger/actions.py b/ranger/actions.py index e2583591..6c597615 100644 --- a/ranger/actions.py +++ b/ranger/actions.py @@ -153,6 +153,13 @@ class Actions(EnvironmentAware, SettingsAware): """Toggle a boolean option named """ if isinstance(self.env.settings[string], bool): self.env.settings[string] ^= True + + def sort(self, func=None, reverse=None): + if reverse is not None: + self.env.settings['reverse'] = bool(reverse) + + if func is not None: + self.env.settings['sort'] = str(func) def force_load_preview(self): cf = self.env.cf diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py index 68294b22..2cbad0d4 100644 --- a/ranger/defaults/keys.py +++ b/ranger/defaults/keys.py @@ -35,7 +35,7 @@ def initialize_commands(command_list): bind('J', do('move_pointer_by_pages', 0.5)) bind('K', do('move_pointer_by_pages', -0.5)) bind('E', do('edit_file')) - bind('o', do('force_load_preview')) +# bind('o', do('force_load_preview')) bind(' ', do('mark', toggle=True)) bind('v', do('mark', all=True, toggle=True)) @@ -53,6 +53,23 @@ def initialize_commands(command_list): bind('ta', do('toggle_boolean_option', 'auto_load_preview')) bind('tc', do('toggle_boolean_option', 'collapse_preview')) + sort_hint = "//s//ize //b//ase//n//ame //m//time //t//ype //r//everse" + sort_dict = { + 's': 'size', + 'b': 'basename', + 'n': 'basename', + 'm': 'mtime', + 't': 'type', + } + for key, val in sort_dict.items(): + for key, is_upper in ((key.lower(), False), (key.upper(), True)): + # reverse if any of the two letters is capital + bind('o' + key, do('sort', func=val, reverse=is_upper)) + bind('O' + key, do('sort', func=val, reverse=True)) + bind('or', 'Or', 'oR', 'OR', lambda fm, n: \ + fm.sort(reverse=not fm.settings.reverse)) + command_list.hint(sort_hint, 'o', 'O') + bind('cd', do('open_console', ':', 'cd ')) bind('f', do('open_console', '>', 'find ')) diff --git a/ranger/defaults/options.py b/ranger/defaults/options.py index f542f69f..d4172eee 100644 --- a/ranger/defaults/options.py +++ b/ranger/defaults/options.py @@ -13,3 +13,5 @@ max_history_size = 20 auto_load_preview = True max_dirsize_for_autopreview = None +sort = 'basename' +reverse = False diff --git a/ranger/fsobject/directory.py b/ranger/fsobject/directory.py index 66d99464..f91f99d9 100644 --- a/ranger/fsobject/directory.py +++ b/ranger/fsobject/directory.py @@ -32,6 +32,15 @@ class Directory(SuperClass, SettingsAware): old_show_hidden = None old_directories_first = None + old_reverse = None + old_sort = None + + sort_dict = { + 'basename': sort_by_basename, + 'size': lambda path: path.size, + 'mtime': lambda path: -(path.stat and path.stat.st_mtime or 1), + 'type': lambda path: path.mimetype, + } def __init__(self, path): from os.path import isfile @@ -46,6 +55,8 @@ class Directory(SuperClass, SettingsAware): # to find out if something has changed: self.old_show_hidden = self.settings.show_hidden self.old_directories_first = self.settings.directories_first + self.old_sort = self.settings.sort + self.old_reverse = self.settings.reverse def mark_item(self, item, val): item._mark(val) @@ -189,7 +200,14 @@ class Directory(SuperClass, SettingsAware): return old_pointed_file = self.pointed_file - self.files.sort(key = sort_by_basename) + try: + sort_func = self.sort_dict[self.settings.sort] + except: + sort_func = sort_by_basename + self.files.sort(key = sort_func) + + if self.settings.reverse: + self.files.reverse() if self.settings.directories_first: self.files.sort(key = sort_by_directory) @@ -200,10 +218,14 @@ class Directory(SuperClass, SettingsAware): self.correct_pointer() self.old_directories_first = self.settings.directories_first + self.old_sort = self.settings.sort + self.old_reverse = self.settings.reverse def sort_if_outdated(self): """Sort the containing files if they are outdated""" - if self.old_directories_first != self.settings.directories_first: + if self.old_directories_first != self.settings.directories_first \ + or self.old_sort != self.settings.sort \ + or self.old_reverse != self.settings.reverse: self.sort() # Notice: fm.env.cf should always point to the current file. If you diff --git a/ranger/shared/settings.py b/ranger/shared/settings.py index 0132ac97..ec7abedf 100644 --- a/ranger/shared/settings.py +++ b/ranger/shared/settings.py @@ -1,5 +1,6 @@ ALLOWED_SETTINGS = """ -show_hidden scroll_offset directories_first +show_hidden scroll_offset +directories_first sort reverse preview_files max_history_size colorscheme collapse_preview auto_load_preview max_dirsize_for_autopreview -- cgit 1.4.1-2-gfad0