Merge pull request #4833 from epage/plugin

refactor: Track term widths as plugins
This commit is contained in:
Ed Page 2023-04-13 16:09:03 -05:00 committed by GitHub
commit 0501a63c21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 259 additions and 30 deletions

View file

@ -1,3 +1,6 @@
#[cfg(debug_assertions)]
use crate::util::AnyValueId;
/// Behavior of arguments when they are encountered while parsing /// Behavior of arguments when they are encountered while parsing
/// ///
/// # Examples /// # Examples
@ -337,9 +340,7 @@ impl ArgAction {
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub(crate) fn value_type_id(&self) -> Option<crate::parser::AnyValueId> { pub(crate) fn value_type_id(&self) -> Option<AnyValueId> {
use crate::parser::AnyValueId;
match self { match self {
Self::Set => None, Self::Set => None,
Self::Append => None, Self::Append => None,

View file

@ -18,6 +18,7 @@ use crate::builder::PossibleValue;
use crate::builder::Str; use crate::builder::Str;
use crate::builder::StyledStr; use crate::builder::StyledStr;
use crate::builder::ValueRange; use crate::builder::ValueRange;
use crate::util::AnyValueId;
use crate::ArgAction; use crate::ArgAction;
use crate::Id; use crate::Id;
use crate::ValueHint; use crate::ValueHint;
@ -4004,7 +4005,7 @@ impl Arg {
self.value_hint.unwrap_or_else(|| { self.value_hint.unwrap_or_else(|| {
if self.is_takes_value_set() { if self.is_takes_value_set() {
let type_id = self.get_value_parser().type_id(); let type_id = self.get_value_parser().type_id();
if type_id == crate::parser::AnyValueId::of::<std::path::PathBuf>() { if type_id == AnyValueId::of::<std::path::PathBuf>() {
ValueHint::AnyPath ValueHint::AnyPath
} else { } else {
ValueHint::default() ValueHint::default()

View file

@ -11,6 +11,7 @@ use std::path::Path;
// Internal // Internal
use crate::builder::app_settings::{AppFlags, AppSettings}; use crate::builder::app_settings::{AppFlags, AppSettings};
use crate::builder::arg_settings::ArgSettings; use crate::builder::arg_settings::ArgSettings;
use crate::builder::ext::Extensions;
use crate::builder::ArgAction; use crate::builder::ArgAction;
use crate::builder::IntoResettable; use crate::builder::IntoResettable;
use crate::builder::PossibleValue; use crate::builder::PossibleValue;
@ -90,8 +91,6 @@ pub struct Command {
usage_name: Option<String>, usage_name: Option<String>,
help_str: Option<StyledStr>, help_str: Option<StyledStr>,
disp_ord: Option<usize>, disp_ord: Option<usize>,
term_w: Option<usize>,
max_w: Option<usize>,
#[cfg(feature = "help")] #[cfg(feature = "help")]
template: Option<StyledStr>, template: Option<StyledStr>,
settings: AppFlags, settings: AppFlags,
@ -105,6 +104,7 @@ pub struct Command {
subcommand_heading: Option<Str>, subcommand_heading: Option<Str>,
external_value_parser: Option<super::ValueParser>, external_value_parser: Option<super::ValueParser>,
long_help_exists: bool, long_help_exists: bool,
app_ext: Extensions,
} }
/// # Basic API /// # Basic API
@ -1104,7 +1104,7 @@ impl Command {
#[must_use] #[must_use]
#[cfg(any(not(feature = "unstable-v5"), feature = "wrap_help"))] #[cfg(any(not(feature = "unstable-v5"), feature = "wrap_help"))]
pub fn term_width(mut self, width: usize) -> Self { pub fn term_width(mut self, width: usize) -> Self {
self.term_w = Some(width); self.app_ext.set(TermWidth(width));
self self
} }
@ -1131,8 +1131,8 @@ impl Command {
#[inline] #[inline]
#[must_use] #[must_use]
#[cfg(any(not(feature = "unstable-v5"), feature = "wrap_help"))] #[cfg(any(not(feature = "unstable-v5"), feature = "wrap_help"))]
pub fn max_term_width(mut self, w: usize) -> Self { pub fn max_term_width(mut self, width: usize) -> Self {
self.max_w = Some(w); self.app_ext.set(MaxTermWidth(width));
self self
} }
@ -3720,12 +3720,12 @@ impl Command {
#[cfg(feature = "help")] #[cfg(feature = "help")]
pub(crate) fn get_term_width(&self) -> Option<usize> { pub(crate) fn get_term_width(&self) -> Option<usize> {
self.term_w self.app_ext.get::<TermWidth>().map(|e| e.0)
} }
#[cfg(feature = "help")] #[cfg(feature = "help")]
pub(crate) fn get_max_term_width(&self) -> Option<usize> { pub(crate) fn get_max_term_width(&self) -> Option<usize> {
self.max_w self.app_ext.get::<MaxTermWidth>().map(|e| e.0)
} }
pub(crate) fn get_keymap(&self) -> &MKeyMap { pub(crate) fn get_keymap(&self) -> &MKeyMap {
@ -4190,8 +4190,7 @@ impl Command {
sc.settings = sc.settings | self.g_settings; sc.settings = sc.settings | self.g_settings;
sc.g_settings = sc.g_settings | self.g_settings; sc.g_settings = sc.g_settings | self.g_settings;
sc.term_w = self.term_w; sc.app_ext.update(&self.app_ext);
sc.max_w = self.max_w;
} }
} }
@ -4612,8 +4611,6 @@ impl Default for Command {
usage_name: Default::default(), usage_name: Default::default(),
help_str: Default::default(), help_str: Default::default(),
disp_ord: Default::default(), disp_ord: Default::default(),
term_w: Default::default(),
max_w: Default::default(),
#[cfg(feature = "help")] #[cfg(feature = "help")]
template: Default::default(), template: Default::default(),
settings: Default::default(), settings: Default::default(),
@ -4627,6 +4624,7 @@ impl Default for Command {
subcommand_heading: Default::default(), subcommand_heading: Default::default(),
external_value_parser: Default::default(), external_value_parser: Default::default(),
long_help_exists: false, long_help_exists: false,
app_ext: Default::default(),
} }
} }
} }
@ -4651,6 +4649,18 @@ impl fmt::Display for Command {
} }
} }
trait AppTag: crate::builder::ext::Extension {}
#[derive(Default, Copy, Clone, Debug)]
struct TermWidth(usize);
impl AppTag for TermWidth {}
#[derive(Default, Copy, Clone, Debug)]
struct MaxTermWidth(usize);
impl AppTag for MaxTermWidth {}
fn two_elements_of<I, T>(mut iter: I) -> Option<(T, T)> fn two_elements_of<I, T>(mut iter: I) -> Option<(T, T)>
where where
I: Iterator<Item = T>, I: Iterator<Item = T>,

View file

@ -0,0 +1,216 @@
use crate::util::AnyValueId;
use crate::util::FlatMap;
#[derive(Default, Clone, Debug)]
pub(crate) struct Extensions {
extensions: FlatMap<AnyValueId, BoxedExtension>,
}
impl Extensions {
#[allow(dead_code)]
pub(crate) fn get<T: Extension>(&self) -> Option<&T> {
let id = AnyValueId::of::<T>();
self.extensions.get(&id).map(|e| e.as_ref::<T>())
}
#[allow(dead_code)]
pub(crate) fn get_mut<T: Extension>(&mut self) -> Option<&mut T> {
let id = AnyValueId::of::<T>();
self.extensions.get_mut(&id).map(|e| e.as_mut::<T>())
}
#[allow(dead_code)]
pub(crate) fn get_or_insert_default<T: Extension + Default>(&mut self) -> &mut T {
let id = AnyValueId::of::<T>();
self.extensions
.entry(id)
.or_insert_with(|| BoxedExtension::new(T::default()))
.as_mut::<T>()
}
#[allow(dead_code)]
pub(crate) fn set<T: Extension + Into<BoxedEntry>>(&mut self, tagged: T) -> bool {
let BoxedEntry { id, value } = tagged.into();
self.extensions.insert(id, value).is_some()
}
#[allow(dead_code)]
pub(crate) fn remove<T: Extension>(&mut self) -> Option<Box<dyn Extension>> {
let id = AnyValueId::of::<T>();
self.extensions.remove(&id).map(BoxedExtension::into_inner)
}
pub(crate) fn update(&mut self, other: &Self) {
for (key, value) in other.extensions.iter() {
self.extensions.insert(*key, value.clone());
}
}
}
/// Supports conversion to `Any`. Traits to be extended by `impl_downcast!` must extend `Extension`.
pub(crate) trait Extension: std::fmt::Debug + Send + Sync + 'static {
/// Convert `Box<dyn Trait>` (where `Trait: Extension`) to `Box<dyn Any>`.
///
/// `Box<dyn Any>` can /// then be further `downcast` into
/// `Box<ConcreteType>` where `ConcreteType` implements `Trait`.
fn into_any(self: Box<Self>) -> Box<dyn std::any::Any>;
/// Clone `&Box<dyn Trait>` (where `Trait: Extension`) to `Box<dyn Extension>`.
///
/// `Box<dyn Any>` can /// then be further `downcast` into
// `Box<ConcreteType>` where `ConcreteType` implements `Trait`.
fn clone_extension(&self) -> Box<dyn Extension>;
/// Convert `&Trait` (where `Trait: Extension`) to `&Any`.
///
/// This is needed since Rust cannot /// generate `&Any`'s vtable from
/// `&Trait`'s.
fn as_any(&self) -> &dyn std::any::Any;
/// Convert `&mut Trait` (where `Trait: Extension`) to `&Any`.
///
/// This is needed since Rust cannot /// generate `&mut Any`'s vtable from
/// `&mut Trait`'s.
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}
impl<T> Extension for T
where
T: Clone + std::fmt::Debug + Send + Sync + 'static,
{
fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
self
}
fn clone_extension(&self) -> Box<dyn Extension> {
Box::new(self.clone())
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
impl Clone for Box<dyn Extension> {
fn clone(&self) -> Self {
self.as_ref().clone_extension()
}
}
#[derive(Clone)]
#[repr(transparent)]
struct BoxedExtension(Box<dyn Extension>);
impl BoxedExtension {
fn new<T: Extension>(inner: T) -> Self {
Self(Box::new(inner))
}
fn into_inner(self) -> Box<dyn Extension> {
self.0
}
fn as_ref<T: Extension>(&self) -> &T {
self.0.as_ref().as_any().downcast_ref::<T>().unwrap()
}
fn as_mut<T: Extension>(&mut self) -> &mut T {
self.0.as_mut().as_any_mut().downcast_mut::<T>().unwrap()
}
}
impl std::fmt::Debug for BoxedExtension {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.0.fmt(f)
}
}
#[derive(Clone)]
pub(crate) struct BoxedEntry {
id: AnyValueId,
value: BoxedExtension,
}
impl BoxedEntry {
pub(crate) fn new(r: impl Extension) -> Self {
let id = AnyValueId::from(&r);
let value = BoxedExtension::new(r);
BoxedEntry { id, value }
}
}
impl<R: Extension> From<R> for BoxedEntry {
fn from(inner: R) -> Self {
BoxedEntry::new(inner)
}
}
#[cfg(test)]
mod test {
use super::*;
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
struct Number(usize);
#[test]
fn get() {
let mut ext = Extensions::default();
ext.set(Number(10));
assert_eq!(ext.get::<Number>(), Some(&Number(10)));
}
#[test]
fn get_mut() {
let mut ext = Extensions::default();
ext.set(Number(10));
*ext.get_mut::<Number>().unwrap() = Number(20);
assert_eq!(ext.get::<Number>(), Some(&Number(20)));
}
#[test]
fn get_or_insert_default_empty() {
let mut ext = Extensions::default();
assert_eq!(ext.get_or_insert_default::<Number>(), &Number(0));
}
#[test]
fn get_or_insert_default_full() {
let mut ext = Extensions::default();
ext.set(Number(10));
assert_eq!(ext.get_or_insert_default::<Number>(), &Number(10));
}
#[test]
fn set() {
let mut ext = Extensions::default();
assert!(!ext.set(Number(10)));
assert_eq!(ext.get::<Number>(), Some(&Number(10)));
assert!(ext.set(Number(20)));
assert_eq!(ext.get::<Number>(), Some(&Number(20)));
}
#[test]
fn reset() {
let mut ext = Extensions::default();
assert_eq!(ext.get::<Number>(), None);
assert!(ext.remove::<Number>().is_none());
assert_eq!(ext.get::<Number>(), None);
assert!(!ext.set(Number(10)));
assert_eq!(ext.get::<Number>(), Some(&Number(10)));
assert!(ext.remove::<Number>().is_some());
assert_eq!(ext.get::<Number>(), None);
}
#[test]
fn update() {
let mut ext = Extensions::default();
assert_eq!(ext.get::<Number>(), None);
let mut new = Extensions::default();
assert!(!new.set(Number(10)));
ext.update(&new);
assert_eq!(ext.get::<Number>(), Some(&Number(10)));
}
}

View file

@ -7,6 +7,7 @@ mod arg_group;
mod arg_predicate; mod arg_predicate;
mod arg_settings; mod arg_settings;
mod command; mod command;
mod ext;
mod os_str; mod os_str;
mod possible_value; mod possible_value;
mod range; mod range;

View file

@ -1,8 +1,8 @@
use std::convert::TryInto; use std::convert::TryInto;
use std::ops::RangeBounds; use std::ops::RangeBounds;
use crate::parser::AnyValue; use crate::util::AnyValue;
use crate::parser::AnyValueId; use crate::util::AnyValueId;
/// Parse/validate argument values /// Parse/validate argument values
/// ///

View file

@ -5,10 +5,10 @@ use std::ops::Deref;
// Internal // Internal
use crate::builder::{Arg, ArgPredicate, Command}; use crate::builder::{Arg, ArgPredicate, Command};
use crate::parser::AnyValue;
use crate::parser::Identifier; use crate::parser::Identifier;
use crate::parser::PendingArg; use crate::parser::PendingArg;
use crate::parser::{ArgMatches, MatchedArg, SubCommand, ValueSource}; use crate::parser::{ArgMatches, MatchedArg, SubCommand, ValueSource};
use crate::util::AnyValue;
use crate::util::FlatMap; use crate::util::FlatMap;
use crate::util::Id; use crate::util::Id;
use crate::INTERNAL_ERROR_MSG; use crate::INTERNAL_ERROR_MSG;

View file

@ -1,3 +1,5 @@
use crate::util::AnyValueId;
/// Violation of [`ArgMatches`][crate::ArgMatches] assumptions /// Violation of [`ArgMatches`][crate::ArgMatches] assumptions
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[allow(missing_copy_implementations)] // We might add non-Copy types in the future #[allow(missing_copy_implementations)] // We might add non-Copy types in the future
@ -7,9 +9,9 @@ pub enum MatchesError {
#[non_exhaustive] #[non_exhaustive]
Downcast { Downcast {
/// Type for value stored in [`ArgMatches`][crate::ArgMatches] /// Type for value stored in [`ArgMatches`][crate::ArgMatches]
actual: super::AnyValueId, actual: AnyValueId,
/// The target type to downcast to /// The target type to downcast to
expected: super::AnyValueId, expected: AnyValueId,
}, },
/// Argument not defined in [`Command`][crate::Command] /// Argument not defined in [`Command`][crate::Command]
#[non_exhaustive] #[non_exhaustive]

View file

@ -8,11 +8,11 @@ use std::slice::Iter;
// Internal // Internal
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
use crate::builder::Str; use crate::builder::Str;
use crate::parser::AnyValue;
use crate::parser::AnyValueId;
use crate::parser::MatchedArg; use crate::parser::MatchedArg;
use crate::parser::MatchesError; use crate::parser::MatchesError;
use crate::parser::ValueSource; use crate::parser::ValueSource;
use crate::util::AnyValue;
use crate::util::AnyValueId;
use crate::util::FlatMap; use crate::util::FlatMap;
use crate::util::Id; use crate::util::Id;
use crate::INTERNAL_ERROR_MSG; use crate::INTERNAL_ERROR_MSG;

View file

@ -6,10 +6,10 @@ use std::{
}; };
use crate::builder::ArgPredicate; use crate::builder::ArgPredicate;
use crate::parser::AnyValue;
use crate::parser::AnyValueId;
use crate::parser::ValueSource; use crate::parser::ValueSource;
use crate::util::eq_ignore_case; use crate::util::eq_ignore_case;
use crate::util::AnyValue;
use crate::util::AnyValueId;
use crate::INTERNAL_ERROR_MSG; use crate::INTERNAL_ERROR_MSG;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -1,9 +1,7 @@
mod any_value;
mod arg_matches; mod arg_matches;
mod matched_arg; mod matched_arg;
mod value_source; mod value_source;
pub use any_value::AnyValueId;
pub use arg_matches::IdsRef; pub use arg_matches::IdsRef;
pub use arg_matches::RawValues; pub use arg_matches::RawValues;
pub use arg_matches::Values; pub use arg_matches::Values;
@ -11,6 +9,5 @@ pub use arg_matches::ValuesRef;
pub use arg_matches::{ArgMatches, Indices}; pub use arg_matches::{ArgMatches, Indices};
pub use value_source::ValueSource; pub use value_source::ValueSource;
pub(crate) use any_value::AnyValue;
pub(crate) use arg_matches::SubCommand; pub(crate) use arg_matches::SubCommand;
pub(crate) use matched_arg::MatchedArg; pub(crate) use matched_arg::MatchedArg;

View file

@ -10,8 +10,6 @@ mod validator;
pub(crate) mod features; pub(crate) mod features;
pub(crate) use self::arg_matcher::ArgMatcher; pub(crate) use self::arg_matcher::ArgMatcher;
pub(crate) use self::matches::AnyValue;
pub(crate) use self::matches::AnyValueId;
pub(crate) use self::matches::{MatchedArg, SubCommand}; pub(crate) use self::matches::{MatchedArg, SubCommand};
pub(crate) use self::parser::Identifier; pub(crate) use self::parser::Identifier;
pub(crate) use self::parser::PendingArg; pub(crate) use self::parser::PendingArg;

View file

@ -13,9 +13,9 @@ use crate::error::Result as ClapResult;
use crate::mkeymap::KeyType; use crate::mkeymap::KeyType;
use crate::output::Usage; use crate::output::Usage;
use crate::parser::features::suggestions; use crate::parser::features::suggestions;
use crate::parser::AnyValue;
use crate::parser::{ArgMatcher, SubCommand}; use crate::parser::{ArgMatcher, SubCommand};
use crate::parser::{Validator, ValueSource}; use crate::parser::{Validator, ValueSource};
use crate::util::AnyValue;
use crate::util::Id; use crate::util::Id;
use crate::ArgAction; use crate::ArgAction;
use crate::INTERNAL_ERROR_MSG; use crate::INTERNAL_ERROR_MSG;

View file

@ -1,5 +1,6 @@
#![allow(clippy::single_component_path_imports)] #![allow(clippy::single_component_path_imports)]
mod any_value;
pub(crate) mod flat_map; pub(crate) mod flat_map;
pub(crate) mod flat_set; pub(crate) mod flat_set;
mod graph; mod graph;
@ -8,6 +9,8 @@ mod str_to_bool;
pub use self::id::Id; pub use self::id::Id;
pub(crate) use self::any_value::AnyValue;
pub(crate) use self::any_value::AnyValueId;
pub(crate) use self::flat_map::Entry; pub(crate) use self::flat_map::Entry;
pub(crate) use self::flat_map::FlatMap; pub(crate) use self::flat_map::FlatMap;
pub(crate) use self::flat_set::FlatSet; pub(crate) use self::flat_set::FlatSet;