Skip to content

Commit

Permalink
Use tty instead of stdin on termion (#199)
Browse files Browse the repository at this point in the history
* Use tty instead of stdin on termion

* Rename Tty enum variant to TTY

* Update CHANGELOG

* Allow upper-case acronym
  • Loading branch information
mikaelmello authored Dec 27, 2023
1 parent 70bf4ca commit 79f02db
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
- Expand workflow clippy task to lint all-features in workspace.
- Add docs badge to readme.
- **Breaking** The Select and Multiselect Filter now scores input and is now expected to return an `Option<i64>`, making it possible to order/rank the list of options. [#176](https://github.com/mikaelmello/inquire/pull/176)
`None`: Will not be displayed in the list of options.
`Some(score)`: score determines the order of options, higher score, higher on the list of options.
`None`: Will not be displayed in the list of options.
`Some(score)`: score determines the order of options, higher score, higher on the list of options.
- Implement fuzzy search as default on Select and MultiSelect prompts. [#176](https://github.com/mikaelmello/inquire/pull/176)
- Add new option on Select/MultiSelect prompts allowing to reset selection to the first item on filter-input changes. [#176](https://github.com/mikaelmello/inquire/pull/176)
- Emacs-like keybindings added where applicable:
- Ctrl-p/Ctrl-n for up/down
- Ctrl-b/Ctrl-f for left/right
- Ctrl-j/Ctrl-g for enter/cancel
- Ctrl-p/Ctrl-n for up/down
- Ctrl-b/Ctrl-f for left/right
- Ctrl-j/Ctrl-g for enter/cancel
- Added 'with_starting_filter_input' to both Select and MultiSelect, which allows for setting an initial value to the filter section of the prompt.

### Fixes

- Fixed typos in the code's comments.
- Fixed issue where inquire, using termion, would crash when receiving piped inputs.

### Dependency changes (some breaking)

Expand Down
5 changes: 4 additions & 1 deletion inquire/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ impl From<CustomUserError> for InquireError {

impl From<io::Error> for InquireError {
fn from(err: io::Error) -> Self {
InquireError::IO(err)
match err.raw_os_error() {
Some(25 | 6) => InquireError::NotTTY,
_ => InquireError::IO(err),
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions inquire/src/terminal/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crossterm::{
event::{self, KeyCode, KeyEvent, KeyModifiers},
queue,
style::{Attribute, Color, Print, SetAttribute, SetBackgroundColor, SetForegroundColor},
terminal::{self, enable_raw_mode, ClearType},
terminal::{self, ClearType},
Command,
};

use crate::{
error::{InquireError, InquireResult},
error::InquireResult,
ui::{Attributes, Key, Styled},
};

Expand All @@ -34,10 +34,7 @@ pub struct CrosstermTerminal<'a> {

impl<'a> CrosstermTerminal<'a> {
pub fn new() -> InquireResult<Self> {
enable_raw_mode().map_err(|e| match e.raw_os_error() {
Some(25 | 6) => InquireError::NotTTY,
_ => InquireError::from(e),
})?;
crossterm::terminal::enable_raw_mode()?;

Ok(Self {
io: IO::Std { w: stderr() },
Expand Down
32 changes: 13 additions & 19 deletions inquire/src/terminal/termion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use core::fmt;
use std::io::{stderr, stdin, Result, Stderr, Stdin, Write};
use std::{
fs::File,
io::{Result, Write},
};

use termion::{
color::{self, Color},
Expand All @@ -11,18 +14,15 @@ use termion::{
};

use crate::{
error::{InquireError, InquireResult},
error::InquireResult,
ui::{Attributes, Styled},
};

use super::{Terminal, INITIAL_IN_MEMORY_CAPACITY};

#[allow(clippy::upper_case_acronyms)]
enum IO<'a> {
#[allow(unused)]
Std {
r: Keys<Stdin>,
w: RawTerminal<Stderr>,
},
TTY(RawTerminal<File>, Keys<File>),
#[allow(unused)]
Custom {
r: &'a mut dyn Iterator<Item = &'a Key>,
Expand All @@ -38,18 +38,12 @@ pub struct TermionTerminal<'a> {
impl<'a> TermionTerminal<'a> {
#[allow(unused)]
pub fn new() -> InquireResult<Self> {
let raw_mode = stderr()
.into_raw_mode()
.map_err(|e| match e.raw_os_error() {
Some(25 | 6) => InquireError::NotTTY,
_ => e.into(),
});
let tty = termion::get_tty()?;
let raw_terminal = tty.into_raw_mode()?;
let keys = raw_terminal.try_clone()?.keys();

Ok(Self {
io: IO::Std {
r: stdin().keys(),
w: raw_mode?,
},
io: IO::TTY(raw_terminal, keys),
in_memory_content: String::with_capacity(INITIAL_IN_MEMORY_CAPACITY),
})
}
Expand All @@ -73,7 +67,7 @@ impl<'a> TermionTerminal<'a> {

fn get_writer(&mut self) -> &mut dyn Write {
match &mut self.io {
IO::Std { r: _, w } => w,
IO::TTY(w, _) => w,
IO::Custom { r: _, w } => w,
}
}
Expand Down Expand Up @@ -126,7 +120,7 @@ impl<'a> Terminal for TermionTerminal<'a> {
fn read_key(&mut self) -> Result<crate::ui::Key> {
loop {
match &mut self.io {
IO::Std { r, w: _ } => {
IO::TTY(_, r) => {
if let Some(key) = r.next() {
return key.map(|k| k.into());
}
Expand Down

0 comments on commit 79f02db

Please sign in to comment.