From 73a61a42dd480f6e051817ea166cfbc1defbd1c1 Mon Sep 17 00:00:00 2001 From: LangLangbart <92653266+LangLangBart@users.noreply.github.com> Date: Mon, 6 Jan 2025 12:00:40 +0100 Subject: [PATCH] chore: handle colons in GitHub file names --- gh-find-code | 29 +++++++++++++++++++++++++---- readme.md | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/gh-find-code b/gh-find-code index d01f589..b0ddc55 100755 --- a/gh-find-code +++ b/gh-find-code @@ -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 @@ -863,13 +879,18 @@ view_contents() { bat_args+=("--file-name='${file_name//"'"/\`} │ 🅻 ${line_numbers[*]:-}'") 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") ;; *) @@ -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 \{}'" \ diff --git a/readme.md b/readme.md index be1662c..64ef946 100644 --- a/readme.md +++ b/readme.md @@ -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.