diff options
author | hut <hut@lavabit.com> | 2009-12-02 14:29:10 +0100 |
---|---|---|
committer | hut <hut@lavabit.com> | 2009-12-02 14:29:10 +0100 |
commit | e6dfc442a984bc50a70be5017f4ab9a0e6efadef (patch) | |
tree | f2cf064015ca93b6070c5253378af0826ce16937 /ranger/gui | |
parent | 09bba8ddbe8af1cd6bf7dede1c6c7c53a01fc2a8 (diff) | |
download | ranger-e6dfc442a984bc50a70be5017f4ab9a0e6efadef.tar.gz |
implemented a console (without commands)
Diffstat (limited to 'ranger/gui')
-rw-r--r-- | ranger/gui/defaultui.py | 28 | ||||
-rw-r--r-- | ranger/gui/ui.py | 55 | ||||
-rw-r--r-- | ranger/gui/wconsole.py | 105 | ||||
-rw-r--r-- | ranger/gui/widget.py | 10 |
4 files changed, 165 insertions, 33 deletions
diff --git a/ranger/gui/defaultui.py b/ranger/gui/defaultui.py index d4d89047..547f20ff 100644 --- a/ranger/gui/defaultui.py +++ b/ranger/gui/defaultui.py @@ -1,11 +1,12 @@ -from ranger.gui.ui import UI as SuperClass -from ranger.gui.wdisplay import WDisplay -from ranger.gui.wtitlebar import WTitleBar RATIO = ( 0.15, 0.15, 0.4, 0.3 ) +from ranger.gui.ui import UI as SuperClass class DefaultUI(SuperClass): def setup(self): + from ranger.gui.wdisplay import WDisplay + from ranger.gui.wtitlebar import WTitleBar + from ranger.gui.wconsole import WConsole self.titlebar = WTitleBar(self.win, self.colorscheme) self.add_widget(self.titlebar) @@ -19,9 +20,9 @@ class DefaultUI(SuperClass): self.displays[2].main_display = True for disp in self.displays: self.add_widget(disp) - - def scroll(self, relative): - self.main_display.scroll(relative) + + self.console = WConsole(self.win, self.colorscheme) + self.add_widget(self.console) def resize(self): SuperClass.resize(self) @@ -33,11 +34,24 @@ class DefaultUI(SuperClass): for ratio in RATIO: wid = int(ratio * x) try: - self.displays[i].setdim(1, leftborder, y-1, wid - 1) + self.displays[i].setdim(1, leftborder, y-2, wid - 1) except KeyError: pass leftborder += wid i += 1 self.titlebar.setdim(0, 0, 1, x) + self.console.setdim(y-1, 0, 1, x) + + # ---specials--- + def open_console(self, mode): + if self.console.open(mode): + self.console.on_close = self.close_console + self.console.visible = True + + def close_console(self): + self.console.visible = False + + def scroll(self, relative): + self.main_display.scroll(relative) diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py index a04455ab..79077cbc 100644 --- a/ranger/gui/ui.py +++ b/ranger/gui/ui.py @@ -1,5 +1,22 @@ import curses +class MouseEvent(): + import curses + PRESSED = [ 0, + curses.BUTTON1_PRESSED, + curses.BUTTON2_PRESSED, + curses.BUTTON3_PRESSED, + curses.BUTTON4_PRESSED ] + + def __init__(self, getmouse): + _, self.x, self.y, _, self.bstate = getmouse + + def pressed(self, n): + try: + return (self.bstate & MouseEvent.PRESSED[n]) != 0 + except: + return False + class UI(): def __init__(self, env, commandlist, colorscheme): self.env = env @@ -15,7 +32,7 @@ class UI(): os.environ['ESCDELAY'] = '25' self.win = curses.initscr() - self.win.leaveok(1) + self.win.leaveok(0) self.win.keypad(1) curses.noecho() @@ -51,10 +68,10 @@ class UI(): else: fm.scroll(relative = 3) - def setup(self): - pass + def can(self, attr): + return hasattr(self, attr) - def scroll(self, relative): + def setup(self): pass def resize(self): @@ -74,8 +91,10 @@ class UI(): def press(self, key, fm): self.env.key_append(key) -# from ranger.helper import log -# log(self.env.keybuffer) + for widg in self.widgets: + if widg.focused: + widg.press(key, fm, self.env) + return try: cmd = self.commandlist.paths[self.env.keybuffer] @@ -98,28 +117,14 @@ class UI(): self.win.erase() for widg in self.widgets: widg.feed_env(self.env) - widg.draw() + if widg.visible: + widg.draw() + for widg in self.widgets: + if widg.visible: + widg.finalize() self.win.refresh() def get_next_key(self): key = self.win.getch() curses.flushinp() return key - - -class MouseEvent(): - import curses - PRESSED = [ 0, - curses.BUTTON1_PRESSED, - curses.BUTTON2_PRESSED, - curses.BUTTON3_PRESSED, - curses.BUTTON4_PRESSED ] - - def __init__(self, getmouse): - _, self.x, self.y, _, self.bstate = getmouse - - def pressed(self, n): - try: - return (self.bstate & MouseEvent.PRESSED[n]) != 0 - except: - return False diff --git a/ranger/gui/wconsole.py b/ranger/gui/wconsole.py new file mode 100644 index 00000000..4411e83b --- /dev/null +++ b/ranger/gui/wconsole.py @@ -0,0 +1,105 @@ +from ranger.gui.widget import Widget as SuperClass +import curses + +CONSOLE_MODES = tuple(':/?>!') + +class WConsole(SuperClass): + def __init__(self, win, colorscheme): + from ranger.command import CommandList + from ranger.conf.keys import initialize_console_commands + SuperClass.__init__(self, win, colorscheme) + self.mode = None + self.visible = False + self.commandlist = CommandList() + initialize_console_commands(self.commandlist) + self.last_cursor_mode = 1 + self.clear() + + def draw(self): + if self.mode is None: + return + + self.win.addstr(self.y, self.x, ":" + self.line) + + def finalize(self): + try: + self.win.move(self.y, self.x + self.pos + 1) + except: + pass + + def open(self, mode): + if mode not in CONSOLE_MODES: + return False + + self.last_cursor_mode = curses.curs_set(1) + self.mode = mode + self.focused = True + self.visible = True + return True + + def close(self): + curses.curs_set(self.last_cursor_mode) + self.focused = False + self.visible = False + if hasattr(self, 'on_close'): + self.on_close() + + def clear(self): + self.pos = 0 + self.line = '' + + def press(self, key, fm, env): + from curses.ascii import ctrl, ESC + from ranger.helper import log + log(key) + + try: + cmd = self.commandlist.paths[env.keybuffer] + except KeyError: + env.key_clear() + return + + if cmd == self.commandlist.dummy_object: + return + + cmd.execute(self, fm) + env.key_clear() + + def type_key(self, key): + if isinstance(key, int): + key = chr(key) + + if self.pos == len(self.line): + self.line += key + else: + self.line = self.line[:self.pos] + key + self.line[self.pos:] + + self.pos += len(key) + + def move(self, relative = 0, absolute = None): + if absolute is not None: + if absolute < 0: + self.pos = len(self.line) + 1 + absolute + else: + self.pos = absolute + + self.pos = min(max(0, self.pos + relative), len(self.line)) + + def delete_rest(self, direction): + if direction > 0: + self.line = self.line[:self.pos] + else: + self.line = self.line[self.pos:] + self.pos = 0 + + def delete(self, mod): + pos = self.pos + mod + + self.line = self.line[0:pos] + self.line[pos+1:] + self.move(relative = mod) + + def execute(self): + self.line = '' + self.pos = 0 + self.close() + diff --git a/ranger/gui/widget.py b/ranger/gui/widget.py index f9a0651f..fa1ca585 100644 --- a/ranger/gui/widget.py +++ b/ranger/gui/widget.py @@ -11,7 +11,9 @@ def combine(keylist, keys): class Widget(): def __init__(self, win, colorscheme): self.win = win + self.focused = False self.colorscheme = colorscheme + self.visible = True self.setdim(0, 0, 0, 0) def color(self, keylist = None, *keys): @@ -49,7 +51,7 @@ class Widget(): return (x >= self.x and x < self.x + self.wid) and \ (y >= self.y and y < self.y + self.hei) - def feed_env(self): + def feed_env(self, env): pass def feed(self): @@ -57,9 +59,15 @@ class Widget(): def click(self, event, fm): pass + + def press(self, key, fm): + pass def draw(self): pass + def finalize(self): + pass + def destroy(self): pass |