feat(functions): add open-url and repo-open browser helpers

Extract the browser-detection and launch logic out of config-help into a
reusable open-url function, then build repo-open on top of it to open the
current repo's origin remote in a browser.

- open-url: resolve the best graphical browser (fish_help_browser -> BROWSER
  -> xdg-mime https handler -> known binaries -> xdg-open) and launch it
  backgrounded. Silent by default; -v/--verbose reports the browser, -s/--silent
  is accepted explicitly. Browser console chatter is discarded.
- repo-open: normalize the origin URL from HTTPS and SSH/scp forms, deep-link
  to the current branch (falling back to the remote default) and sub-directory.
  Provider layout resolved via git config browse.provider, hostname heuristic
  (github/gitlab/gitea/bitbucket, codeberg->gitea), then github default.
  -p/--print emits the URL, -r/--root ignores the sub-directory.
- config-help: replace the inlined browser block with an open-url call.
- abbr: add open-repo and url-open abbreviations that expand to the canonical
  command names on space/enter.
- docs: document both functions in the SSoT (fish-config.md + index) and add a
  repo-open row to the README doc-browsing table.
This commit is contained in:
2026-07-04 02:02:28 -04:00
parent 0a83bd425c
commit 4761c48d96
7 changed files with 376 additions and 65 deletions
+2 -65
View File
@@ -138,71 +138,8 @@ function config-help --description 'Open the offline fish shell configuration ma
set -l page_url "file://$html_dir/$html_path"
# Browser detection — mirrors fish's help.fish priority order but
# resolves actual browser binaries before falling back to xdg-open.
# xdg-open dispatches on the file's MIME type (text/html), which can
# be associated with non-browser apps (e.g. ebook readers). Using a
# real browser binary directly with a file:// URI avoids that lookup.
set -l graphical_browsers \
firefox firefox-esr chromium chromium-browser google-chrome \
brave-browser vivaldi vivaldi-stable epiphany falkon qutebrowser \
opera x-www-browser htmlview
set -l browser $fish_help_browser
if not set -q browser[1]
if set -q BROWSER
echo $BROWSER | read -at browser
if not type -q $browser[1]
set_color red
echo "error: \$BROWSER '$browser[1]' is not a valid command" >&2
set_color normal
return 1
end
else
# Resolve the https scheme handler from xdg-mime and use its
# binary directly — most reliable on modern Linux desktops.
if type -q xdg-mime
set -l desk (xdg-mime query default x-scheme-handler/https 2>/dev/null)
if test -n "$desk"
set -l candidate (string replace -r '\.desktop$' '' -- $desk)
if type -q $candidate
set browser $candidate
end
end
end
# Fall back to trying known browser binaries in order.
if not set -q browser[1]
for b in $graphical_browsers
if type -q -f $b
set browser $b
break
end
end
end
# Last resort: xdg-open (may hit wrong app for local files).
if not set -q browser[1]; and type -q xdg-open
set browser xdg-open
end
end
end
if not set -q browser[1]
set_color red
echo "error: could not find a web browser — set \$fish_help_browser or \$BROWSER" >&2
set_color normal
return 1
end
set_color green
echo "Opening HTML docs in $browser[1]…"
set_color normal
# Background the browser so it doesn't block the terminal.
sh -c '("$@") &' -- $browser $page_url
return 0
open-url $page_url
return $status
end
# ── --man / -m ───────────────────────────────────────────────