diff options
author | hut <hut@lavabit.com> | 2009-11-28 16:23:54 +0100 |
---|---|---|
committer | hut <hut@lavabit.com> | 2009-11-28 16:23:54 +0100 |
commit | 419e4aa53fc99c29dce66cd46b7f894efd2d57a8 (patch) | |
tree | e547103e4b71fd63fc57d5ae0c2f98c3d7e0525a /ranger | |
parent | a66c4a26f79d71210422f7d977b892d5a700dae8 (diff) | |
download | ranger-419e4aa53fc99c29dce66cd46b7f894efd2d57a8.tar.gz |
blub
Diffstat (limited to 'ranger')
-rw-r--r-- | ranger/color.py | 26 | ||||
-rw-r--r-- | ranger/directory.py | 74 | ||||
-rw-r--r-- | ranger/environment.py | 17 | ||||
-rw-r--r-- | ranger/fm.py | 32 | ||||
-rw-r--r-- | ranger/fsobject.py | 4 | ||||
-rw-r--r-- | ranger/keys.py | 28 | ||||
-rw-r--r-- | ranger/options.py | 2 | ||||
-rw-r--r-- | ranger/ui.py | 3 | ||||
-rw-r--r-- | ranger/wdisplay.py | 47 | ||||
-rw-r--r-- | ranger/widget.py | 14 | ||||
-rw-r--r-- | ranger/wtitlebar.py | 3 |
11 files changed, 214 insertions, 36 deletions
diff --git a/ranger/color.py b/ranger/color.py new file mode 100644 index 00000000..5701c0ee --- /dev/null +++ b/ranger/color.py @@ -0,0 +1,26 @@ +#import curses + +color_pairs = {10: 0} + + +#class ColorScheme(): +# def isdir + + +#def get_color(fg, bg): +# c = bg+2 + 9*(fg + 2) +# try: +# return color_pairs[c] +# except KeyError: +# size = len(color_pairs) +# curses.init_pair(size, curses.COLOR_RED, curses.COLOR_WHITE) +# color_pairs[c] = size +# return color_pairs[c] +# +#def color(fg = -1, bg = -1, attribute = 0): +# pass +## prin +# curses.attrset(attribute | curses.color_pair(get_color(fg, bg))) + +#color(-1, -1) +#print(color_pairs) diff --git a/ranger/directory.py b/ranger/directory.py index 665f9f7d..2cad7ca8 100644 --- a/ranger/directory.py +++ b/ranger/directory.py @@ -1,14 +1,25 @@ import ranger.fsobject from ranger import file, debug +from ranger.fsobject import FSObject as SuperClass + +def sort_by_basename(path): + return path.basename + +def sort_by_directory(path): + return -int( isinstance( path, Directory ) ) + class NoDirectoryGiven(Exception): pass -class Directory(ranger.fsobject.FSObject): +class Directory(SuperClass): def __init__(self, path): from os.path import isdir - if not isdir(path): raise NoDirectoryGiven() - ranger.fsobject.FSObject.__init__(self, path) + + if not isdir(path): + raise NoDirectoryGiven() + + SuperClass.__init__(self, path) self.content_loaded = False self.scheduled = False self.enterable = False @@ -19,8 +30,13 @@ class Directory(ranger.fsobject.FSObject): self.pointed_index = None self.pointed_file = None self.scroll_begin = 0 + self.show_hidden = False + self.directories_first = True + + # to find out if something has changed: self.old_show_hidden = self.show_hidden + self.old_directories_first = None #self.directories_first def load_content(self): from os.path import join, isdir, basename @@ -49,23 +65,43 @@ class Directory(ranger.fsobject.FSObject): f.load() files.append(f) - files.sort(key = lambda x: x.basename) self.files = files + self.old_directories_first = None +# self.sort() if len(self.files) > 0: if self.pointed_file is not None: self.move_pointer_to_file_path(self.pointed_file) - if self.pointed_file is None: - self.move_pointer(absolute = 0) +# if self.pointed_file is None: +# self.correct_pointer() else: self.filenames = None self.files = None - self.infostring = ranger.fsobject.FSObject.BAD_INFO + self.infostring = superclass.BAD_INFO + + def sort(self): + old_pointed_file = self.pointed_file + self.files.sort(key = sort_by_basename) + + if self.directories_first: + self.files.sort(key = sort_by_directory) + + if self.pointed_index is not None: + self.move_pointer_to_file_path(old_pointed_file) + else: + self.correct_pointer() + + self.old_directories_first = self.directories_first + def sort_if_outdated(self): + if self.old_directories_first != self.directories_first: + self.sort() + # Notice: fm.env.cf should always point to the current file. If you # modify the current directory with this function, make sure # to update fm.env.cf aswell. def move_pointer(self, relative=0, absolute=None): + if self.empty(): return i = self.pointed_index if isinstance(absolute, int): if absolute < 0: @@ -85,6 +121,8 @@ class Directory(ranger.fsobject.FSObject): except AttributeError: pass self.load_content_once() + if self.empty(): return + i = 0 for f in self.files: if f.path == path: @@ -94,16 +132,21 @@ class Directory(ranger.fsobject.FSObject): return False def correct_pointer(self): - i = self.pointed_index + """make sure the pointer is in the valid range of 0 : len(self.files)-1 (or None if directory is empty.)""" - if i >= len(self.files): - i = len(self.files) - 1 + if self.files is None or len(self.files) == 0: + self.pointed_index = None + self.pointed_file = None - if i < 0: - i = 0 + else: + i = self.pointed_index - self.pointed_index = i - self.pointed_file = self[i] + if i is None: i = 0 + if i >= len(self.files): i = len(self.files) - 1 + if i < 0: i = 0 + + self.pointed_index = i + self.pointed_file = self[i] def load_content_once(self): if not self.content_loaded: @@ -128,6 +171,9 @@ class Directory(ranger.fsobject.FSObject): return True return False + def empty(self): + return self.files is None or len(self.files) == 0 + def __len__(self): if not self.accessible: raise ranger.fsobject.NotLoadedYet() return len(self.files) diff --git a/ranger/environment.py b/ranger/environment.py index d6635e9f..4b53d25a 100644 --- a/ranger/environment.py +++ b/ranger/environment.py @@ -30,6 +30,8 @@ class Environment(): else: try: return self.directories[self.cf.path] + except AttributeError: + return None except KeyError: return self.cf @@ -48,17 +50,17 @@ class Environment(): last_path = None for path in reversed(self.pathway): - if not last_path: - last_path = path.path + if last_path is None: + last_path = path continue -# log(( path.path, last_path )) path.move_pointer_to_file_path(last_path) - last_path = path.path + last_path = path def enter_dir(self, path): + from os.path import normpath, join, expanduser # get the absolute path - path = os.path.normpath(os.path.join(self.path, path)) + path = normpath(join(self.path, expanduser(path))) try: new_pwd = self.get_directory(path) @@ -85,5 +87,10 @@ class Environment(): self.assign_correct_cursor_positions() # set the current file. + self.pwd.directories_first = self.opt['directories_first'] + self.pwd.sort_if_outdated() self.cf = self.pwd.pointed_file + from ranger.debug import log + log(self.cf) + return True diff --git a/ranger/fm.py b/ranger/fm.py index 141414b0..8c6c7f07 100644 --- a/ranger/fm.py +++ b/ranger/fm.py @@ -1,3 +1,6 @@ +from os import devnull +null = open(devnull, 'a') + class FM(): def __init__(self, environment): self.env = environment @@ -26,19 +29,29 @@ class FM(): def exit(self): raise SystemExit() + def enter_dir(self, path): + self.env.enter_dir(path) + def move_left(self): self.env.enter_dir('..') def move_right(self): - path = self.env.cf.path - if not self.env.enter_dir(path): - self.execute_file(path) + try: + path = self.env.cf.path + if not self.env.enter_dir(path): + self.execute_file(path) + except AttributeError: + pass + +# def execute_file(self, path): +# import os +# self.ui.exit() +# os.system("mplayer '" + path + "'") +# self.ui.initialize() def execute_file(self, path): - import os - self.ui.exit() - os.system("mplayer '" + path + "'") - self.ui.initialize() + from subprocess import Popen + Popen(('mplayer', '-fs', path), stdout = null, stderr = null) def move_pointer(self, relative = 0, absolute = None): self.env.cf = self.env.pwd.move_pointer(relative, absolute) @@ -50,6 +63,11 @@ class FM(): def redraw(self): self.ui.redraw() + def reset(self): + old_path = self.env.pwd.path + self.env.directories = {} + self.enter_dir(old_path) + def toggle_boolean_option(self, string): if isinstance(self.env.opt[string], bool): self.env.opt[string] ^= True diff --git a/ranger/fsobject.py b/ranger/fsobject.py index 099d3c5c..91c73bfc 100644 --- a/ranger/fsobject.py +++ b/ranger/fsobject.py @@ -43,7 +43,8 @@ class FSObject(object): if os.path.isdir(self.path): self.type = ranger.fstype.Directory try: - self.infostring = ' %d' % len(os.listdir(self.path)) + self.size = len(os.listdir(self.path)) + self.infostring = ' %d' % self.size self.runnable = True except OSError: self.infostring = FSObject.BAD_INFO @@ -51,6 +52,7 @@ class FSObject(object): self.accessible = False elif os.path.isfile(self.path): self.type = ranger.fstype.File + self.size = self.stat.st_size self.infostring = ' %d' % self.stat.st_size else: self.type = ranger.fstype.Unknown diff --git a/ranger/keys.py b/ranger/keys.py index b120c507..c9b817e3 100644 --- a/ranger/keys.py +++ b/ranger/keys.py @@ -22,6 +22,9 @@ def initialize_commands(command_list): def toggle_option(string): return lambda fm: fm.toggle_boolean_option(string) + def cd(path): + return lambda fm: fm.enter_dir(path) + cl.bind(FM.move_left, 'h', 195, 'back') cl.bind(FM.move_right, 'l', 'forward') cl.bind(move( relative = 1 ), 'j') @@ -33,7 +36,32 @@ def initialize_commands(command_list): cl.bind(toggle_option('show_hidden'), 'th') + + gX = { + 'h': '~', + 'e': '/etc', + 'u': '/usr', + 'r': '/', + 'm': '/media', + 'n': '/mnt', + 't': '~/.trash', + 's': '/srv', + } + + for x, path in gX.items(): + cl.bind( cd(path), 'g' + x ) + +# cl.bind(cd("~"), 'gh') +# cl.bind(cd("/etc"), 'ge') +# cl.bind(cd("/usr"), 'gu') +# cl.bind(cd("/"), 'gr') +# cl.bind(cd("/media"), 'gm') +# cl.bind(cd("/mnt"), 'gn') +# cl.bind(cd("~/.trash"), 'gt') +# cl.bind(cd("/srv"), 'gs') + cl.bind(FM.exit, 'q', ctrl('D'), 'ZZ') + cl.bind(FM.reset, ctrl('R')) cl.bind(FM.redraw, ctrl('L')) cl.bind(FM.resize, curses.KEY_RESIZE) diff --git a/ranger/options.py b/ranger/options.py index 50957c8a..92e38c8d 100644 --- a/ranger/options.py +++ b/ranger/options.py @@ -4,4 +4,4 @@ def get(): def dummy(): """ provide a way of getting options until get() is implemented """ - return { 'show_hidden': False, 'scroll_offset': 2 } + return { 'show_hidden': False, 'scroll_offset': 2, 'directories_first': True, 'preview_files' : True } diff --git a/ranger/ui.py b/ranger/ui.py index 9e8acb73..6f98364b 100644 --- a/ranger/ui.py +++ b/ranger/ui.py @@ -18,6 +18,8 @@ class UI(): curses.noecho() curses.halfdelay(10) curses.curs_set(0) + curses.start_color() + curses.use_default_colors() def setup(self): pass @@ -63,6 +65,7 @@ class UI(): widg.feed_env(self.env) widg.draw() self.win.refresh() +# log(self.env.cf) def get_next_key(self): key = self.win.getch() diff --git a/ranger/wdisplay.py b/ranger/wdisplay.py index 55c0c563..e5229c7d 100644 --- a/ranger/wdisplay.py +++ b/ranger/wdisplay.py @@ -1,10 +1,13 @@ import ranger.widget from ranger.debug import log +from ranger.color import color_pairs import curses +from ranger.widget import Widget as SuperClass +#from ranger.color import color -class WDisplay(ranger.widget.Widget): +class WDisplay(SuperClass): def __init__(self, win, level): - ranger.widget.Widget.__init__(self,win) + SuperClass.__init__(self,win) self.level = level self.main_display = False self.display_infostring = False @@ -14,6 +17,8 @@ class WDisplay(ranger.widget.Widget): self.target = env.at_level(self.level) self.show_hidden = env.opt['show_hidden'] self.scroll_offset = env.opt['scroll_offset'] + self.directories_first = env.opt['directories_first'] + self.preview_files = env.opt['preview_files'] def draw(self): from ranger.file import File @@ -32,28 +37,57 @@ class WDisplay(ranger.widget.Widget): if not self.target.accessible: self.win.addnstr(self.y, self.x, "not accessible", self.wid) return - self.win.addnstr(self.y, self.x, "this is a file.", self.wid) + + if self.preview_files: + try: + if self.target.size < 1024 * 20: + f = open(self.target.path, 'r') + for line in range(self.hei): + read = f.readline().expandtabs() + self.win.addnstr(self.y + line, self.x, read, self.wid) + except: + pass + + else: + self.win.addnstr(self.y, self.x, "this is a file.", self.wid) def draw_directory(self): + from ranger.directory import Directory self.target.show_hidden = self.show_hidden self.target.load_content_if_outdated() + self.target.directories_first = self.directories_first + self.target.sort_if_outdated() main_display = self.main_display + if self.target.empty(): + self.color(bg=1) + self.win.addnstr(self.y, self.x, "empty", self.wid) + self.color() + return + if not self.target.accessible: self.win.addnstr(self.y, self.x, "not accessible", self.wid) return +# log(color_pairs) + self.set_scroll_begin() + selected_i = self.target.pointed_index for line in range(self.hei): i = line + self.scroll_begin # last file reached? try: drawed = self.target[i] except IndexError: break - invert = i == self.target.pointed_index + if isinstance(drawed, Directory): + self.color(fg = 4) + else: + self.color() + + invert = i == selected_i if invert: - self.win.attrset(curses.A_REVERSE) + self.win.attron(curses.A_REVERSE) if self.main_display: self.win.addnstr( @@ -73,8 +107,7 @@ class WDisplay(ranger.widget.Widget): x = self.x + self.wid - 1 - len(info) if x > self.x: self.win.addstr(self.y + line, x, str(info) + ' ') - if invert: - self.win.attrset(curses.A_NORMAL) + self.win.attrset(0) def get_scroll_begin(self): offset = self.scroll_offset diff --git a/ranger/widget.py b/ranger/widget.py index 725810ec..e07db7a8 100644 --- a/ranger/widget.py +++ b/ranger/widget.py @@ -1,4 +1,5 @@ import curses +from ranger.color import color_pairs class OutOfBoundsException(Exception): pass @@ -7,6 +8,19 @@ class Widget(): self.win = win self.setdim(0, 0, 0, 0) + def get_color(self, fg, bg): + c = bg+2 + 9*(fg + 2) + try: + return color_pairs[c] + except KeyError: + size = len(color_pairs) + curses.init_pair(size, fg, bg) + color_pairs[c] = size + return color_pairs[c] + + def color(self, fg = -1, bg = -1, attr = 0): + self.win.attrset(attr | curses.color_pair(self.get_color(fg, bg))) + def setdim(self, y, x, hei=None, wid=None): maxy, maxx = self.win.getmaxyx() wid = wid or maxx - x diff --git a/ranger/wtitlebar.py b/ranger/wtitlebar.py index e5cdb313..231914e9 100644 --- a/ranger/wtitlebar.py +++ b/ranger/wtitlebar.py @@ -1,7 +1,8 @@ import curses import ranger.widget +from ranger.widget import Widget as SuperClass -class WTitleBar(ranger.widget.Widget): +class WTitleBar(SuperClass): def feed_env(self, env): self.pathway = env.pathway |