1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-11-01 15:42:06 +00:00

Compare commits

...

7 Commits
1.14 ... 1.15

Author SHA1 Message Date
nvbn
0925c7966f Bump version 2015-04-21 05:34:44 +02:00
nvbn
dd01303663 Update list of rules in readme 2015-04-21 05:34:34 +02:00
nvbn
e822fade4c #10 Add require_confirmation option 2015-04-21 05:30:15 +02:00
nvbn
0dcefad7bb Merge branch 'NabeelValapra-master' 2015-04-20 22:00:47 +02:00
nvbn
7888315196 #52 Use cp -a, add tests 2015-04-20 22:00:37 +02:00
nvbn
3665a23b9a Merge branch 'master' of git://github.com/NabeelValapra/thefuck into NabeelValapra-master 2015-04-20 21:54:29 +02:00
Nabeel Valapra
f9f757f618 Added rule:cp_omitting_directory 2015-04-20 14:34:09 +05:30
6 changed files with 94 additions and 6 deletions

View File

@@ -65,11 +65,27 @@ Did you mean this?
repl
➜ fuck
lein repl
nREPL server started on port 54848 on host 127.0.0.1 - nrepl://127.0.0.1:54848
REPL-y 0.3.1
...
```
If you are scary to blindly run changed command, there's `require_confirmation`
[settings](#Settings) option:
```bash
➜ apt-get install vim
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
➜ fuck
sudo apt-get install vim [Enter/Ctrl+C]
[sudo] password for nvbn:
Reading package lists... Done
...
```
## Requirements
- pip
@@ -133,11 +149,16 @@ The Fuck tries to match rule for the previous command, create new command
using matched rule and run it. Rules enabled by default:
* `cd_parent` – changes `cd..` to `cd ..`;
* `cp_omitting_directory` – adds `-a` when you `cp` directory;
* `git_no_command` – fixes wrong git commands like `git brnch`;
* `git_push` – adds `--set-upstream origin $branch` to previous failed `git push`;
* `has_exists_script` – prepends `./` when script/binary exists;
* `lein_not_task` – fixes wrong `lein` tasks like `lein rpl`;
* `mkdir_p` – adds `-p` when you trying to create directory without parent;
* `no_command` – fixes wrong console commands, for example `vom/vim`;
* `sudo` – prepends `sudo` to previous command if it failed because of permissions;
* `lein_not_task` – fixes wrong `lein` tasks like `lein rpl`.
* `python_command` – prepends `python` when you trying to run not executable/without `./` python script;
* `rm_dir` – adds `-rf` when you trying to remove directory;
* `sudo` – prepends `sudo` to previous command if it failed because of permissions.
## Creating your own rules
@@ -167,9 +188,10 @@ def get_new_command(command, settings):
## Settings
The Fuck has a few settings parameters:
The Fuck has a few settings parameters, they can be changed in `~/.thefuck/settings.py`:
* `rules` – list of enabled rules, by default all;
* `require_confirmation` – require confirmation before running new command, by default `False`;
* `wait_command` – max amount of time in seconds for getting previous command output;
* `command_not_found` – path to `command_not_found` binary,
by default `/usr/lib/command-not-found`.

View File

@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(name='thefuck',
version=1.14,
version=1.15,
description="Magnificent app which corrects your previous console command",
author='Vladimir Iakovlev',
author_email='nvbn.rm@gmail.com',

View File

@@ -0,0 +1,14 @@
from mock import Mock
from thefuck.rules.cp_omitting_directory import match, get_new_command
def test_match():
assert match(Mock(script='cp dir', stderr="cp: omitting directory 'dir'"),
None)
assert not match(Mock(script='some dir',
stderr="cp: omitting directory 'dir'"), None)
assert not match(Mock(script='cp dir', stderr=""), None)
def test_get_new_command():
assert get_new_command(Mock(script='cp dir'), None) == 'cp -a dir'

View File

@@ -70,3 +70,28 @@ def test_get_matched_rule():
rules, None) is None
assert main.get_matched_rule(main.Command('cd ..', '', ''),
rules, None) == rules[0]
def test_run_rule(capsys):
with patch('thefuck.main.confirm', return_value=True):
main.run_rule(main.Rule(None, lambda *_: 'new-command'),
None, None)
assert capsys.readouterr() == ('new-command\n', '')
with patch('thefuck.main.confirm', return_value=False):
main.run_rule(main.Rule(None, lambda *_: 'new-command'),
None, None)
assert capsys.readouterr() == ('', '')
def test_confirm(capsys):
# When confirmation not required:
assert main.confirm('command', Mock(require_confirmation=False))
assert capsys.readouterr() == ('', 'command\n')
# When confirmation required and confirmed:
with patch('thefuck.main.sys.stdin.read', return_value='\n'):
assert main.confirm('command', Mock(require_confirmation=True))
assert capsys.readouterr() == ('', 'command [Enter/Ctrl+C]')
# When confirmation required and ctrl+c:
with patch('thefuck.main.sys.stdin.read', side_effect=KeyboardInterrupt):
assert not main.confirm('command', Mock(require_confirmation=True))
assert capsys.readouterr() == ('', 'command [Enter/Ctrl+C]Aborted\n')

View File

@@ -28,6 +28,7 @@ def get_settings(user_dir):
str(user_dir.joinpath('settings.py')))
settings.__dict__.setdefault('rules', None)
settings.__dict__.setdefault('wait_command', 3)
settings.__dict__.setdefault('require_confirmation', False)
return settings
@@ -90,11 +91,27 @@ def get_matched_rule(command, rules, settings):
return rule
def confirm(new_command, settings):
"""Returns `True` when running of new command confirmed."""
if not settings.require_confirmation:
sys.stderr.write(new_command + '\n')
return True
sys.stderr.write(new_command + ' [Enter/Ctrl+C]')
sys.stderr.flush()
try:
sys.stdin.read(1)
return True
except KeyboardInterrupt:
sys.stderr.write('Aborted\n')
return False
def run_rule(rule, command, settings):
"""Runs command from rule for passed command."""
new_command = rule.get_new_command(command, settings)
sys.stderr.write(new_command + '\n')
print(new_command)
if confirm(new_command, settings):
print(new_command)
def is_second_run(command):

View File

@@ -0,0 +1,10 @@
import re
def match(command, settings):
return command.script.startswith('cp ') \
and 'cp: omitting directory' in command.stderr.lower()
def get_new_command(command, settings):
return re.sub(r'^cp', 'cp -a', command.script)