diff options
-rw-r--r-- | ranger/commands.py | 31 | ||||
-rw-r--r-- | ranger/help/console.py | 4 |
2 files changed, 35 insertions, 0 deletions
diff --git a/ranger/commands.py b/ranger/commands.py index a6fc77fb..b3625037 100644 --- a/ranger/commands.py +++ b/ranger/commands.py @@ -315,6 +315,37 @@ class edit(Command): return self._tab_directory_content() +class eval_(Command): + """ + :eval <python code> + + Evaluates the python code. + `fm' is a reference to the FM instance. + To display text, use the function `p'. + + Examples: + :eval fm + :eval len(fm.env.directories) + :eval p("Hello World!") + """ + name = 'eval' + + def execute(self): + code = parse(self.line).rest(1) + fm = self.fm + p = fm.notify + try: + try: + result = eval(code) + except SyntaxError: + exec(code) + else: + if result: + p(result) + except Exception as err: + p(err) + + class rename(Command): """ :rename <newname> diff --git a/ranger/help/console.py b/ranger/help/console.py index 1ea06f90..cd382d2e 100644 --- a/ranger/help/console.py +++ b/ranger/help/console.py @@ -77,6 +77,10 @@ it conflicts with ":cd". :edit <filename> Opens the specified file in the text editor. +:eval <python_code> + Evaluates the given code inside ranger. `fm' is a reference to + the filemanager instance, `p' is a function to print text. + :filter <string> Displays only files which contain <string> in their basename. |