mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-14 14:48:49 +00:00
#N/A: Add Stack Overflow rule to lookup error messages in terminal
This commit is contained in:
parent
e94f85c0b8
commit
52e4c418a4
@ -24,8 +24,7 @@ def main():
|
||||
logs.version(get_installation_info().version,
|
||||
sys.version.split()[0], shell.info())
|
||||
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:
|
||||
fix_command(known_args)
|
||||
elif known_args.alias:
|
||||
|
@ -1,9 +1,9 @@
|
||||
from thefuck.types import Rule
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def post_match_execute(known_args):
|
||||
"""Executes post match funtion. Used when `thefuck` called with `--post-match` argument."""
|
||||
# check the first flag to see what rule
|
||||
rule = Rule.from_path(known_args.command[0])
|
||||
rule.post_match(known_args.command[1:])
|
||||
|
||||
# check the first flag to see what rule
|
||||
rule = Rule.from_path(Path(known_args.command[0]))
|
||||
rule.post_match(known_args.command[1], known_args.command[2])
|
||||
|
@ -1,7 +1,12 @@
|
||||
from urllib import parse
|
||||
import requests
|
||||
import sys
|
||||
import colorama
|
||||
from thefuck.conf import settings
|
||||
|
||||
is_post_match = True
|
||||
priority = 9500
|
||||
|
||||
|
||||
|
||||
def match(command):
|
||||
@ -14,6 +19,8 @@ def get_new_command(command):
|
||||
|
||||
def post_match(fc,output):
|
||||
num_of_result = 3;
|
||||
# make sure the output is not too long
|
||||
output = output[0:100]
|
||||
search_query = "{} {}".format(fc, output)
|
||||
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)
|
||||
@ -22,11 +29,36 @@ def post_match(fc,output):
|
||||
if response:
|
||||
response_json = response.json()
|
||||
for i in response_json["items"]:
|
||||
item = "{} \n".format(i["title"])
|
||||
display_list.append(item)
|
||||
# print("response_json",display_list)
|
||||
# print(response_json)
|
||||
# print("title", response_json["items"][0]["title"])
|
||||
display_list.append({"title":i["title"], "link":i["link"], "tags":i["tags"]})
|
||||
stackoverflow(display_list)
|
||||
|
||||
# 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_
|
||||
|
@ -21,7 +21,7 @@ class Bash(Generic):
|
||||
export PYTHONIOENCODING=utf-8;
|
||||
TF_CMD=$(
|
||||
thefuck {argument_placeholder} $@
|
||||
) && eval $TF_CMD;
|
||||
) && eval "$TF_CMD";
|
||||
unset TF_HISTORY;
|
||||
export PYTHONIOENCODING=$TF_PYTHONIOENCODING;
|
||||
{alter_history}
|
||||
|
Loading…
x
Reference in New Issue
Block a user