Skip to content

Commit 5c0c0fd

Browse files
authored
Merge pull request #9 from badrbouslikhin/master
Feature gate local echo.
2 parents 7cffa63 + ecbeb9b commit 5c0c0fd

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ readme = "README.md"
1111
[dependencies]
1212

1313

14+
[features]
15+
default = ["echo"]
16+
echo = []
17+
18+
1419
[dev-dependencies]
1520
pancurses = "0.16"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ It contains multiple paragraphs and should be preceeded by the parameter list.
171171

172172
* None
173173

174+
### v0.3.3
175+
176+
* Add the possibility to disable local echo (via echo feature, enabled by default)
177+
174178
### v0.3.2
175179

176180
* Shortened help output.

src/lib.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,21 @@ where
276276
/// Add a byte to the menu runner's buffer. If this byte is a
277277
/// carriage-return, the buffer is scanned and the appropriate action
278278
/// performed.
279+
/// By default, an echo feature is enabled to display commands on the terminal.
279280
pub fn input_byte(&mut self, input: u8) {
280281
// Strip carriage returns
281282
if input == 0x0A {
282283
return;
283284
}
284285
let outcome = if input == 0x0D {
286+
#[cfg(not(feature = "echo"))]
287+
{
288+
// Echo the command
289+
write!(self.context, "\r").unwrap();
290+
if let Ok(s) = core::str::from_utf8(&self.buffer[0..self.used]) {
291+
write!(self.context, "{}", s).unwrap();
292+
}
293+
}
285294
// Handle the command
286295
self.process_command();
287296
Outcome::CommandProcessed
@@ -296,19 +305,22 @@ where
296305
self.buffer[self.used] = input;
297306
self.used += 1;
298307

299-
// We have to do this song and dance because `self.prompt()` needs
300-
// a mutable reference to self, and we can't have that while
301-
// holding a reference to the buffer at the same time.
302-
// This line grabs the buffer, checks it's OK, then releases it again
303-
let valid = core::str::from_utf8(&self.buffer[0..self.used]).is_ok();
304-
// Now we've released the buffer, we can draw the prompt
305-
if valid {
306-
write!(self.context, "\r").unwrap();
307-
self.prompt(false);
308-
}
309-
// Grab the buffer again to render it to the screen
310-
if let Ok(s) = core::str::from_utf8(&self.buffer[0..self.used]) {
311-
write!(self.context, "{}", s).unwrap();
308+
#[cfg(feature = "echo")]
309+
{
310+
// We have to do this song and dance because `self.prompt()` needs
311+
// a mutable reference to self, and we can't have that while
312+
// holding a reference to the buffer at the same time.
313+
// This line grabs the buffer, checks it's OK, then releases it again
314+
let valid = core::str::from_utf8(&self.buffer[0..self.used]).is_ok();
315+
// Now we've released the buffer, we can draw the prompt
316+
if valid {
317+
write!(self.context, "\r").unwrap();
318+
self.prompt(false);
319+
}
320+
// Grab the buffer again to render it to the screen
321+
if let Ok(s) = core::str::from_utf8(&self.buffer[0..self.used]) {
322+
write!(self.context, "{}", s).unwrap();
323+
}
312324
}
313325
Outcome::NeedMore
314326
} else {

0 commit comments

Comments
 (0)