Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 20 additions & 4 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ pub struct Engine {
has_history: bool,
}

pub enum Signal {
SUCCESS(String),
SIGINT, // Typically Ctrl-c
EOF, // Typically Ctrl-d
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could use CtrlC and CtrlD here instead of SIGINT/EOF. Helps with the readability and probably is a little more accurate if we're running on a platform without signals.

}

pub fn print_message(stdout: &mut Stdout, msg: &str) -> Result<()> {
stdout
.queue(Print("\n"))?
Expand All @@ -59,6 +65,15 @@ pub fn print_message(stdout: &mut Stdout, msg: &str) -> Result<()> {
Ok(())
}

pub fn print_crlf(stdout: &mut Stdout) -> Result<()> {
stdout
.queue(Print("\n"))?
.queue(MoveToColumn(1))?;
stdout.flush()?;

Ok(())
}

fn buffer_repaint(stdout: &mut Stdout, engine: &Engine, prompt_offset: u16) -> Result<()> {
let raw_buffer = engine.get_buffer();
let new_index = engine.get_insertion_point();
Expand Down Expand Up @@ -286,7 +301,7 @@ impl Engine {
self.line_buffer.move_word_right()
}

pub fn read_line(&mut self, stdout: &mut Stdout) -> Result<String> {
pub fn read_line(&mut self, stdout: &mut Stdout) -> Result<Signal> {
// print our prompt
stdout
.execute(SetForegroundColor(Color::Blue))?
Expand All @@ -305,7 +320,7 @@ impl Engine {
}) => match code {
KeyCode::Char('d') => {
if self.get_buffer().is_empty() {
return Ok("exit".to_string());
return Ok(Signal::EOF);
} else {
self.run_edit_commands(&[EditCommand::Delete]);
}
Expand All @@ -332,7 +347,8 @@ impl Engine {
self.run_edit_commands(&[EditCommand::MoveRight]);
}
KeyCode::Char('c') => {
return Ok("".to_string());
self.run_edit_commands(&[EditCommand::Clear]);
return Ok(Signal::SIGINT);
}
KeyCode::Char('h') => {
self.run_edit_commands(&[EditCommand::Backspace]);
Expand Down Expand Up @@ -403,7 +419,7 @@ impl Engine {
EditCommand::Clear,
]);

return Ok(buffer);
return Ok(Signal::SUCCESS(buffer));
}
KeyCode::Up => {
self.run_edit_commands(&[EditCommand::PreviousHistory]);
Expand Down
24 changes: 16 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crossterm::{
mod line_buffer;

mod engine;
use engine::{print_message, Engine};
use engine::{Engine, Signal, print_crlf, print_message};

// this fn is totally ripped off from crossterm's examples
// it's really a diagnostic routine to see if crossterm is
Expand Down Expand Up @@ -59,13 +59,21 @@ fn main() -> Result<()> {
let mut engine = Engine::new();

loop {
if let Ok(buffer) = engine.read_line(&mut stdout) {
if buffer.trim() == "exit" {
break;
}

if !buffer.trim().is_empty() {
print_message(&mut stdout, &format!("Our buffer: {}", buffer))?;
if let Ok(sig) = engine.read_line(&mut stdout) {
match sig {
Signal::EOF => {
break;
}
Signal::SUCCESS(buffer) => {
if (buffer.trim() == "exit") || (buffer.trim() == "logout") {
break;
}
print_message(&mut stdout, &format!("Our buffer: {}", buffer))?;
}
Signal::SIGINT => {
// We need to move one line down to start with the prompt on a new line
print_crlf(&mut stdout)?;
}
}
}
}
Expand Down