Skip to content

Commit cde458b

Browse files
authored
Make inputs fail if not connected to a terminal (#261)
1 parent dcb1974 commit cde458b

File tree

6 files changed

+31
-1
lines changed

6 files changed

+31
-1
lines changed

src/prompts/confirm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ impl Confirm<'_> {
150150
}
151151

152152
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<bool>> {
153+
if !term.is_term() {
154+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
155+
}
156+
153157
let mut render = TermThemeRenderer::new(term, self.theme);
154158

155159
let default_if_show = if self.show_default {

src/prompts/input.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cmp::Ordering, fmt::Debug, iter, str::FromStr};
1+
use std::{cmp::Ordering, fmt::Debug, io, iter, str::FromStr};
22

33
use console::{Key, Term};
44

@@ -288,6 +288,10 @@ where
288288

289289
/// Like [`interact_text`](Self::interact_text) but allows a specific terminal to be set.
290290
pub fn interact_text_on(mut self, term: &Term) -> Result<T> {
291+
if !term.is_term() {
292+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
293+
}
294+
291295
let mut render = TermThemeRenderer::new(term, self.theme);
292296

293297
loop {
@@ -639,6 +643,10 @@ where
639643

640644
/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
641645
pub fn interact_on(mut self, term: &Term) -> Result<T> {
646+
if !term.is_term() {
647+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
648+
}
649+
642650
let mut render = TermThemeRenderer::new(term, self.theme);
643651

644652
loop {

src/prompts/multi_select.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ impl MultiSelect<'_> {
196196
}
197197

198198
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<Vec<usize>>> {
199+
if !term.is_term() {
200+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
201+
}
202+
199203
if self.items.is_empty() {
200204
return Err(io::Error::new(
201205
io::ErrorKind::Other,

src/prompts/password.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::io;
2+
13
use console::Term;
24
use zeroize::Zeroizing;
35

@@ -135,6 +137,10 @@ impl<'a> Password<'a> {
135137

136138
/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
137139
pub fn interact_on(self, term: &Term) -> Result<String> {
140+
if !term.is_term() {
141+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
142+
}
143+
138144
let mut render = TermThemeRenderer::new(term, self.theme);
139145
render.set_prompts_reset_height(false);
140146

src/prompts/select.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ impl Select<'_> {
185185

186186
/// Like `interact` but allows a specific terminal to be set.
187187
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
188+
if !term.is_term() {
189+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
190+
}
191+
188192
if self.items.is_empty() {
189193
return Err(io::Error::new(
190194
io::ErrorKind::Other,

src/prompts/sort.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ impl Sort<'_> {
168168
}
169169

170170
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<Vec<usize>>> {
171+
if !term.is_term() {
172+
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
173+
}
174+
171175
if self.items.is_empty() {
172176
return Err(io::Error::new(
173177
io::ErrorKind::Other,

0 commit comments

Comments
 (0)