From af6658b377d145529e0b9304247ed3b9b6e40856 Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 19 Jan 2010 21:18:53 +0100 Subject: implemented #48: abbreviate commands --- TODO | 2 +- ranger/commands.py | 16 ++++++++++++++-- ranger/gui/widgets/console.py | 6 +++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index 8d8b65f5..efa27548 100644 --- a/TODO +++ b/TODO @@ -35,7 +35,7 @@ General (X) #43 10/01/18 internally treat the bookmarks ` and ' the same ( ) #44 10/01/18 more error messages :P ( ) #47 10/01/19 less restricive auto preview - ( ) #48 10/01/19 abbreviate commands with first unambiguous substring + (X) #48 10/01/19 abbreviate commands with first unambiguous substring Bugs diff --git a/ranger/commands.py b/ranger/commands.py index ea8e41f4..0cfe78b6 100644 --- a/ranger/commands.py +++ b/ranger/commands.py @@ -404,11 +404,23 @@ del varname del var def alias(**kw): + """Create an alias for commands, eg: alias(quit=exit)""" for key, value in kw.items(): by_name[key] = value -alias(q=quit) -alias(e=edit) +def get_command(name, abbrev=True): + if abbrev: + lst = [cls for cmd, cls in by_name.items() if cmd.startswith(name)] + if len(lst) == 0: + raise KeyError + if len(lst) == 1: + return lst[0] + raise ValueError("Ambiguous command") + else: + try: + return by_name[name] + except KeyError: + return None def command_generator(start): return (cmd + ' ' for cmd in by_name if cmd.startswith(start)) diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py index 0da0480b..5c888da5 100644 --- a/ranger/gui/widgets/console.py +++ b/ranger/gui/widgets/console.py @@ -292,8 +292,12 @@ class CommandConsole(ConsoleWithTab): return None try: - return commands.by_name[command_name] + return commands.get_command(command_name) except KeyError: + self.fm.notify("Invalid command! Press ? for help.", bad=True) + return None + except ValueError as e: + self.fm.notify(e) return None def _get_tab(self): -- cgit 1.4.1-2-gfad0