Skip to content

Instantly share code, notes, and snippets.

@mutoo
Created March 14, 2023 23:41
Show Gist options
  • Save mutoo/a509e23adb112872d4063dda4a8d867a to your computer and use it in GitHub Desktop.
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);
# -*- 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:])
@mutoo
Copy link
Author

mutoo commented Mar 14, 2023

Usage: download and put it in the folder: ~/.config/thefuck/rules/

➜  ~ ping http://google.com
ping: cannot resolve http://google.com: Unknown host
➜  ~ fuck
ping google.com [enter/↑/↓/ctrl+c]
PING google.com (142.250.70.174): 56 data bytes
64 bytes from 142.250.70.174: icmp_seq=0 ttl=57 time=15.850 ms
64 bytes from 142.250.70.174: icmp_seq=1 ttl=57 time=14.481 ms
64 bytes from 142.250.70.174: icmp_seq=2 ttl=57 time=18.862 ms
64 bytes from 142.250.70.174: icmp_seq=3 ttl=57 time=14.833 ms
64 bytes from 142.250.70.174: icmp_seq=4 ttl=57 time=15.854 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment