Skip to content

Commit

Permalink
Improved detection of files and IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
alberti42 committed Jan 2, 2025
1 parent 70345d4 commit 56dec87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
22 changes: 15 additions & 7 deletions tmux-fzf-links-python-pkg/tmux_fzf_links/default_schemes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import re
import sys
from .export import OpenerType, SchemeEntry
from os.path import expanduser
from pathlib import Path

def git_post_handler(match:re.Match[str]) -> list[str]:
Expand All @@ -16,9 +17,9 @@ def error_post_handler(match:re.Match[str]) -> list[str]:
return [f"{file}:{line}"]

def heuristic_find_file(file_path_str:str) -> Path | None:
# Expand tilde (~) to the user's home directory
file_path = Path(file_path_str).expanduser()

# Expand tilde (~) to the user's home directory
file_path = Path(expanduser(file_path_str))
file_path.expanduser
# Check if the file exists either as is or relative to the current directory
if file_path.exists():
return file_path # Return the absolute path
Expand All @@ -41,7 +42,14 @@ def file_pre_handler(match: re.Match[str]) -> str | None:

def file_post_handler(match:re.Match[str]) -> list[str]:
file_path_str = match.group(0)
return ['open','-R', str(heuristic_find_file(file_path_str))]
if sys.platform == "darwin":
return ['open','-R', str(heuristic_find_file(file_path_str))]
elif sys.platform == "linux":
return ['xdg-open', str(heuristic_find_file(file_path_str))]
elif sys.platform == "win32":
return ['explorer', str(heuristic_find_file(file_path_str))]
else:
raise Exception(f"platform {sys.platform} not supported")

# Define schemes
default_schemes: dict[str, SchemeEntry] = {
Expand All @@ -56,7 +64,7 @@ def file_post_handler(match:re.Match[str]) -> list[str]:
"opener": OpenerType.CUSTOM,
"post_handler": file_post_handler,
"pre_handler": file_pre_handler,
"regex": re.compile(r"\~?[a-zA-Z0-9_\/\-]+\.[a-zA-Z0-9]+")
"regex": re.compile(r"\~?[a-zA-Z0-9_\/\-\:\.]+")
},
"<git>": {
"opener":OpenerType.BROWSER,
Expand All @@ -69,7 +77,7 @@ def file_post_handler(match:re.Match[str]) -> list[str]:
"post_handler": error_post_handler,
"pre_handler": None,
"regex": re.compile(r"File \"(?P<file>...*?)\"\, line (?P<line>[0-9]+)")
}
},
}

__all__ = ["default_schemes"]
13 changes: 4 additions & 9 deletions user_schemes/user_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@
from tmux_fzf_links.export import OpenerType, SchemeEntry

def ip_pre_handler(match:re.Match[str]) -> str | None:
if not match.group("uri"):
# keep the match only if no `https?://` precedes the IP address
return f"{match.group("ip")}"
else:
# drop the match
return None
return match.group("ip")

def ip_post_handler(match:re.Match[str]) -> list[str]:
return [f"https://{match.group(0)}"]
ip_addr_str = match.group("ip")
return [f"https://{ip_addr_str}"]

# Define schemes
user_schemes: dict[str, SchemeEntry] = {
"<ip>": {
"opener": OpenerType.BROWSER,
"post_handler": ip_post_handler,
"pre_handler": ip_pre_handler,
"regex": re.compile(r"(?P<uri>https?://)?(?P<ip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\S+))")
# [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?(/\S+)*
"regex": re.compile(r"([\'\" \t\{\[\(\~])(?P<ip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[^ \t\)\]\}\"\'\n]+)")
},
}

Expand Down

0 comments on commit 56dec87

Please sign in to comment.