Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ readme = "README.md"
[dependencies]


[features]
default = ["echo"]
echo = []


[dev-dependencies]
pancurses = "0.16"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ It contains multiple paragraphs and should be preceeded by the parameter list.

* None

### v0.3.3

* Add the possibility to disable local echo (via echo feature, enabled by default)

### v0.3.2

* Shortened help output.
Expand Down
38 changes: 25 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,21 @@ where
/// Add a byte to the menu runner's buffer. If this byte is a
/// carriage-return, the buffer is scanned and the appropriate action
/// performed.
/// By default, an echo feature is enabled to display commands on the terminal.
pub fn input_byte(&mut self, input: u8) {
// Strip carriage returns
if input == 0x0A {
return;
}
let outcome = if input == 0x0D {
#[cfg(not(feature = "echo"))]
{
// Echo the command
write!(self.context, "\r").unwrap();
if let Ok(s) = core::str::from_utf8(&self.buffer[0..self.used]) {
write!(self.context, "{}", s).unwrap();
}
}
// Handle the command
self.process_command();
Outcome::CommandProcessed
Expand All @@ -296,19 +305,22 @@ where
self.buffer[self.used] = input;
self.used += 1;

// We have to do this song and dance because `self.prompt()` needs
// a mutable reference to self, and we can't have that while
// holding a reference to the buffer at the same time.
// This line grabs the buffer, checks it's OK, then releases it again
let valid = core::str::from_utf8(&self.buffer[0..self.used]).is_ok();
// Now we've released the buffer, we can draw the prompt
if valid {
write!(self.context, "\r").unwrap();
self.prompt(false);
}
// Grab the buffer again to render it to the screen
if let Ok(s) = core::str::from_utf8(&self.buffer[0..self.used]) {
write!(self.context, "{}", s).unwrap();
#[cfg(feature = "echo")]
{
// We have to do this song and dance because `self.prompt()` needs
// a mutable reference to self, and we can't have that while
// holding a reference to the buffer at the same time.
// This line grabs the buffer, checks it's OK, then releases it again
let valid = core::str::from_utf8(&self.buffer[0..self.used]).is_ok();
// Now we've released the buffer, we can draw the prompt
if valid {
write!(self.context, "\r").unwrap();
self.prompt(false);
}
// Grab the buffer again to render it to the screen
if let Ok(s) = core::str::from_utf8(&self.buffer[0..self.used]) {
write!(self.context, "{}", s).unwrap();
}
}
Outcome::NeedMore
} else {
Expand Down