summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2023-11-22 16:23:35 -0500
committerAndrew Gallant <jamslam@gmail.com>2023-11-25 15:03:53 -0500
commit286de9564e511f01f13756c207e9b26ec781591d (patch)
tree44dddf7fa54b2cca271a5b98ff021cfb9569194b
parent038524a580072e9346ee9f782679618e380f4f2e (diff)
cli: rejigger --version to include PCRE2 info
This adds info about whether PCRE2 is available or not to the output of --version. Essentially, --version now subsumes --pcre2-version, although we do retain the former because it (usefully) emits an exit code based on whether PCRE2 is available or not. Closes #2645
-rw-r--r--CHANGELOG.md2
-rw-r--r--crates/core/flags/doc/version.rs32
-rw-r--r--crates/core/flags/mod.rs1
-rw-r--r--crates/core/main.rs34
4 files changed, 44 insertions, 25 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb46bbad..e91102bb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,6 +44,8 @@ Feature enhancements:
The `--debug` flag now indicates whether stdin or `./` is being searched.
* [FEATURE #2643](https://github.com/BurntSushi/ripgrep/issues/2643):
Make `-d` a short flag for `--max-depth`.
+* [FEATURE #2645](https://github.com/BurntSushi/ripgrep/issues/2645):
+ The `--version` output will now also contain PCRE2 availability information.
Bug fixes:
diff --git a/crates/core/flags/doc/version.rs b/crates/core/flags/doc/version.rs
index ce462837..8e52861f 100644
--- a/crates/core/flags/doc/version.rs
+++ b/crates/core/flags/doc/version.rs
@@ -33,6 +33,7 @@ pub(crate) fn generate_long() -> String {
let mut out = String::new();
writeln!(out, "{}", generate_short()).unwrap();
+ writeln!(out).unwrap();
writeln!(out, "features:{}", features().join(",")).unwrap();
if !compile.is_empty() {
writeln!(out, "simd(compile):{}", compile.join(",")).unwrap();
@@ -40,9 +41,40 @@ pub(crate) fn generate_long() -> String {
if !runtime.is_empty() {
writeln!(out, "simd(runtime):{}", runtime.join(",")).unwrap();
}
+ let (pcre2_version, _) = generate_pcre2();
+ writeln!(out, "\n{pcre2_version}").unwrap();
out
}
+/// Generates multi-line version string with PCRE2 information.
+///
+/// This also returns whether PCRE2 is actually available in this build of
+/// ripgrep.
+pub(crate) fn generate_pcre2() -> (String, bool) {
+ let mut out = String::new();
+
+ #[cfg(feature = "pcre2")]
+ {
+ use grep::pcre2;
+
+ let (major, minor) = pcre2::version();
+ write!(out, "PCRE2 {}.{} is available", major, minor).unwrap();
+ if cfg!(target_pointer_width = "64") && pcre2::is_jit_available() {
+ writeln!(out, " (JIT is available)").unwrap();
+ } else {
+ writeln!(out, " (JIT is unavailable)").unwrap();
+ }
+ (out, true)
+ }
+
+ #[cfg(not(feature = "pcre2"))]
+ {
+ writeln!(out, "PCRE2 is not available in this build of ripgrep.")
+ .unwrap();
+ (out, false)
+ }
+}
+
/// Returns the relevant SIMD features supported by the CPU at runtime.
///
/// This is kind of a dirty violation of abstraction, since it assumes
diff --git a/crates/core/flags/mod.rs b/crates/core/flags/mod.rs
index aefe1638..e2b0ccb8 100644
--- a/crates/core/flags/mod.rs
+++ b/crates/core/flags/mod.rs
@@ -30,6 +30,7 @@ pub(crate) use crate::flags::{
man::generate as generate_man_page,
version::{
generate_long as generate_version_long,
+ generate_pcre2 as generate_version_pcre2,
generate_short as generate_version_short,
},
},
diff --git a/crates/core/main.rs b/crates/core/main.rs
index ab2230de..64f35ceb 100644
--- a/crates/core/main.rs
+++ b/crates/core/main.rs
@@ -383,6 +383,7 @@ fn generate(mode: crate::flags::GenerateMode) -> anyhow::Result<ExitCode> {
fn special(mode: crate::flags::SpecialMode) -> anyhow::Result<ExitCode> {
use crate::flags::SpecialMode;
+ let mut exit = ExitCode::from(0);
let output = match mode {
SpecialMode::HelpShort => flags::generate_help_short(),
SpecialMode::HelpLong => flags::generate_help_long(),
@@ -390,33 +391,16 @@ fn special(mode: crate::flags::SpecialMode) -> anyhow::Result<ExitCode> {
SpecialMode::VersionLong => flags::generate_version_long(),
// --pcre2-version is a little special because it emits an error
// exit code if this build of ripgrep doesn't support PCRE2.
- SpecialMode::VersionPCRE2 => return version_pcre2(),
+ SpecialMode::VersionPCRE2 => {
+ let (output, available) = flags::generate_version_pcre2();
+ if !available {
+ exit = ExitCode::from(1);
+ }
+ output
+ }
};
writeln!(std::io::stdout(), "{}", output.trim_end())?;
- Ok(ExitCode::from(0))
-}
-
-/// The top-level entry point for `--pcre2-version`.
-fn version_pcre2() -> anyhow::Result<ExitCode> {
- let mut stdout = std::io::stdout().lock();
-
- #[cfg(feature = "pcre2")]
- {
- use grep::pcre2;
-
- let (major, minor) = pcre2::version();
- writeln!(stdout, "PCRE2 {}.{} is available", major, minor)?;
- if cfg!(target_pointer_width = "64") && pcre2::is_jit_available() {
- writeln!(stdout, "JIT is available")?;
- }
- Ok(ExitCode::from(0))
- }
-
- #[cfg(not(feature = "pcre2"))]
- {
- writeln!(stdout, "PCRE2 is not available in this build of ripgrep.")?;
- Ok(ExitCode::from(1))
- }
+ Ok(exit)
}
/// Prints a heuristic error messages when nothing is searched.