Skip to content

Commit

Permalink
chore: handle colons in GitHub file names
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed Jan 6, 2025
1 parent 543b3d5 commit 73a61a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,22 @@ open_query_in_browser() {
fi
}

# Open a GitHub file in the browser. Some users put colons in their file names, though POSIX
# recommends avoiding this and 'gh' can't handle it. Colons are temporarily replaced with a
# placeholder to generate the URL.
# https://pubs.opengroup.org/onlinepubs/9699919799.2016edition/basedefs/V1_chap03.html#tag_03_282
open_file_in_browser() {
local repo=$1 file_name=$2 line_number=$3
local url=""
local placeholder="__C_0_L_0_N__"
if [[ $file_name == *:* ]]; then
url=$(command gh browse --no-browser --repo "$repo" "${file_name//:/${placeholder}}:${line_number}")
command "$python_executable" -m webbrowser "${url//${placeholder}/:}"
else
command gh browse --repo "$repo" "${file_name}:${line_number}"
fi
}

# Adding the current value for 'FZF_QUERY', exported by 'fzf', to the history file.
add_history() {
if ((GHFC_HISTORY_LIMIT)); then
Expand Down Expand Up @@ -863,13 +879,18 @@ view_contents() {
bat_args+=("--file-name='${file_name//"'"/\`} │ 🅻 ${line_numbers[*]:-<none>}'")

if $open_in_editor; then
tempfile_with_ext="${store_file_contents}_${index}_${file_name}"
command cp "${store_file_contents}_${index}_fetched" "$tempfile_with_ext"
case $(command basename "${EDITOR-}") in
code | codium)
code | codium | cursor | windsurf)
# VSCode cannot handle opening files with colons in their names
tempfile_with_ext="${store_file_contents}_${index}_${file_name//:/_}"
command cp "${store_file_contents}_${index}_fetched" "$tempfile_with_ext"

editor_args=(--reuse-window --goto "${tempfile_with_ext}:${line_numbers:-1}")
;;
nano | nvim | vi | vim)
tempfile_with_ext="${store_file_contents}_${index}_${file_name}"
command cp "${store_file_contents}_${index}_fetched" "$tempfile_with_ext"

editor_args=("+${line_numbers:-1}" "$tempfile_with_ext")
;;
*)
Expand Down Expand Up @@ -1020,7 +1041,7 @@ main() {
--bind "?:transform:[[ ! \$FZF_PROMPT =~ \"$fzf_prompt_helpABC\" ]] &&
echo 'change-prompt($fzf_prompt_helpABC)+change-preview-window(~0:+1)+change-preview:print_help_text' ||
echo 'change-prompt($default_fzf_prompt)+change-preview-window(+\{1}+3/3)+change-preview:\view_contents \{}'" \
--bind "${GHFC_OPEN_BROWSER_KEY}:execute-silent:command gh browse --repo {4} {5}:{1}" \
--bind "${GHFC_OPEN_BROWSER_KEY}:execute-silent:open_file_in_browser {4} {5} {1}" \
--bind "${GHFC_OPEN_EDITOR_KEY}:transform:[[ \$FZF_MATCH_COUNT -ge 1 && \${EDITOR##*/} =~ ^(code|codium)$ ]] &&
echo 'execute-silent:open_in_editor=true \view_contents \{}' ||
echo 'execute:open_in_editor=true \view_contents \{}'" \
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ BAT_THEME="Dracula" gh find-code
### Editor
- The extension uses the `EDITOR` environment variable to determine in which
editor the selected file will be opened, works with `nano`, `nvim/vi/vim`,
and `VSCode/VSCodium`.
and `VSCode` and some of its derivatives (e.g. `VSCodium`).
- The code from opened files is stored temporarily and is removed when the
program ends.

Expand Down

0 comments on commit 73a61a4

Please sign in to comment.