1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-18 20:11:17 +00:00

added usage records

This commit is contained in:
steven 2020-02-03 12:10:44 -06:00
parent 67cecf4671
commit a85eb5800a
3 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1 @@
from .statistics import CommandRecords

View File

@ -0,0 +1,46 @@
import json
import thefuck.const as const
class CommandRecords(object):
"""Object to interact with 'records.json' data"""
def __init__(self):
"""Opens 'records.json' if available"""
try:
with open("records.json", "r") as records_file:
self.statistics = json.load(records.file)
except:
self.statistics = const.DEFAULT_RECORDS_DICT
def add_records(self, category, **kwargs):
"""Creates new records from arguments
and dumps information to 'records.json'
:type commands: Iterable[thefuck.types.CorrectedCommand]
:type category: Const string
"""
if category == const.APPLIED_RULES:
for command in kwargs['command_list']:
try:
self.statistics[const.APPLIED_RULES][command.script] +=1
except KeyError:
self.statistics[const.APPLIED_RULES].update({command.script: 1})
elif category == const.SELECTED_RULES:
command = kwargs['command_single']
try:
self.statistics[const.SELECTED_RULES][command.script] += 1
except KeyError:
self.statistics[const.SELECTED_RULES].update({command.script: 1})
elif category == const.NO_FUCKS_GIVEN:
self.statistics[const.NO_FUCKS_GIVEN] += 1
def clear_records(self):
self.statistics = const.DEFAULT_RECORDS_DICT
self.save()
def save(self):
with open("records.json", "w") as records_file:
json.dump(self.statistics, records_file)

View File

@ -6,6 +6,7 @@ from .exceptions import NoRuleMatched
from .system import get_key
from .utils import get_alias
from . import logs, const
from .statistics import CommandRecords
def read_actions():
@ -67,21 +68,41 @@ def select_command(corrected_commands):
:rtype: thefuck.types.CorrectedCommand | None
"""
records = CommandRecords()
try:
selector = CommandSelector(corrected_commands)
records.add_records(
command_list = [command for command in corrected_commands],
category = const.APPLIED_RULES
)
records.save()
except NoRuleMatched:
logs.failed('No fucks given' if get_alias() == 'fuck'
else 'Nothing found')
records.add_records(
category = const.NO_FUCKS_GIVEN
)
records.save()
return
if not settings.require_confirmation:
logs.show_corrected_command(selector.value)
records.add_records(
command_single = selector.value,
category = const.SELECTED_RULES
)
records.save()
return selector.value
logs.confirm_text(selector.value)
for action in read_actions():
if action == const.ACTION_SELECT:
records.add_records(
command_single = selector.value,
category = const.SELECTED_RULES
)
records.save()
sys.stderr.write('\n')
return selector.value
elif action == const.ACTION_ABORT:
@ -93,3 +114,4 @@ def select_command(corrected_commands):
elif action == const.ACTION_NEXT:
selector.next()
logs.confirm_text(selector.value)