-
Notifications
You must be signed in to change notification settings - Fork 381
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
cli, config: more granular pagination settings #2927
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -12,7 +12,9 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::HashMap; | ||
use std::io::{IsTerminal as _, Stderr, StderrLock, Stdout, StdoutLock, Write}; | ||
use std::ops::ControlFlow; | ||
use std::process::{Child, ChildStdin, Stdio}; | ||
use std::str::FromStr; | ||
use std::{env, fmt, io, mem}; | ||
|
@@ -101,7 +103,7 @@ impl Write for UiStderr<'_> { | |
pub struct Ui { | ||
color: bool, | ||
pager_cmd: CommandNameAndArgs, | ||
paginate: PaginationChoice, | ||
paginate: Pagination, | ||
progress_indicator: bool, | ||
formatter_factory: FormatterFactory, | ||
output: UiOutput, | ||
|
@@ -165,11 +167,88 @@ pub enum PaginationChoice { | |
Never, | ||
#[default] | ||
Auto, | ||
Always, | ||
} | ||
|
||
impl FromStr for PaginationChoice { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"always" => Ok(PaginationChoice::Always), | ||
"never" => Ok(PaginationChoice::Never), | ||
"auto" => Ok(PaginationChoice::Auto), | ||
_ => Err("must be one of always, never, or auto"), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for PaginationChoice { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let s = match self { | ||
PaginationChoice::Always => "always", | ||
PaginationChoice::Never => "never", | ||
PaginationChoice::Auto => "auto", | ||
}; | ||
write!(f, "{s}") | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default, Eq, PartialEq)] | ||
pub struct Pagination { | ||
default: PaginationChoice, | ||
|
||
commands: HashMap<String, Pagination>, | ||
} | ||
|
||
fn pagination_setting(config: &config::Config) -> Result<PaginationChoice, CommandError> { | ||
impl<'de> serde::de::Deserialize<'de> for Pagination { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
struct PaginationVisitor; | ||
|
||
impl<'de> serde::de::Visitor<'de> for PaginationVisitor { | ||
type Value = Pagination; | ||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
formatter, | ||
r#"always, never, auto, or a mapping of command names to pagination"# | ||
) | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(Pagination { | ||
default: PaginationChoice::from_str(v).map_err(E::custom)?, | ||
..Default::default() | ||
}) | ||
} | ||
|
||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> | ||
where | ||
A: serde::de::MapAccess<'de>, | ||
{ | ||
let mut cfg = Pagination::default(); | ||
|
||
while let Some(k) = map.next_key::<String>()? { | ||
if k == "default" { | ||
cfg.default = map.next_value::<PaginationChoice>()?; | ||
} else { | ||
cfg.commands.insert(k, map.next_value::<Pagination>()?); | ||
} | ||
} | ||
Ok(cfg) | ||
} | ||
} | ||
deserializer.deserialize_any(PaginationVisitor) | ||
} | ||
} | ||
fn pagination_setting(config: &config::Config) -> Result<Pagination, CommandError> { | ||
config | ||
.get::<PaginationChoice>("ui.paginate") | ||
.get::<Pagination>("ui.paginate") | ||
.map_err(|err| CommandError::ConfigError(format!("Invalid `ui.paginate`: {err}"))) | ||
} | ||
|
||
|
@@ -209,10 +288,29 @@ impl Ui { | |
|
||
/// Switches the output to use the pager, if allowed. | ||
#[instrument(skip_all)] | ||
pub fn request_pager(&mut self) { | ||
match self.paginate { | ||
pub fn request_pager<'a>(&mut self, subcommands: impl IntoIterator<Item = &'a str>) { | ||
let paginate = subcommands.into_iter().try_fold( | ||
(self.paginate.default, &self.paginate), | ||
|(out, current), v| match current.commands.get(v) { | ||
None => ControlFlow::Break((out, current)), | ||
Some( | ||
p @ Pagination { | ||
default: PaginationChoice::Auto, | ||
.. | ||
}, | ||
) => ControlFlow::Continue((out, p)), | ||
Some(p) => ControlFlow::Continue((p.default, p)), | ||
}, | ||
); | ||
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. While this is interesting, I think it's better to use a flat map of I'm not sure whether the config table should be pager-specific or more general [commands."branch list"]
paginate = ..
pager = .. |
||
|
||
let paginate = match paginate { | ||
ControlFlow::Break((out, _)) => out, | ||
ControlFlow::Continue((out, _)) => out, | ||
}; | ||
|
||
match paginate { | ||
PaginationChoice::Never => return, | ||
PaginationChoice::Auto => {} | ||
PaginationChoice::Always | PaginationChoice::Auto => {} | ||
} | ||
|
||
match self.output { | ||
|
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.
nit: perhaps,
iter::successors(..).map(..)
can be used.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.
Oh, neat - I think I knew about this at one time, but forgot about it.
lol, and the source even has a comment alluding to this exact implementation of it 😂
I'll switch to that and likely forget about it and get to rediscover it all over again in the future 👍