diff --git a/README.md b/README.md index 395253f4..29d73ff1 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `npm_wrong_command` – fixes wrong npm commands like `npm urgrade`; * `no_command` – fixes wrong console commands, for example `vom/vim`; * `no_such_file` – creates missing directories with `mv` and `cp` commands; -* `open` – prepends `http` to address passed to `open`; +* `open` – either prepends `http://` to address passed to `open` or create a new file or directory and passes it to `open`; * `pip_unknown_command` – fixes wrong `pip` commands, for example `pip instatl/pip install`; * `python_command` – prepends `python` when you trying to run not executable/without `./` python script; * `python_execute` – appends missing `.py` when executing Python files; diff --git a/tests/rules/test_open.py b/tests/rules/test_open.py index 5780e043..9186adf4 100644 --- a/tests/rules/test_open.py +++ b/tests/rules/test_open.py @@ -32,15 +32,18 @@ def test_not_is_arg_url(script): 'open foo.com', 'xdg-open foo.com', 'gnome-open foo.com', - 'kde-open foo.com']) + 'kde-open foo.com', + 'open nonest']) def test_match(script, stderr): assert match(Command(script, stderr=stderr)) @pytest.mark.parametrize('script, new_command', [ - ('open foo.io', 'open http://foo.io'), - ('xdg-open foo.io', 'xdg-open http://foo.io'), - ('gnome-open foo.io', 'gnome-open http://foo.io'), - ('kde-open foo.io', 'kde-open http://foo.io')]) + ('open foo.io', ['open http://foo.io']), + ('xdg-open foo.io', ['xdg-open http://foo.io']), + ('gnome-open foo.io', ['gnome-open http://foo.io']), + ('kde-open foo.io', ['kde-open http://foo.io']), + ('open nonest', ['touch nonest && open nonest', + 'mkdir nonest && open nonest'])]) def test_get_new_command(script, new_command, stderr): assert get_new_command(Command(script, stderr=stderr)) == new_command diff --git a/thefuck/rules/open.py b/thefuck/rules/open.py index b253a489..86f20087 100644 --- a/thefuck/rules/open.py +++ b/thefuck/rules/open.py @@ -24,8 +24,17 @@ def is_arg_url(command): @for_app('open', 'xdg-open', 'gnome-open', 'kde-open') def match(command): - return is_arg_url(command) + return (is_arg_url(command) or + command.stderr.strip().startswith('The file ') and + command.stderr.strip().endswith(' does not exist.')) +@eager def get_new_command(command): - return command.script.replace('open ', 'open http://') + stderr = command.stderr.strip() + if is_arg_url(command): + yield command.script.replace('open ', 'open http://') + elif stderr.startswith('The file ') and stderr.endswith(' does not exist.'): + arg = command.script.split(' ', 1)[1] + for option in ['touch', 'mkdir']: + yield shell.and_(u'{} {}'.format(option, arg), command.script)