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
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod cli;
mod client;
mod config;
mod editor;
mod render;
mod repl;
mod term;
mod utils;

use std::sync::Arc;
Expand Down
11 changes: 8 additions & 3 deletions src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::client::ChatGptClient;
use crate::config::{Config, Role};
use crate::editor;
use crate::render::{self, MarkdownRender};
use crate::term;
use crate::utils::{copy, dump};
use anyhow::{anyhow, Result};
use crossbeam::channel::{unbounded, Sender};
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Repl {
dump_repl_help();
}
".clear" => match args {
Some("screen") => self.editor.clear_scrollback()?,
Some("screen") => term::clear_screen(0)?,
Some("history") => {
let history = Box::new(self.editor.history_mut());
history
Expand All @@ -139,7 +139,7 @@ impl Repl {
"// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)",
1,
);
let content = editor::edit()?;
let content = term::edit()?;
dump("", 1);
handler.handle(ReplCmd::Submit(content))?;
}
Expand Down Expand Up @@ -189,6 +189,11 @@ impl Repl {
ReedlineEvent::MenuNext,
]),
);
keybindings.add_binding(
KeyModifiers::CONTROL,
KeyCode::Char('l'),
ReedlineEvent::ExecuteHostCommand(".clear screen".into()),
);
keybindings
}

Expand Down
28 changes: 27 additions & 1 deletion src/editor.rs → src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyModifiers},
queue, style,
terminal::{self, disable_raw_mode, enable_raw_mode},
terminal::{self, disable_raw_mode, enable_raw_mode, ClearType},
};

use crate::utils::paste;
Expand Down Expand Up @@ -108,3 +108,29 @@ impl<'a, T: Write> Session<'a, T> {
Ok(())
}
}

pub fn clear_screen(keep_lines: u16) -> Result<()> {
enable_raw_mode()?;
let mut stdout = io::stdout();

let ret = clear_screen_inner(&mut stdout, keep_lines);

// restore terminal
disable_raw_mode()?;

ret
}

fn clear_screen_inner(writer: &mut Stdout, keep_lines: u16) -> Result<()> {
let (_, h) = terminal::size()?;
queue!(
writer,
style::Print("\n".repeat((h - 2).into())),
terminal::ScrollUp(2),
cursor::MoveTo(0, 0),
terminal::Clear(ClearType::FromCursorDown),
terminal::ScrollUp(keep_lines),
)?;
writer.flush()?;
Ok(())
}