Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show found files as hyperlinks just as ls does? #1295

Closed
097115 opened this issue Apr 2, 2023 · 4 comments · Fixed by #1571
Closed

Show found files as hyperlinks just as ls does? #1295

097115 opened this issue Apr 2, 2023 · 4 comments · Fixed by #1571

Comments

@097115
Copy link

097115 commented Apr 2, 2023

Could it possible that fd would return its results as hyperlinks, just as ls, for instance, does:

--hyperlink[=WHEN]
    hyperlink file names; WHEN can be 'always' (default if omitted), 'auto', or 'never'

(Which -- in some terminal emulators -- would allow to open the found files with hotkeys, like via the "Hints" mechanism in alacritty.)

@dandavison
Copy link

dandavison commented Apr 25, 2023

I think this might be nice. However, we could also do this with a shell wrapper. Here are a couple of quick attempts at that in bash and nushell, preserving fd's colored output, and assuming we want a link to open the path in VSCode.

bash: (requires ansifilter)

function hyperlink() {
    local url="$1"
    local text="$2"
    printf '\e]8;;%s\e\\%s\e]8;;\e\\\n' "$url" "$text"
}

function fd() {
    local _path
    command fd --color=always "$@" \
    | while read _path; do
        local abspath=$(readlink -f $(echo -n $_path | ansifilter))
        local url="vscode://file/$abspath"
        hyperlink $url $_path
      done
}

nushell: (nushell currently doesn't support passing arbitrary arguments on to the wrapped executable)

export def 'str hyperlink' [text: string] {
  let url = $in
  let osc8 = (ansi -o '8;;')
  let st = (ansi st)
  $"($osc8)($url)($st)($text)($osc8)($st)"
}

export def fd [
  pattern: string,
  path: string = ".",
] {
  ^fd --color=always $pattern $path
  | lines
  | each {
    |s| $'vscode://file/($s | ansi strip | path expand)' | str hyperlink $s
  }
  | to text
}

@thenbe
Copy link

thenbe commented Jul 10, 2023

another alternative: piping the output to ls

# hyperlinked_fd
hfd() {
	fd "$@" --type=file -X ls -l --hyperlink=auto
}

@097115
Copy link
Author

097115 commented Jul 10, 2023

@thenbe,

Thanks! Way faster than the previous suggestion :). But, on the other hand, still palpably slower than the plain fd.

It's really a pity @sharkdp isn't interested in this feature... :(

tmccombs added a commit to tmccombs/fd that referenced this issue Jun 8, 2024
tmccombs added a commit to tmccombs/fd that referenced this issue Jun 8, 2024
@tmccombs
Copy link
Collaborator

tmccombs commented Jun 8, 2024

As a workaround until #1571 is released you could use the --format option to get this, like so:

fd --format $'\x1b]8;;file://'"${HOSTNAME}${PWD}/{}"$'\x1b\\{}\x1b]8;;\x1b\\'

You may want to use an alias or wrapper script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants