Last active
January 5, 2025 20:48
-
-
Save stuaxo/b1f0ca2517ded79d3cbf55aa2138d360 to your computer and use it in GitHub Desktop.
Output OSC-8 hyperlink in the terminal
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
#!/bin/bash | |
# | |
# hyperlink.sh | |
# | |
# Generate clickable hyperlinks to files in the terminal using OSC-8 standard ANSI. | |
# | |
# Usage: | |
# hyperlink.sh [-t text] [-r relative_folder] [-s] [--help] file | |
# | |
# Many terminals that support OSC-8 hyperlinks including: | |
# - iTerm2 (macOS) | |
# - Windows Terminal (Windows) | |
# - GNOME Terminal (Linux) | |
# - Terminator (Linux) | |
# | |
# The spec for OSC-8 is hosted on in a github gist: | |
# https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda | |
# | |
# The script provides the following options: | |
# -t text Set custom text for the hyperlink | |
# -r relative_folder Make the file path relative to the specified folder | |
# -s shorten Shorten the file path by using tilde instead of the home directory | |
# -h --help Display the help message | |
# | |
# Author: Stuart Axon and everybody that contributed to various LLMs. | |
# | |
# Date: 2023-05-10 | |
# License: GPL | |
# | |
is_output_terminal() { | |
if command -v tput >/dev/null 2>&1 && tput longname >/dev/null 2>&1; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
print_hyperlink() { | |
local filepath="$1" | |
local text="$2" | |
if is_output_terminal; then | |
printf '\e]8;;file://%s\a%s\e]8;;\a\n' "$filepath" "$text" | |
else | |
printf '%s\n' "$text" | |
fi | |
} | |
show_help() { | |
echo "Usage: $0 [-t text] [-r relative_folder] [-s] [--help] file" | |
echo "" | |
echo "Options:" | |
echo " -t text Set custom text for the hyperlink" | |
echo " -r relative_folder Make the file path relative to the specified folder" | |
echo " -s shorten Shorten the file path by using tilde instead of the home directory" | |
echo " -h --help Display this help message" | |
} | |
# Parse command line options | |
while [[ "$#" -gt 0 ]]; do | |
case "$1" in | |
-t) | |
text="$2" | |
shift 2 | |
;; | |
-r) | |
relative_to="$2" | |
shift 2 | |
;; | |
-s) | |
shorten=true | |
shift | |
;; | |
--help) | |
show_help | |
exit 0 | |
;; | |
*) | |
if [[ -z "$file" ]]; then | |
file="$1" | |
shift | |
else | |
echo "Unknown option: $1" | |
show_help | |
exit 1 | |
fi | |
;; | |
esac | |
done | |
if [[ -z "$file" ]]; then | |
show_help | |
exit 1 | |
fi | |
# If text is not provided use display the file path, -r and -s can adjust the format. | |
if [[ -z "$text" ]]; then | |
text="$file" | |
# if -r makes the displayed path relative to a supplied | |
if [[ -n "$relative_to" ]]; then | |
text="$(realpath --relative-to="$relative_to" "$text")" | |
fi | |
# if -s is used and file is in home directory use ~ | |
if [[ -n "$shorten" ]]; then | |
home_dir=$(echo ~) | |
text="${text//$home_dir/\~}" | |
fi | |
fi | |
print_hyperlink "$file" "$text" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment