-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format files and commits as OSC 8 hyperlinks
Closes #257
- Loading branch information
1 parent
0145e5d
commit cca9738
Showing
10 changed files
with
234 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::borrow::Cow; | ||
|
||
use lazy_static::lazy_static; | ||
use regex::{Captures, Regex}; | ||
|
||
use crate::config::Config; | ||
use crate::features::OptionValueFunction; | ||
use crate::git_config_entry::{GitConfigEntry, GitRemoteRepo}; | ||
|
||
pub fn make_feature() -> Vec<(String, OptionValueFunction)> { | ||
builtin_feature!([ | ||
( | ||
"hyperlinks", | ||
bool, | ||
None, | ||
_opt => true | ||
) | ||
]) | ||
} | ||
|
||
pub fn format_commit_line_with_osc8_commit_hyperlink<'a>( | ||
line: &'a str, | ||
config: &Config, | ||
) -> Cow<'a, str> { | ||
if let Some(GitConfigEntry::GitRemote(GitRemoteRepo::GitHubRepo(repo))) = | ||
config.git_config_entries.get("remote.origin.url") | ||
{ | ||
COMMIT_LINE_REGEX.replace(line, |captures: &Captures| { | ||
format_commit_line_captures_with_osc8_commit_hyperlink(captures, repo) | ||
}) | ||
} else { | ||
Cow::from(line) | ||
} | ||
} | ||
|
||
/// Create a file hyperlink to `path`, displaying `text`. | ||
pub fn format_osc8_file_hyperlink<'a>( | ||
relative_path: &'a str, | ||
line_number: Option<usize>, | ||
text: &str, | ||
config: &Config, | ||
) -> Cow<'a, str> { | ||
if let Some(GitConfigEntry::Path(workdir)) = config.git_config_entries.get("delta.__workdir__") | ||
{ | ||
let absolute_path = workdir.join(relative_path); | ||
let mut url = config | ||
.hyperlinks_file_link_format | ||
.replace("{path}", &absolute_path.to_string_lossy()); | ||
if let Some(n) = line_number { | ||
url = url.replace("{line_number}", &format!("{}", n)) | ||
} else { | ||
url = url.replace("{line_number}", "") | ||
}; | ||
Cow::from(format!( | ||
"{osc}8;;{url}{st}{text}{osc}8;;{st}", | ||
url = url, | ||
text = text, | ||
osc = "\x1b]", | ||
st = "\x1b\\" | ||
)) | ||
} else { | ||
Cow::from(relative_path) | ||
} | ||
} | ||
|
||
lazy_static! { | ||
static ref COMMIT_LINE_REGEX: Regex = Regex::new("(.* )([0-9a-f]{40})(.*)").unwrap(); | ||
} | ||
|
||
fn format_commit_line_captures_with_osc8_commit_hyperlink<'a, 'b>( | ||
captures: &'a Captures, | ||
github_repo: &'b str, | ||
) -> String { | ||
let commit = captures.get(2).unwrap().as_str(); | ||
format!( | ||
"{prefix}{osc}8;;{url}{st}{commit}{osc}8;;{st}{suffix}", | ||
url = format_github_commit_url(commit, github_repo), | ||
commit = commit, | ||
prefix = captures.get(1).unwrap().as_str(), | ||
suffix = captures.get(3).unwrap().as_str(), | ||
osc = "\x1b]", | ||
st = "\x1b\\" | ||
) | ||
} | ||
|
||
fn format_github_commit_url(commit: &str, github_repo: &str) -> String { | ||
format!("https://github.com/{}/commit/{}", github_repo, commit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::borrow::Cow; | ||
|
||
use atty; | ||
|
||
use crate::config::Config; | ||
use crate::features; | ||
|
||
/// If output is going to a tty, emit hyperlinks if requested. | ||
// Although raw output should basically be emitted unaltered, we do this. | ||
pub fn format_raw_line<'a>(line: &'a str, config: &Config) -> Cow<'a, str> { | ||
if config.hyperlinks && atty::is(atty::Stream::Stdout) { | ||
features::hyperlinks::format_commit_line_with_osc8_commit_hyperlink(line, config) | ||
} else { | ||
Cow::from(line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::path::PathBuf; | ||
use std::result::Result; | ||
use std::str::FromStr; | ||
|
||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
|
||
use crate::errors::*; | ||
use crate::style::Style; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum GitConfigEntry { | ||
Style(Style), | ||
GitRemote(GitRemoteRepo), | ||
Path(PathBuf), | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum GitRemoteRepo { | ||
GitHubRepo(String), | ||
} | ||
|
||
lazy_static! { | ||
static ref GITHUB_REMOTE_URL: Regex = Regex::new(r"github\.com[:/]([^/]+)/(.+)\.git").unwrap(); | ||
} | ||
|
||
impl FromStr for GitRemoteRepo { | ||
type Err = Error; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if let Some(caps) = GITHUB_REMOTE_URL.captures(s) { | ||
Ok(Self::GitHubRepo(format!( | ||
"{user}/{repo}", | ||
user = caps.get(1).unwrap().as_str(), | ||
repo = caps.get(2).unwrap().as_str() | ||
))) | ||
} else { | ||
Err("Not a GitHub repo.".into()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters