masatolan.com
Pythonプロダクトなので、ちょっと内部で何をしているのかを読んでみることにしました。
github.com
構造。
そんなにソースコードの量は多くないイメージ。重要なところはrulesでなんかごにょごにょしてると思われる。
$ tree -I "*.pyc" thefuck/
thefuck/
|-- __init__.py
|-- conf.py
|-- logs.py
|-- main.py
|-- rules
| |-- __init__.py
| |-- apt_get.py
| |-- brew_install.py
| |-- brew_unknown_command.py
| |-- cd_mkdir.py
| |-- cd_parent.py
| |-- composer_not_command.py
| |-- cp_omitting_directory.py
| |-- fix_alt_space.py
| |-- git_not_command.py
| |-- git_push.py
| |-- has_exists_script.py
| |-- lein_not_task.py
| |-- ls_lah.py
| |-- mkdir_p.py
| |-- no_command.py
| |-- pip_unknown_command.py
| |-- python_command.py
| |-- rm_dir.py
| |-- rm_root.py
| |-- sl_ls.py
| |-- ssh_known_hosts.py
| |-- sudo.py
| `-- switch_lang.py
|-- types.py
`-- utils.py
内部的には何をやっているの?
rulesの中を見ると、だいたいのことはわかったような気がします。
import re
def match(command, settings):
return ('git' in command.script
and " is not a git command. See 'git --help'." in command.stderr
and 'Did you mean' in command.stderr)
def get_new_command(command, settings):
broken_cmd = re.findall(r"git: '([^']*)' is not a git command",
command.stderr)[0]
new_cmd = re.findall(r'Did you mean[^\n]*\n\s*([^\n]*)',
command.stderr)[0]
return command.script.replace(broken_cmd, new_cmd, 1)
Gitでいうと、コマンドが間違っていたら「もしかしてこのコマンド打とうとしてる?」みたいなことを聞かれます。
$ git brnch
git: 'brnch' is not a git command. See 'git --help'.
Did you mean this?
branch
このコマンド内のメッセージを正規表現で取り出して、コマンドが間違っていたら取り出した正規表現のコマンドで再実行する、っていうかんじのことをやっています。
cdとかも似たような感じです。
なんとなく
yeah yeah yeah yeah f off!! f off!!みたいなノリで。