1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-13 22:28:33 +00:00

fuckups rule from branch ifuckedup

This commit is contained in:
Jared Henry Oviatt 2015-05-13 12:36:12 -07:00
parent db3b5f5e5f
commit 3ea8e721d9
3 changed files with 48 additions and 22 deletions

View File

@ -28,12 +28,16 @@ DEFAULT_PRIORITY = 1000
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
'wait_command': 3,
'require_confirmation': False,
'no_colors': False}
'no_colors': False,
'priority': {}}
DEFAULT_FUCKUPS = {'cd..':'cd ..'}
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
'THEFUCK_WAIT_COMMAND': 'wait_command',
'THEFUCK_REQUIRE_CONFIRMATION': 'require_confirmation',
'THEFUCK_NO_COLORS': 'no_colors'}
'THEFUCK_NO_COLORS': 'no_colors',
'THEFUCK_PRIORITY': 'priority'}
SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file
@ -66,16 +70,29 @@ def _rules_from_env(val):
return val
def _priority_from_env(val):
"""Gets priority pairs from env."""
for part in val.split(':'):
try:
rule, priority = part.split('=')
yield rule, int(priority)
except ValueError:
continue
def _val_from_env(env, attr):
"""Transforms env-strings to python."""
val = os.environ[env]
if attr == 'rules':
val = _rules_from_env(val)
return _rules_from_env(val)
elif attr == 'priority':
return dict(_priority_from_env(val))
elif attr == 'wait_command':
val = int(val)
return int(val)
elif attr in ('require_confirmation', 'no_colors'):
val = val.lower() == 'true'
return val
return val.lower() == 'true'
else:
return val
def _settings_from_env():
@ -115,3 +132,13 @@ def initialize_settings_file(user_dir):
settings_file.write(SETTINGS_HEADER)
for setting in DEFAULT_SETTINGS.items():
settings_file.write(u'# {} = {}\n'.format(*setting))
def initialize_fuckups_file(user_dir):
fuckups_path = user_dir.joinpath('.my_fuckups')
if not fuckups_path.is_file():
with fuckups_path.open(mode='w') as fuckups_file:
for key, fix in DEFAULT_FUCKUPS.iteritems():
fuckups_file.write(key + '\n')
fuckups_file.write(fix + '\n')
return

View File

@ -1,7 +1,7 @@
# handle reading and writing local list of fuckups
# fuckups are stored in ~/.thefuck/my_fuckups
# fuckups are stored locally in ~/.thefuck/my_fuckups
# file format:
# fucked up command
@ -12,15 +12,9 @@
# rule is in rules/myfuckups.py
# TODO default fuckups (could replace some existing rules with these, mostly typo rules), share fuckups
# TODO add command that calls add_fuckup() - something like -$ fuck --ifuckedup # not sure how to do this with all these aliases...
# TODO add suggestion to add a fuckup when 'no fucks given'
# TODO add command that calls remove_fuckup() - something like -$ fuck --remove 'cd..' # same problem as add_fuckup
def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuckups
def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/.my_fuckups
FILENAME = '~/.thefuck/my_fuckups'
FILENAME = '~/.thefuck/.my_fuckups'
FILE = open(FILENAME, 'r')
IS_FUCKUP = True
@ -31,7 +25,7 @@ def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuc
if IS_FUCKUP:
key = line
IS_FUCKUP = False
else
else:
cmd = line
IS_FUCKUP = True
fuckups[key] = cmd
@ -40,8 +34,11 @@ def get_fuckups(): # returns a dictionary of your fuckups from ~/.thefuck/my_fuc
return fuckups
def add_fuckup(fucked, fixed)
FILENAME = '~/.thefuck/my_fuckups'
def add_fuckup(fucked, fixed):
print ('Adding fucked: ' + fucked + ' as fixed: ' + fixed)
FILENAME = '~/.thefuck/.my_fuckups'
FILE = open(FILENAME, 'w')
FILE.write(fucked + '\n')
@ -51,9 +48,11 @@ def add_fuckup(fucked, fixed)
return
def remove_fuckup(fuckup)
def remove_fuckup(fuckup):
FILENAME = '~/.thefuck/my_fuckups'
print ('Removing fucked: ' + fucked + ' as fixed: ' + fixed)
FILENAME = '~/.thefuck/.my_fuckups'
FILE = open(FILENAME, 'r')
fuckup_cmd = ''
@ -64,7 +63,7 @@ def remove_fuckup(fuckup)
FILE = open(FILENAME, 'w')
for number, line in enumerate(lines)
for number, line in enumerate(lines):
if line != fuckup and line != fuckup_cmd:
FILE.write(line)
elif line == fuckup:

View File

@ -1,5 +1,5 @@
import custom_fuckups
import thefuck.custom_fuckups
fuckups = custom_fuckups.get_fuckups()