Skip to content

Commit

Permalink
Allow lifetime customization of RenderConfig
Browse files Browse the repository at this point in the history
Fixes #95.

This allows render configs to have a custom lifetime, giving more
freedom for further customization on the values, specially those
defined during runtime.
  • Loading branch information
mikaelmello committed Mar 11, 2023
1 parent 5413efc commit b210851
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion examples/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> InquireResult<()> {
Ok(())
}

fn description_render_config() -> RenderConfig {
fn description_render_config() -> RenderConfig<'static> {
RenderConfig::default()
.with_canceled_prompt_indicator(Styled::new("<skipped>").with_fg(Color::DarkYellow))
}
2 changes: 1 addition & 1 deletion examples/render_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn get_existing_payees() -> &'static [&'static str] {
]
}

fn get_render_config() -> RenderConfig {
fn get_render_config() -> RenderConfig<'static> {
let mut render_config = RenderConfig::default();
render_config.prompt_prefix = Styled::new("$").with_fg(Color::LightRed);
render_config.highlighted_option_prefix = Styled::new("➠").with_fg(Color::LightYellow);
Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use lazy_static::lazy_static;
use crate::ui::RenderConfig;

lazy_static! {
static ref GLOBAL_RENDER_CONFIGURATION: Mutex<RenderConfig> =
static ref GLOBAL_RENDER_CONFIGURATION: Mutex<RenderConfig<'static>> =
Mutex::new(RenderConfig::default());
}

pub fn get_configuration() -> RenderConfig {
pub fn get_configuration() -> RenderConfig<'static> {
*GLOBAL_RENDER_CONFIGURATION.lock().unwrap()
}

/// Acquires a write lock to the global RenderConfig object
/// and updates the inner value with the provided argument.
pub fn set_global_render_config(config: RenderConfig) {
pub fn set_global_render_config(config: RenderConfig<'static>) {
let mut guard = GLOBAL_RENDER_CONFIGURATION.lock().unwrap();
*guard = config;
}
Expand Down
6 changes: 3 additions & 3 deletions src/prompts/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct Confirm<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a> Confirm<'a> {
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<'a> Confirm<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> Confirm<'a> {

pub(crate) fn prompt_with_backend<T: Terminal>(
self,
backend: &mut Backend<T>,
backend: &mut Backend<'a, T>,
) -> InquireResult<bool> {
CustomType::from(self).prompt_with_backend(backend)
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct CustomType<'a, T> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a, T> CustomType<'a, T>
Expand Down Expand Up @@ -228,7 +228,7 @@ where
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down
6 changes: 3 additions & 3 deletions src/prompts/dateselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct DateSelect<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a> DateSelect<'a> {
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<'a> DateSelect<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'a> DateSelect<'a> {

pub(crate) fn prompt_with_backend<T: Terminal>(
self,
backend: &mut Backend<T>,
backend: &mut Backend<'a, T>,
) -> InquireResult<NaiveDate> {
DateSelectPrompt::new(self)?.prompt(backend)
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct Editor<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a> Editor<'a> {
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<'a> Editor<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/multiselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub struct MultiSelect<'a, T> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a, T> MultiSelect<'a, T>
Expand Down Expand Up @@ -262,7 +262,7 @@ where
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct Password<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a> Password<'a> {
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'a> Password<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub struct Select<'a, T> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a, T> Select<'a, T>
Expand Down Expand Up @@ -218,7 +218,7 @@ where
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub struct Text<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub render_config: RenderConfig,
pub render_config: RenderConfig<'a>,
}

impl<'a> Text<'a> {
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'a> Text<'a> {
/// When overriding the config in a prompt, NO_COLOR is no longer considered and your
/// config is treated as the only source of truth. If you want to customize colors
/// and still suport NO_COLOR, you will have to do this on your end.
pub fn with_render_config(mut self, render_config: RenderConfig) -> Self {
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
Expand Down
26 changes: 13 additions & 13 deletions src/ui/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct Position {
pub col: u16,
}

pub struct Backend<T>
pub struct Backend<'a, T>
where
T: Terminal,
{
Expand All @@ -85,14 +85,14 @@ where
show_cursor: bool,
terminal: T,
terminal_size: TerminalSize,
render_config: RenderConfig,
render_config: RenderConfig<'a>,
}

impl<T> Backend<T>
impl<'a, T> Backend<'a, T>
where
T: Terminal,
{
pub fn new(terminal: T, render_config: RenderConfig) -> Result<Self> {
pub fn new(terminal: T, render_config: RenderConfig<'a>) -> Result<Self> {
let terminal_size = terminal.get_size().unwrap_or(TerminalSize {
width: 1000,
height: 1000,
Expand Down Expand Up @@ -349,7 +349,7 @@ where
}
}

impl<T> CommonBackend for Backend<T>
impl<'a, T> CommonBackend for Backend<'a, T>
where
T: Terminal,
{
Expand Down Expand Up @@ -446,7 +446,7 @@ where
}
}

impl<T> TextBackend for Backend<T>
impl<'a, T> TextBackend for Backend<'a, T>
where
T: Terminal,
{
Expand Down Expand Up @@ -474,7 +474,7 @@ where
}

#[cfg(feature = "editor")]
impl<T> EditorBackend for Backend<T>
impl<'a, T> EditorBackend for Backend<'a, T>
where
T: Terminal,
{
Expand All @@ -493,7 +493,7 @@ where
}
}

impl<T> SelectBackend for Backend<T>
impl<'a, T> SelectBackend for Backend<'a, T>
where
T: Terminal,
{
Expand Down Expand Up @@ -521,7 +521,7 @@ where
}
}

impl<T> MultiSelectBackend for Backend<T>
impl<'a, T> MultiSelectBackend for Backend<'a, T>
where
T: Terminal,
{
Expand Down Expand Up @@ -595,7 +595,7 @@ pub mod date {
) -> Result<()>;
}

impl<T> DateSelectBackend for Backend<T>
impl<'a, T> DateSelectBackend for Backend<'a, T>
where
T: Terminal,
{
Expand Down Expand Up @@ -719,7 +719,7 @@ pub mod date {
}
}

impl<T> CustomTypeBackend for Backend<T>
impl<'a, T> CustomTypeBackend for Backend<'a, T>
where
T: Terminal,
{
Expand All @@ -733,7 +733,7 @@ where
}
}

impl<T> PasswordBackend for Backend<T>
impl<'a, T> PasswordBackend for Backend<'a, T>
where
T: Terminal,
{
Expand All @@ -758,7 +758,7 @@ where
}
}

impl<T> Drop for Backend<T>
impl<'a, T> Drop for Backend<'a, T>
where
T: Terminal,
{
Expand Down
Loading

0 comments on commit b210851

Please sign in to comment.