1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-20 01:28:56 +00:00

#N/A: Add Stack Overflow rule to lookup error messages in terminal

This commit is contained in:
asong4211 2019-02-24 09:34:50 -06:00 committed by Payton Garland
parent e94f85c0b8
commit 52e4c418a4
4 changed files with 44 additions and 13 deletions

View File

@ -24,8 +24,7 @@ def main():
logs.version(get_installation_info().version, logs.version(get_installation_info().version,
sys.version.split()[0], shell.info()) sys.version.split()[0], shell.info())
elif known_args.post_match: elif known_args.post_match:
print ("OPE") post_match_execute(known_args)
# post_match_execute(known_args)
elif known_args.command or 'TF_HISTORY' in os.environ: elif known_args.command or 'TF_HISTORY' in os.environ:
fix_command(known_args) fix_command(known_args)
elif known_args.alias: elif known_args.alias:

View File

@ -1,9 +1,9 @@
from thefuck.types import Rule from thefuck.types import Rule
from pathlib import Path
def post_match_execute(known_args): def post_match_execute(known_args):
"""Executes post match funtion. Used when `thefuck` called with `--post-match` argument.""" """Executes post match funtion. Used when `thefuck` called with `--post-match` argument."""
# check the first flag to see what rule # check the first flag to see what rule
rule = Rule.from_path(known_args.command[0]) rule = Rule.from_path(Path(known_args.command[0]))
rule.post_match(known_args.command[1:]) rule.post_match(known_args.command[1], known_args.command[2])

View File

@ -1,7 +1,12 @@
from urllib import parse from urllib import parse
import requests import requests
import sys
import colorama
from thefuck.conf import settings
is_post_match = True is_post_match = True
priority = 9500
def match(command): def match(command):
@ -14,6 +19,8 @@ def get_new_command(command):
def post_match(fc,output): def post_match(fc,output):
num_of_result = 3; num_of_result = 3;
# make sure the output is not too long
output = output[0:100]
search_query = "{} {}".format(fc, output) search_query = "{} {}".format(fc, output)
sq_encoded = parse.quote_plus(search_query) sq_encoded = parse.quote_plus(search_query)
url = "https://api.stackexchange.com/2.2/search/advanced?pagesize={}&order=desc&sort=votes&q={}&accepted=True&site=stackoverflow".format(num_of_result,sq_encoded) url = "https://api.stackexchange.com/2.2/search/advanced?pagesize={}&order=desc&sort=votes&q={}&accepted=True&site=stackoverflow".format(num_of_result,sq_encoded)
@ -22,11 +29,36 @@ def post_match(fc,output):
if response: if response:
response_json = response.json() response_json = response.json()
for i in response_json["items"]: for i in response_json["items"]:
item = "{} \n".format(i["title"]) display_list.append({"title":i["title"], "link":i["link"], "tags":i["tags"]})
display_list.append(item) stackoverflow(display_list)
# print("response_json",display_list)
# print(response_json)
# print("title", response_json["items"][0]["title"])
# import pdb; pdb.set_trace(); # BREAKPOINT # import pdb; pdb.set_trace(); # BREAKPOINT
return display_list return True
def stackoverflow(display_list):
if not display_list:
sys.stdout.write(u"{col}No fuck is found.{reset} \n".format(col=color(colorama.Fore.RED), reset=color(colorama.Style.RESET_ALL)))
for i, msg in enumerate(display_list):
sys.stdout.write(
u'{title_col}{i}. Post Title:{title} \n'
u'Post Tags: {tags} \n'
u'{url_col}URL: {url}\n\n'
.format(
title_col=color(colorama.Fore.GREEN),
url_col=color(colorama.Fore.RED),
title=msg["title"],
tags=msg["tags"],
url=msg["link"],
i=i+1,
reset=color(colorama.Style.RESET_ALL)
))
def color(color_):
"""Utility for ability to disabling colored output."""
if settings.no_colors:
return ''
else:
return color_

View File

@ -21,7 +21,7 @@ class Bash(Generic):
export PYTHONIOENCODING=utf-8; export PYTHONIOENCODING=utf-8;
TF_CMD=$( TF_CMD=$(
thefuck {argument_placeholder} $@ thefuck {argument_placeholder} $@
) && eval $TF_CMD; ) && eval "$TF_CMD";
unset TF_HISTORY; unset TF_HISTORY;
export PYTHONIOENCODING=$TF_PYTHONIOENCODING; export PYTHONIOENCODING=$TF_PYTHONIOENCODING;
{alter_history} {alter_history}