Make hints configurable.

- See #336.
This commit is contained in:
Justus Winter 2024-12-02 14:58:12 +01:00
parent 092ba48e5a
commit dcc3db167d
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
2 changed files with 33 additions and 2 deletions

View File

@ -45,6 +45,9 @@ pub struct Config {
/// Whether to be more quiet.
quiet: bool,
/// Whether to show hints.
hints: Option<bool>,
encrypt_for_self: BTreeSet<Fingerprint>,
policy_path: Option<PathBuf>,
policy_inline: Option<Vec<u8>>,
@ -57,6 +60,7 @@ impl Default for Config {
Config {
verbose: false,
quiet: false,
hints: None,
encrypt_for_self: Default::default(),
policy_path: None,
policy_inline: None,
@ -123,6 +127,11 @@ impl Config {
self.quiet
}
/// Returns whether to show hints.
pub fn hints(&self) -> bool {
self.hints.unwrap_or(! self.quiet())
}
/// Returns the certificates that should be added to the list of
/// recipients if `encrypt --for-self` is given.
pub fn encrypt_for_self(&self) -> &BTreeSet<Fingerprint> {
@ -214,6 +223,7 @@ impl ConfigFile {
[ui]
#verbosity = \"default\" # or \"verbose\" or \"quiet\"
#hints = true
[encrypt]
#for-self = [\"fingerprint of your key\"]
@ -431,7 +441,11 @@ impl ConfigFile {
write!(&mut raw, "{}", default_policy_inline)?;
// Now, parse the resulting configuration.
let doc: DocumentMut = std::str::from_utf8(&raw)?.parse()?;
let mut doc: DocumentMut = std::str::from_utf8(&raw)?.parse()?;
// Tweak a few settings.
doc.get_mut("ui".into()).unwrap()
.set(&"hints".into(), sq.config.hints().into())?;
// Double check that it is well-formed.
apply_schema(&mut None, &mut None, None, doc.iter(), TOP_LEVEL_SCHEMA)?;
@ -616,6 +630,7 @@ const TOP_LEVEL_SCHEMA: Schema = &[
/// Schema for the `ui` section.
const UI_SCHEMA: Schema = &[
("hints", apply_ui_hints),
("verbosity", apply_ui_verbosity),
];
@ -630,6 +645,22 @@ fn apply_ui(config: &mut Option<&mut Config>, cli: &mut Option<&mut Augmentation
Ok(())
}
/// Validates the `ui.hints` value.
fn apply_ui_hints(config: &mut Option<&mut Config>,
_cli: &mut Option<&mut Augmentations>,
path: &str, item: &Item)
-> Result<()>
{
let s = item.as_bool()
.ok_or_else(|| Error::bad_item_type(path, item, "bool"))?;
if let Some(config) = config {
config.hints = Some(s);
}
Ok(())
}
/// Validates the `ui.verbosity` value.
fn apply_ui_verbosity(config: &mut Option<&mut Config>,
cli: &mut Option<&mut Augmentations>,

View File

@ -1736,7 +1736,7 @@ impl<'store: 'rstore, 'rstore> Sq<'store, 'rstore> {
/// Prints a hint for the user.
pub fn hint(&self, msg: fmt::Arguments) -> Hint {
Hint::new(self.quiet())
Hint::new(! self.config.hints())
.hint(msg)
}