Skip to content

Commit

Permalink
Make inputs fail if not connected to a terminal (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored May 21, 2023
1 parent dcb1974 commit cde458b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/prompts/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ impl Confirm<'_> {
}

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

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

let default_if_show = if self.show_default {
Expand Down
10 changes: 9 additions & 1 deletion src/prompts/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cmp::Ordering, fmt::Debug, iter, str::FromStr};
use std::{cmp::Ordering, fmt::Debug, io, iter, str::FromStr};

use console::{Key, Term};

Expand Down Expand Up @@ -288,6 +288,10 @@ where

/// Like [`interact_text`](Self::interact_text) but allows a specific terminal to be set.
pub fn interact_text_on(mut self, term: &Term) -> Result<T> {
if !term.is_term() {
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
}

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

loop {
Expand Down Expand Up @@ -639,6 +643,10 @@ where

/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
pub fn interact_on(mut self, term: &Term) -> Result<T> {
if !term.is_term() {
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
}

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

loop {
Expand Down
4 changes: 4 additions & 0 deletions src/prompts/multi_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ impl MultiSelect<'_> {
}

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

if self.items.is_empty() {
return Err(io::Error::new(
io::ErrorKind::Other,
Expand Down
6 changes: 6 additions & 0 deletions src/prompts/password.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io;

use console::Term;
use zeroize::Zeroizing;

Expand Down Expand Up @@ -135,6 +137,10 @@ impl<'a> Password<'a> {

/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
pub fn interact_on(self, term: &Term) -> Result<String> {
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.set_prompts_reset_height(false);

Expand Down
4 changes: 4 additions & 0 deletions src/prompts/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ impl Select<'_> {

/// Like `interact` but allows a specific terminal to be set.
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
if !term.is_term() {
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
}

if self.items.is_empty() {
return Err(io::Error::new(
io::ErrorKind::Other,
Expand Down
4 changes: 4 additions & 0 deletions src/prompts/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ impl Sort<'_> {
}

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

if self.items.is_empty() {
return Err(io::Error::new(
io::ErrorKind::Other,
Expand Down

0 comments on commit cde458b

Please sign in to comment.