Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect termios ECHO flag #673

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ crossbeam = { version = "0.8.2", optional = true }
crossterm = { version = "0.27.0", features = ["serde"] }
fd-lock = "3.0.3"
itertools = "0.10.3"
nix = { version = "0.27.1", features = ["term"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super critical but we could make this a unix only dependency for slightly faster downloads on windows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, forgot to do that.

nu-ansi-term = "0.49.0"
rusqlite = { version = "0.29.0", optional = true }
serde = { version = "1.0", features = ["derive"] }
Expand Down
32 changes: 24 additions & 8 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
// Manage optional kitty protocol
kitty_protocol: KittyProtocolGuard,

// Echo typed input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this an absolutely obscure usecase I think expanding this to a small doccomment with pointers what you are trying to achieve and the relation to the termios flag makes sense for future generations.

echo_on: bool,

#[cfg(feature = "external_printer")]
external_printer: Option<ExternalPrinter<String>>,
}
Expand Down Expand Up @@ -218,6 +221,7 @@
cursor_shapes: None,
bracketed_paste: BracketedPasteGuard::default(),
kitty_protocol: KittyProtocolGuard::default(),
echo_on: true,
#[cfg(feature = "external_printer")]
external_printer: None,
}
Expand Down Expand Up @@ -609,6 +613,14 @@
/// Returns a [`std::io::Result`] in which the `Err` type is [`std::io::Result`]
/// and the `Ok` variant wraps a [`Signal`] which handles user inputs.
pub fn read_line(&mut self, prompt: &dyn Prompt) -> Result<Signal> {
// Needs to be done before `terminal::enable_raw_mode()` which modifies the terminal flags
#[cfg(unix)]
if let Ok(attr) = nix::sys::termios::tcgetattr(std::io::stdin()) {
self.echo_on = attr
.local_flags
.contains(nix::sys::termios::LocalFlags::ECHO);
}

Check warning on line 622 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L618-L622

Added lines #L618 - L622 were not covered by tests

terminal::enable_raw_mode()?;
self.bracketed_paste.enter();
self.kitty_protocol.enter();
Expand All @@ -618,6 +630,7 @@
self.bracketed_paste.exit();
self.kitty_protocol.exit();
terminal::disable_raw_mode()?;

result
}

Expand Down Expand Up @@ -1638,14 +1651,17 @@
let cursor_position_in_buffer = self.editor.insertion_point();
let buffer_to_paint = self.editor.get_buffer();

let (before_cursor, after_cursor) = self
.highlighter
.highlight(buffer_to_paint, cursor_position_in_buffer)
.render_around_insertion_point(
cursor_position_in_buffer,
prompt,
self.use_ansi_coloring,
);
let (before_cursor, after_cursor) = if self.echo_on {
self.highlighter
.highlight(buffer_to_paint, cursor_position_in_buffer)
.render_around_insertion_point(
cursor_position_in_buffer,
prompt,
self.use_ansi_coloring,
)

Check warning on line 1661 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1654-L1661

Added lines #L1654 - L1661 were not covered by tests
} else {
(String::new(), String::new())

Check warning on line 1663 in src/engine.rs

View check run for this annotation

Codecov / codecov/patch

src/engine.rs#L1663

Added line #L1663 was not covered by tests
};

let hint: String = if self.hints_active() {
self.hinter.as_mut().map_or_else(String::new, |hinter| {
Expand Down