Created
March 14, 2023 23:41
-
-
Save mutoo/a509e23adb112872d4063dda4a8d867a to your computer and use it in GitHub Desktop.
thefuck customized rule: ping – fixes cannot resolve host issues when http(s):// is prepended (eg. copy url from browser address bar);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
from six.moves.urllib.parse import urlparse | |
from thefuck.utils import for_app, memoize | |
@memoize | |
def get_hostname_from_url(maybeUrl): | |
try: | |
results = urlparse(maybeUrl) | |
return results.hostname | |
except Exception: | |
return None | |
@memoize | |
def get_index_of_url(parts): | |
for i, part in enumerate(parts): | |
if get_hostname_from_url(part): | |
return i | |
return None | |
@for_app('ping') | |
def match(command): | |
if 'cannot resolve' not in command.output: | |
return False | |
# It only fix if the command has a valid url | |
index_of_url = get_index_of_url(command.script_parts) | |
return index_of_url is not None | |
def get_new_command(command): | |
index_of_url = get_index_of_url(command.script_parts) | |
url = command.script_parts[index_of_url] | |
hostname = get_hostname_from_url(url) | |
return ' '.join(command.script_parts[:index_of_url] + [hostname] + command.script_parts[index_of_url + 1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: download and put it in the folder:
~/.config/thefuck/rules/