-
Notifications
You must be signed in to change notification settings - Fork 419
Support external subcommands: rg, diff, git-show (etc.) #1769
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ use std::ffi::OsString; | |||||||
use std::path::{Path, PathBuf}; | ||||||||
|
||||||||
use bat::assets::HighlightingAssets; | ||||||||
use clap::error::Error; | ||||||||
use clap::{ArgMatches, ColorChoice, CommandFactory, FromArgMatches, Parser, ValueEnum, ValueHint}; | ||||||||
use clap_complete::Shell; | ||||||||
use console::Term; | ||||||||
|
@@ -16,6 +17,7 @@ use crate::config::delta_unreachable; | |||||||
use crate::env::DeltaEnv; | ||||||||
use crate::git_config::GitConfig; | ||||||||
use crate::options; | ||||||||
use crate::subcommands; | ||||||||
use crate::utils; | ||||||||
use crate::utils::bat::output::PagingMode; | ||||||||
|
||||||||
|
@@ -1215,23 +1217,12 @@ pub enum DetectDarkLight { | |||||||
#[derive(Debug)] | ||||||||
pub enum Call<T> { | ||||||||
Delta(T), | ||||||||
DeltaDiff(T, PathBuf, PathBuf), | ||||||||
SubCommand(T, subcommands::SubCommand), | ||||||||
Help(String), | ||||||||
Version(String), | ||||||||
} | ||||||||
|
||||||||
// Custom conversion because a) generic TryFrom<A,B> is not possible and | ||||||||
// b) the Delta(T) variant can't be converted. | ||||||||
impl<A> Call<A> { | ||||||||
fn try_convert<B>(self) -> Option<Call<B>> { | ||||||||
use Call::*; | ||||||||
match self { | ||||||||
Delta(_) => None, | ||||||||
Help(help) => Some(Help(help)), | ||||||||
Version(ver) => Some(Version(ver)), | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
impl Opt { | ||||||||
fn handle_help_and_version(args: &[OsString]) -> Call<ArgMatches> { | ||||||||
match Self::command().try_get_matches_from(args) { | ||||||||
|
@@ -1281,31 +1272,55 @@ impl Opt { | |||||||
Call::Help(help) | ||||||||
} | ||||||||
Err(e) => { | ||||||||
e.exit(); | ||||||||
// Calls `e.exit()` if error persists. | ||||||||
let (matches, subcmd) = subcommands::extract(args, e); | ||||||||
Call::SubCommand(matches, subcmd) | ||||||||
} | ||||||||
Ok(matches) => { | ||||||||
// subcommands take precedence over diffs | ||||||||
let minus_file = matches.get_one::<PathBuf>("minus_file").map(PathBuf::from); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is our support for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything clap parses takes precedence, in fact, here intervention is required to "steal" this argument from the command line parser. |
||||||||
if let Some(subcmd) = &minus_file { | ||||||||
if let Some(arg) = subcmd.to_str() { | ||||||||
Comment on lines
+1282
to
+1283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is basically just code golf, done while trying to get into the substance of the PR. Feel free to ignore.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's wait for if-let chains :) |
||||||||
if subcommands::SUBCOMMANDS.contains(&arg) { | ||||||||
let unreachable_error = | ||||||||
Error::new(clap::error::ErrorKind::InvalidSubcommand); | ||||||||
let (matches, subcmd) = subcommands::extract(args, unreachable_error); | ||||||||
return Call::SubCommand(matches, subcmd); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
match ( | ||||||||
minus_file, | ||||||||
matches.get_one::<PathBuf>("plus_file").map(PathBuf::from), | ||||||||
) { | ||||||||
(Some(minus_file), Some(plus_file)) => { | ||||||||
Call::DeltaDiff(matches, minus_file, plus_file) | ||||||||
} | ||||||||
_ => Call::Delta(matches), | ||||||||
} | ||||||||
} | ||||||||
Ok(matches) => Call::Delta(matches), | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
pub fn from_args_and_git_config( | ||||||||
args: Vec<OsString>, | ||||||||
env: &DeltaEnv, | ||||||||
assets: HighlightingAssets, | ||||||||
) -> Call<Self> { | ||||||||
) -> (Call<()>, Option<Opt>) { | ||||||||
#[cfg(test)] | ||||||||
// Set argv[0] when called in tests: | ||||||||
let args = { | ||||||||
let mut args = args; | ||||||||
args.insert(0, OsString::from("delta")); | ||||||||
args | ||||||||
}; | ||||||||
let matches = match Self::handle_help_and_version(&args) { | ||||||||
Call::Delta(t) => t, | ||||||||
msg => { | ||||||||
return msg | ||||||||
.try_convert() | ||||||||
.unwrap_or_else(|| panic!("Call<_> conversion failed")) | ||||||||
} | ||||||||
let (matches, call) = match Self::handle_help_and_version(&args) { | ||||||||
Call::Delta(t) => (t, Call::Delta(())), | ||||||||
Call::DeltaDiff(t, a, b) => (t, Call::DeltaDiff((), a, b)), | ||||||||
Call::SubCommand(t, cmd) => (t, Call::SubCommand((), cmd)), | ||||||||
Call::Help(help) => return (Call::Help(help), None), | ||||||||
Call::Version(ver) => return (Call::Version(ver), None), | ||||||||
}; | ||||||||
|
||||||||
let mut final_config = if *matches.get_one::<bool>("no_gitconfig").unwrap_or(&false) { | ||||||||
|
@@ -1321,12 +1336,8 @@ impl Opt { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
Call::Delta(Self::from_clap_and_git_config( | ||||||||
env, | ||||||||
matches, | ||||||||
final_config, | ||||||||
assets, | ||||||||
)) | ||||||||
let opt = Self::from_clap_and_git_config(env, matches, final_config, assets); | ||||||||
(call, Some(opt)) | ||||||||
} | ||||||||
|
||||||||
pub fn from_iter_and_git_config<I>( | ||||||||
|
@@ -1397,9 +1408,3 @@ lazy_static! { | |||||||
.into_iter() | ||||||||
.collect(); | ||||||||
} | ||||||||
|
||||||||
// Call::Help(format!( | ||||||||
// "foo\nbar\nbatz\n{}\n{}", | ||||||||
// help.replace("Options:", "well well\n\nOptions:"), | ||||||||
// h2 | ||||||||
// )) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clever...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that... praise? It must be, so elegant!