pub struct ConfigMenu<'a> { /* private fields */ }Expand description
A configurable terminal menu for viewing and editing application settings.
ConfigMenu reads key/value settings from a SQLite table, displays them as
a numbered menu, and allows the selected value to be updated.
It can optionally mask secret values, validate proposed changes, customise ordering, and register additional user-defined menu actions.
Implementations§
Source§impl<'a> ConfigMenu<'a>
impl<'a> ConfigMenu<'a>
Sourcepub fn key_column(self, key_column: impl Into<String>) -> Self
pub fn key_column(self, key_column: impl Into<String>) -> Self
Sourcepub fn value_column(self, value_column: impl Into<String>) -> Self
pub fn value_column(self, value_column: impl Into<String>) -> Self
Sourcepub fn order_by(self, order_by: impl Into<String>) -> Self
pub fn order_by(self, order_by: impl Into<String>) -> Self
Sets the column name to use for ordering settings in the menu. If not specified, settings will be ordered by the key column. The specified column must contain values that can be used to determine the display order of settings.
§Arguments
order_by- The name of the column to use for ordering settings in the menu.
§Returns
The ConfigMenu instance with the updated ordering configuration.
Sourcepub fn secret_keys<I, S>(self, keys: I) -> Self
pub fn secret_keys<I, S>(self, keys: I) -> Self
Sets the keys of settings that should be treated as secrets and have their values masked in the menu. When a setting with a key in this list has a non-empty value, it will be displayed as “********” in the menu to indicate that it is a secret. The actual value of the setting will still be loaded and updated in the database as normal; this only affects how the value is displayed in the menu.
§Arguments
keys- An iterable of keys that should be treated as secrets and have their values masked in the menu.
§Returns
The ConfigMenu instance with the updated secret keys configuration.
Sourcepub fn validator<F>(self, validator: F) -> Self
pub fn validator<F>(self, validator: F) -> Self
Sets a custom validator function that will be called to validate new values for settings before they are updated in the database.
The validator function should take the setting key and the new value as arguments and return Ok(()) if the value is valid or an Err with a message if the value is invalid.
If a validator is set and it returns an error for a new value, the menu will display the error message and prevent the update
from being applied to the database.
This allows you to enforce constraints on setting values, such as requiring a certain format or range of values.
§Arguments
validator- A function that takes a setting key and a new value.
§Returns
The ConfigMenu instance with the updated validator configuration.
Sourcepub fn action<F>(
self,
key: impl Into<String>,
label: impl Into<String>,
callback: F,
) -> Self
pub fn action<F>( self, key: impl Into<String>, label: impl Into<String>, callback: F, ) -> Self
Adds a custom action to the menu that can be triggered by entering a specific key.
The action will be displayed in the menu prompt with the specified label, and when the user enters the corresponding key,
the provided callback function will be executed.
The callback function should take a reference to the database connection as an argument and return Ok(()) if the action was
successful or an Err with a message if the action failed.
This allows you to add additional functionality to the menu, such as resetting settings to default values, syncing settings with
an external service, or performing any other custom operation that can be implemented in Rust.
The action key must not be empty, must not consist solely of digits, and must not be “q” or “quit” (case-sensitive), as these
are reserved for exiting the menu.
If an invalid or reserved action key is provided, the menu will return an error when you try to run it.
§Arguments
key- The key that the user must enter to trigger the action.label- A description of the action that will be displayed in the menu prompt.callback- A function that will be called when the action is triggered. It should take a reference to the database connection and return aResultindicating success or failure of the action.
§Returns
The ConfigMenu instance with the new action added to its configuration.