Skip to content
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

Adding alert as new message type #288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/alert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use dialoguer::{theme::ColorfulTheme, Alert};

fn main() {
let _ = Alert::with_theme(&ColorfulTheme::default())
.with_prompt("Something went wrong! Press enter to continue.")
.interact();

let _ = Alert::with_theme(&ColorfulTheme::default())
.with_alert_text("This is an alert, press enter to continue.")
.interact();

let _ = Alert::with_theme(&ColorfulTheme::default())
.with_alert_text("Strange things happened: <spooky error message>.")
.with_prompt("Press enter to continue.")
.interact();
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ pub use prompts::fuzzy_select::FuzzySelect;
#[cfg(feature = "password")]
pub use prompts::password::Password;
pub use prompts::{
confirm::Confirm, input::Input, multi_select::MultiSelect, select::Select, sort::Sort,
alert::Alert, confirm::Confirm, input::Input, multi_select::MultiSelect, select::Select,
sort::Sort,
};

#[cfg(feature = "completion")]
Expand Down
148 changes: 148 additions & 0 deletions src/prompts/alert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use std::io;

use console::{Key, Term};

use crate::{
theme::{render::TermThemeRenderer, SimpleTheme, Theme},
Result,
};

/// Renders an alert prompt.
///
/// ## Example
///
/// ```rust,no_run
/// use dialoguer::{theme::ColorfulTheme, Alert};
///
/// fn main() {
/// let _ = Alert::with_theme(&ColorfulTheme::default())
/// .with_prompt("Something went wrong! Press enter to continue.")
/// .interact();
///
/// let _ = Alert::with_theme(&ColorfulTheme::default())
/// .with_alert_text("This is an alert, press enter to continue.")
/// .interact();
///
/// let _ = Alert::with_theme(&ColorfulTheme::default())
/// .with_alert_text("Strange things happened: <spooky error message>.")
/// .with_prompt("Press enter to continue.")
/// .interact();
/// }
/// ```
#[derive(Clone)]
pub struct Alert<'a> {
alert_text: String,
prompt: String,
theme: &'a dyn Theme,
}

impl Default for Alert<'static> {
fn default() -> Self {
Self::new()
}
}

impl Alert<'static> {
/// Creates a alert prompt with default theme.
pub fn new() -> Self {
Self::with_theme(&SimpleTheme)
}
}

impl Alert<'_> {
/// Sets the alert content message.
pub fn with_alert_text<S: Into<String>>(mut self, alert_text: S) -> Self {
self.alert_text = alert_text.into();
self
}

/// Sets the alert prompt.
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}

/// Enables user interaction.
///
/// The dialog is rendered on stderr.
#[inline]
pub fn interact(self) -> Result<Option<()>> {
self.interact_on(&Term::stderr())
}

/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
#[inline]
pub fn interact_on(self, term: &Term) -> Result<Option<()>> {
Ok(Some(self._interact_on(term)?.ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case")
})?))
}

fn _interact_on(self, term: &Term) -> Result<Option<()>> {
if !term.is_term() {
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
}

let mut render = TermThemeRenderer::new(term, self.theme);

render.alert_prompt(&self.alert_text, &self.prompt)?;

term.hide_cursor()?;
term.flush()?;

// Default behavior: wait for user to hit the Enter key.
loop {
let input = term.read_key()?;
match input {
Key::Enter => (),
_ => {
continue;
}
};

break;
}

term.write_line("")?;
term.show_cursor()?;
term.flush()?;

Ok(Some(()))
}
}

impl<'a> Alert<'a> {
/// Creates an alert prompt with a specific theme.
///
/// ## Example
///
/// ```rust,no_run
/// use dialoguer::{theme::ColorfulTheme, Alert};
///
/// fn main() {
/// let alert = Alert::with_theme(&ColorfulTheme::default())
/// .interact();
/// }
/// ```
pub fn with_theme(theme: &'a dyn Theme) -> Self {
Self {
alert_text: "".into(),
prompt: "".into(),
theme,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_clone() {
let alert = Alert::new()
.with_alert_text("FYI: ground gets wet if it rains.")
.with_prompt("Press enter continue");

let _ = alert.clone();
}
}
1 change: 1 addition & 0 deletions src/prompts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::needless_doctest_main)]

pub mod alert;
pub mod confirm;
pub mod input;
pub mod multi_select;
Expand Down
19 changes: 19 additions & 0 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ pub trait Theme {
write!(f, "error: {}", err)
}

/// Formats an alert prompt.
fn format_alert_prompt(
&self,
f: &mut dyn fmt::Write,
alert_text: &str,
prompt: &str,
) -> fmt::Result {
if !alert_text.is_empty() {
write!(f, "⚠ {}", &alert_text)?;
}
if !prompt.is_empty() {
if !alert_text.is_empty() {
writeln!(f, "")?;
}
write!(f, "{}", &prompt)?;
}
Ok(())
}

/// Formats a confirm prompt.
fn format_confirm_prompt(
&self,
Expand Down
6 changes: 6 additions & 0 deletions src/theme/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ impl<'a> TermThemeRenderer<'a> {
self.write_formatted_line(|this, buf| this.theme.format_error(buf, err))
}

pub fn alert_prompt(&mut self, alert_text: &str, prompt: &str) -> Result<usize> {
self.write_formatted_str(|this, buf| {
this.theme.format_alert_prompt(buf, alert_text, prompt)
})
}

pub fn confirm_prompt(&mut self, prompt: &str, default: Option<bool>) -> Result<usize> {
self.write_formatted_str(|this, buf| this.theme.format_confirm_prompt(buf, prompt, default))
}
Expand Down