-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move prompt utils into a
utils
module
- Loading branch information
Showing
5 changed files
with
72 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod net; | ||
pub mod prompts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
| _ => {}, | ||
} | ||
} |