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

Support {host} in hyperlinks #1901

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ pub struct Opt {
)]
/// Format string for file hyperlinks (requires --hyperlinks).
Comment on lines 432 to 433
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you are at it, the above (in GitHub I can't add a comment there..) should be file:///, not just //, see the mentioned RFC 8089.

Copy link
Owner Author

@dandavison dandavison Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying that

        default_value = "file://{path}",

is incorrect? {path} is documented as being an absolute file path, so on Unix will have a leading slash; if we change the default here to 3 slashes then wouldn't we need to make a code change to avoid 4 slashes when concatenating the path? Also we use "file-line://{path}:{line}" as an example in the text (two slashes).

I think I struggle to think in non-unix terms -- I'm guilty of thinking of "absolute path" as synonymous with "starts with slash".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the path contains the third /, then it is fine.

Copy link
Owner Author

@dandavison dandavison Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind if we address this in a separate PR? I'm doubtless being stupid but it seems to me that if {path} is an absolute path, such as /etc/fstab then it's correct as-is, yielding file:///etc/fstab. I'd prefer not to make a drive-by change that's not related to the PR, without understanding the correctness implications.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is okay as-is, there is nothing to address :)

See debug_assert!(absolute_path.as_ref().is_absolute());

///
/// The placeholders "{path}" and "{line}" will be replaced by the absolute file path and the
/// line number, respectively. The default value of this option creates hyperlinks using
/// standard file URLs; your operating system should open these in the application registered
/// for that file type. However, these do not make use of the line number. In order for the link
/// to open the file at the correct line number, you could use a custom URL format such as
/// "file-line://{path}:{line}" and register an application to handle the custom "file-line" URL
/// scheme by opening the file in your editor/IDE at the indicated line number. See
/// <https://github.com/dandavison/open-in-editor> for an example.
/// The placeholders "{host}", "{path}", and "{line}" will be replaced by the hostname,
/// absolute file path and the line number, respectively. The default value of this option
/// creates hyperlinks using standard file URLs; your operating system should open these in the
/// application registered for that file type. However, these do not make use of the line
/// number. In order for the link to open the file at the correct line number, you could use a
/// custom URL format such as "file-line://{path}:{line}" and register an application to handle
/// the custom "file-line" URL scheme by opening the file in your editor/IDE at the indicated
/// line number. See <https://github.com/dandavison/open-in-editor> for an example.
pub hyperlinks_file_link_format: String,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to reduce the text a bit, what about:


Placeholders "{path}" and "{line}" will be replaced by the absolute file path and the line number; "{host}" with the hostname delta is currently running on.

The default "file:" scheme creates hyperlinks using standard file URI with only the filename, which your terminal or OS should handle. You can specify any scheme, such as "file-line://{path}:{line}" and register an application to handle it. See <https://dandavison.github.io/delta/hyperlinks.html> for details.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks for doing that, I wasn't happy with it either. I've pushed a version very similar to what you wrote.


#[arg(
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct Config {
pub grep_output_type: Option<GrepType>,
pub grep_separator_symbol: String,
pub handle_merge_conflicts: bool,
pub hostname: Option<String>,
pub hunk_header_file_style: Style,
pub hunk_header_line_number_style: Style,
pub hunk_header_style_include_file_path: HunkHeaderIncludeFilePath,
Expand Down Expand Up @@ -326,6 +327,7 @@ impl From<cli::Opt> for Config {
grep_output_type,
grep_separator_symbol: opt.grep_separator_symbol,
handle_merge_conflicts: !opt.raw,
hostname: opt.env.hostname,
hunk_header_file_style: styles["hunk-header-file-style"],
hunk_header_line_number_style: styles["hunk-header-line-number-style"],
hunk_header_style: styles["hunk-header-style"],
Expand Down
7 changes: 7 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct DeltaEnv {
pub features: Option<String>,
pub git_config_parameters: Option<String>,
pub git_prefix: Option<String>,
pub hostname: Option<String>,
pub navigate: Option<String>,
pub pagers: (Option<String>, Option<String>),
}
Expand All @@ -33,6 +34,7 @@ impl DeltaEnv {
let features = env::var(DELTA_FEATURES).ok();
let git_config_parameters = env::var(GIT_CONFIG_PARAMETERS).ok();
let git_prefix = env::var(GIT_PREFIX).ok();
let hostname = hostname();
let navigate = env::var(DELTA_NAVIGATE).ok();

let current_dir = env::current_dir().ok();
Expand All @@ -53,12 +55,17 @@ impl DeltaEnv {
features,
git_config_parameters,
git_prefix,
hostname,
navigate,
pagers,
}
}
}

fn hostname() -> Option<String> {
grep_cli::hostname().ok()?.to_str().map(|s| s.to_string())
}

#[cfg(test)]
pub mod tests {
use super::DeltaEnv;
Expand Down
3 changes: 3 additions & 0 deletions src/features/hyperlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ where
let mut url = config
.hyperlinks_file_link_format
.replace("{path}", &absolute_path.as_ref().to_string_lossy());
if let Some(host) = &config.hostname {
url = url.replace("{host}", host)
}
if let Some(n) = line_number {
url = url.replace("{line}", &format!("{n}"))
} else {
Expand Down
Loading