Skip to content

Commit

Permalink
refactor: move prompt utils into a utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Nov 23, 2024
1 parent 9ce888e commit 88be884
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 62 deletions.
63 changes: 1 addition & 62 deletions src/actions/prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,7 @@ use inquire::{Confirm, CustomType, Editor, Select, Text};
use crate::actions::State;
use crate::config::prompts::*;
use crate::config::{Number, Value};

/// Helper module holding useful functions.
mod helpers {
use std::fmt::Display;
use std::process;

use crossterm::style::Stylize;
use inquire::formatter::StringFormatter;
use inquire::ui::{Color, RenderConfig, StyleSheet, Styled};
use inquire::InquireError;

/// Returns configured theme.
pub fn theme<'r>() -> RenderConfig<'r> {
let default = RenderConfig::default();
let stylesheet = StyleSheet::default();

let prompt_prefix = Styled::new("?").with_fg(Color::LightYellow);
let answered_prefix = Styled::new("✓").with_fg(Color::LightGreen);

default
.with_prompt_prefix(prompt_prefix)
.with_answered_prompt_prefix(answered_prefix)
.with_default_value(stylesheet.with_fg(Color::DarkGrey))
}

/// Returns a formatter that shows `<empty>` if the input is empty.
pub fn empty_formatter<'s>() -> StringFormatter<'s> {
&|input| {
if input.is_empty() {
"<empty>".dark_grey().to_string()
} else {
input.to_string()
}
}
}

/// Helper method that generates `(name, hint, help)`.
pub fn messages<S>(name: S, hint: S) -> (String, String, String)
where
S: Into<String> + AsRef<str> + Display,
{
let name = name.into();
let hint = format!("{}:", &hint);
let help = format!("The answer will be mapped to: {}", &name);

(name, hint, help)
}

/// Handle interruption/cancelation events.
pub fn interrupt(err: InquireError) {
match err {
| InquireError::OperationCanceled => {
process::exit(0);
},
| InquireError::OperationInterrupted => {
println!("{}", "<interrupted>".red());
process::exit(0);
},
| _ => {},
}
}
}
use crate::utils::prompts as helpers;

impl ConfirmPrompt {
/// Execute the prompt and populate the state.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pub(crate) mod report;
pub(crate) mod repository;
pub(crate) mod spinner;
pub(crate) mod unpacker;
pub(crate) mod utils;
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod net;
pub mod prompts;
10 changes: 10 additions & 0 deletions src/utils/net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
use std::time::Duration;

#[allow(dead_code)]
pub fn is_online() -> bool {
let ip = Ipv4Addr::new(1, 1, 1, 1);
let address = SocketAddr::V4(SocketAddrV4::new(ip, 80));

TcpStream::connect_timeout(&address, Duration::from_secs(5)).map_or(false, |_| true)
}
58 changes: 58 additions & 0 deletions src/utils/prompts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::fmt::Display;
use std::process;

use crossterm::style::Stylize;
use inquire::formatter::StringFormatter;
use inquire::ui::{Color, RenderConfig, StyleSheet, Styled};
use inquire::InquireError;

/// Returns configured theme.
pub fn theme<'r>() -> RenderConfig<'r> {
let default = RenderConfig::default();
let stylesheet = StyleSheet::default();

let prompt_prefix = Styled::new("?").with_fg(Color::LightYellow);
let answered_prefix = Styled::new("✓").with_fg(Color::LightGreen);

default
.with_prompt_prefix(prompt_prefix)
.with_answered_prompt_prefix(answered_prefix)
.with_default_value(stylesheet.with_fg(Color::DarkGrey))
}

/// Returns a formatter that shows `<empty>` if the input is empty.
pub fn empty_formatter<'s>() -> StringFormatter<'s> {
&|input| {
if input.is_empty() {
"<empty>".dark_grey().to_string()
} else {
input.to_string()
}
}
}

/// Helper method that generates `(name, hint, help)`.
pub fn messages<S>(name: S, hint: S) -> (String, String, String)
where
S: Into<String> + AsRef<str> + Display,
{
let name = name.into();
let hint = format!("{}:", &hint);
let help = format!("The answer will be mapped to: {}", &name);

(name, hint, help)
}

/// Handle interruption/cancelation events.
pub fn interrupt(err: InquireError) {
match err {
| InquireError::OperationCanceled => {
process::exit(0);
},
| InquireError::OperationInterrupted => {
println!("{}", "<interrupted>".red());
process::exit(0);
},
| _ => {},
}
}

0 comments on commit 88be884

Please sign in to comment.