Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 21 additions & 3 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::io::{Stdout, Write};
use std::ops::Deref;

use crossterm::{
cursor::{MoveToColumn, RestorePosition, SavePosition},
cursor::{MoveTo, MoveToColumn, RestorePosition, SavePosition},
event::{read, Event, KeyCode, KeyEvent, KeyModifiers},
style::{Color, Print, ResetColor, SetForegroundColor},
terminal::{Clear, ClearType},
terminal::{self, Clear, ClearType},
QueueableCommand, Result,
};

Expand Down Expand Up @@ -60,6 +60,7 @@ pub enum Signal {
Success(String),
CtrlC, // Interrupt current editing
CtrlD, // End terminal session
CtrlL, // FormFeed/Clear current screen
}

pub fn print_message(stdout: &mut Stdout, msg: &str) -> Result<()> {
Expand All @@ -81,6 +82,16 @@ pub fn print_crlf(stdout: &mut Stdout) -> Result<()> {
Ok(())
}

pub fn clear_screen(stdout: &mut Stdout) -> Result<()> {
let (_, num_lines) = terminal::size()?;
for _ in 0..2 * num_lines {
stdout.queue(Print("\n"))?;
}
stdout.queue(MoveTo(0, 0))?;
stdout.flush()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

You might be able to run this crossterm command for a clear in one step: https://docs.rs/crossterm/0.19.0/crossterm/terminal/enum.ClearType.html

Copy link
Member Author

Choose a reason for hiding this comment

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

ClearType::All only blanks the currently visible screen, so you cannot scroll back up to the previous output as with bash et al.

Copy link
Contributor

Choose a reason for hiding this comment

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

Today I learned :)

Ok(())
}

fn queue_prompt(stdout: &mut Stdout) -> Result<()> {
// print our prompt
stdout
Expand Down Expand Up @@ -436,7 +447,11 @@ impl Engine {
}

pub fn read_line(&mut self, stdout: &mut Stdout) -> Result<Signal> {
queue_prompt(stdout)?;
if self.history_search.is_some() {
history_search_paint(stdout, &self)?;
} else {
buffer_paint(stdout, &self)?;
}
stdout.flush()?;

loop {
Expand Down Expand Up @@ -477,6 +492,9 @@ impl Engine {
self.run_edit_commands(&[EditCommand::Clear]);
return Ok(Signal::CtrlC);
}
KeyCode::Char('l') => {
return Ok(Signal::CtrlL);
}
KeyCode::Char('h') => {
self.run_edit_commands(&[EditCommand::Backspace]);
}
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::stdout;
mod line_buffer;

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

mod diagnostic;
use diagnostic::print_events;
Expand Down Expand Up @@ -40,12 +40,19 @@ fn main() -> Result<()> {
if (buffer.trim() == "exit") || (buffer.trim() == "logout") {
break;
}
if buffer.trim() == "clear" {
clear_screen(&mut stdout)?;
continue;
}
print_message(&mut stdout, &format!("Our buffer: {}", buffer))?;
}
Signal::CtrlC => {
// We need to move one line down to start with the prompt on a new line
print_crlf(&mut stdout)?;
}
Signal::CtrlL => {
clear_screen(&mut stdout)?;
}
}
}
}
Expand Down