use std::io::{self, IsTerminal, Read, Write};
use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
use crate::tui::InlinePromptInput;
pub(super) async fn read(label: &str) -> anyhow::Result<String> {
if !crate::stdio::stdin_is_terminal() || !io::stderr().is_terminal() {
anyhow::bail!("entering {label} requires an interactive terminal");
}
let secret = read_hidden(label).await;
let _ = writeln!(io::stderr());
secret
}
pub(super) async fn read_or_stdin(label: &str) -> anyhow::Result<String> {
if crate::stdio::stdin_is_terminal() {
return read(label).await;
}
let text = tokio::task::spawn_blocking(|| {
let mut text = String::new();
io::stdin().read_to_string(&mut text).map(|_| text)
})
.await??;
let text = text.trim();
if text.is_empty() {
anyhow::bail!("{label} cannot be empty");
}
if text.chars().any(char::is_control) {
anyhow::bail!("{label} must be a single line");
}
Ok(text.to_owned())
}
async fn read_hidden(label: &str) -> anyhow::Result<String> {
let mut input = InlinePromptInput::acquire()?;
write!(
io::stderr(),
"enter {label} (input hidden; Enter save, Esc cancel): "
)?;
io::stderr().flush()?;
let mut secret = String::new();
loop {
match input.next().await? {
Event::Key(key) if key.kind != KeyEventKind::Release => match key.code {
KeyCode::Esc => anyhow::bail!("cancelled"),
KeyCode::Char('c' | 'C' | 'd' | 'D')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
anyhow::bail!("cancelled");
}
KeyCode::Enter => {
let secret = secret.trim();
if secret.is_empty() {
anyhow::bail!("{label} cannot be empty");
}
return Ok(secret.to_owned());
}
KeyCode::Backspace => {
secret.pop();
}
KeyCode::Char(ch)
if !ch.is_control()
&& !key
.modifiers
.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
{
secret.push(ch);
}
_ => {}
},
Event::Paste(text) => {
let text = text.trim();
if text.chars().any(char::is_control) {
anyhow::bail!("{label} must be a single line");
}
secret.push_str(text);
}
_ => {}
}
}
}