use std::io::{IsTerminal as _, Write};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use crossterm::style::{Attribute, SetAttribute};
use crossterm::{cursor, execute, queue, terminal};
use hexicon_host::pane::{PaneInfo, PaneSpec, Placement, Rect, Screen, color};
const MORE_PROMPT: &str = "[More]";
pub(crate) struct Device {
out: Box<dyn Write>,
size: Box<dyn Fn() -> (usize, usize)>,
cursor: Box<dyn FnMut() -> Option<(usize, usize)>>,
}
impl Device {
fn real() -> Self {
Self {
out: Box::new(std::io::stdout()),
size: Box::new(size),
cursor: Box::new(|| {
cursor::position()
.ok()
.map(|(x, y)| (usize::from(x), usize::from(y)))
}),
}
}
}
pub(crate) struct Terminal {
device: Device,
paging: bool,
lines: usize,
screen: Screen,
panes: bool,
region: Option<Rect>,
status: (String, String),
}
enum Key {
Named(String),
End,
}
impl Terminal {
pub(crate) fn detect(paging: bool) -> Option<Self> {
if !(std::io::stdin().is_terminal() && std::io::stdout().is_terminal()) {
return None;
}
Some(Self::new(
Device::real(),
paging,
supports_control_sequences(),
))
}
fn new(device: Device, paging: bool, panes: bool) -> Self {
let (rows, columns) = (device.size)();
Self {
device,
paging,
lines: 0,
screen: Screen::new(rows, columns, false),
panes,
region: None,
status: (String::new(), String::new()),
}
}
pub(crate) fn width(&mut self) -> Option<usize> {
self.sync_size();
Some(self.screen.main().width)
}
fn page_length(&self) -> Option<usize> {
if !self.paging {
return None;
}
Some(self.screen.main().height.saturating_sub(2).max(1))
}
pub(crate) fn start_page(&mut self) {
self.lines = 0;
}
pub(crate) fn print(&mut self, text: &str) {
for piece in text.split_inclusive('\n') {
if let Some(length) = self.page_length()
&& self.lines >= length
{
let out = &mut self.device.out;
let _ = out.write_all(MORE_PROMPT.as_bytes());
let _ = out.flush();
let key = read_key(self.region.is_some());
let blank = " ".repeat(MORE_PROMPT.len());
let _ = write!(out, "\r{blank}\r");
self.lines = match key {
Key::Named(name) if name == "\n" => length - 1,
Key::Named(_) => 0,
Key::End => {
self.paging = false;
0
}
};
}
let out = &mut self.device.out;
let _ = out.write_all(piece.as_bytes());
if piece.ends_with('\n') {
self.lines += 1;
}
}
let _ = self.device.out.flush();
}
pub(crate) fn read_key(&mut self) -> Option<String> {
let _ = self.device.out.flush();
self.start_page();
match read_key(self.region.is_some()) {
Key::Named(name) => Some(name),
Key::End => None,
}
}
pub(crate) fn clear(&mut self) {
let _ = queue!(
self.device.out,
terminal::Clear(terminal::ClearType::All),
cursor::MoveTo(0, 0)
);
if self.region.is_some() {
let main = self.screen.main();
let _ = queue!(self.device.out, cursor::MoveTo(0, main.y as u16));
self.draw_all();
}
let _ = self.device.out.flush();
self.start_page();
}
pub(crate) fn has_panes(&self) -> bool {
self.panes
}
pub(crate) fn panes<T>(&mut self, change: impl FnOnce(&mut Screen) -> T) -> T {
let result = change(&mut self.screen);
self.refresh();
result
}
pub(crate) fn panes_later(&mut self, change: impl FnOnce(&mut Screen)) {
change(&mut self.screen);
}
pub(crate) fn flush_panes(&mut self) {
self.refresh();
}
pub(crate) fn create_pane(
&mut self,
parent: Option<u32>,
placement: Placement,
spec: PaneSpec,
) -> Option<u32> {
if !self.panes {
return None;
}
self.sync_size();
self.panes(|screen| screen.create(parent, placement, spec))
}
pub(crate) fn pane_info(&mut self, id: u32) -> Option<PaneInfo> {
self.sync_size();
self.screen.info(id)
}
pub(crate) fn set_status(&mut self, left: Option<&str>, right: Option<&str>) {
if !self.panes {
return;
}
if let Some(left) = left {
self.status.0 = left.to_owned();
}
if let Some(right) = right {
self.status.1 = right.to_owned();
}
let (left, right) = self.status.clone();
self.panes(|screen| screen.set_status(&left, &right));
}
fn sync_size(&mut self) {
let (rows, columns) = (self.device.size)();
self.screen.resize(rows, columns);
self.refresh();
}
fn refresh(&mut self) {
if self.screen.take_layout_changed() {
self.screen.take_changed();
self.apply_layout();
self.draw_all();
} else {
for id in self.screen.take_changed() {
self.draw(id);
}
}
let _ = self.device.out.flush();
}
fn apply_layout(&mut self) {
let (rows, columns) = self.screen.size();
let mut main = self.screen.main();
if main.height == 0 {
main = Rect {
y: rows.saturating_sub(1),
height: 1,
..main
};
}
let whole = Rect {
x: 0,
y: 0,
width: columns,
height: rows,
};
if self.region.is_none() && main == whole {
return;
}
let old = self.region.unwrap_or(whole);
let _ = self.device.out.flush();
let (column, row) =
(self.device.cursor)().unwrap_or((0, old.y + old.height.saturating_sub(1)));
let bottom = main.y + main.height - 1;
let mut row = row;
let out = &mut self.device.out;
if row > bottom {
let lines = row - bottom;
let _ = write!(out, "\x1b[{};{}r", old.y + 1, old.y + old.height);
let _ = queue!(out, terminal::ScrollUp(lines as u16));
row = bottom;
}
row = row.max(main.y);
for line in main.y..=bottom {
if line < old.y || line >= old.y + old.height {
let _ = queue!(
out,
cursor::MoveTo(0, line as u16),
terminal::Clear(terminal::ClearType::CurrentLine)
);
}
}
let _ = write!(out, "\x1b[{};{}r", main.y + 1, bottom + 1);
let _ = queue!(out, cursor::MoveTo(column as u16, row as u16));
self.region = Some(main);
}
fn draw_all(&mut self) {
for (id, _) in self.screen.panes() {
self.draw(id);
}
}
fn draw(&mut self, id: u32) {
let Some(rect) = self.screen.rect(id) else {
return;
};
if rect.width == 0 || rect.height == 0 {
return;
}
let reverse = self.screen.screen_color(id) == Some(color::STATUS_BG);
let out = &mut self.device.out;
let _ = queue!(out, cursor::SavePosition);
for (i, line) in self.screen.view(id).iter().enumerate() {
let _ = queue!(out, cursor::MoveTo(rect.x as u16, (rect.y + i) as u16));
if reverse {
let _ = queue!(out, SetAttribute(Attribute::Reverse));
}
let _ = out.write_all(line.as_bytes());
if reverse {
let _ = queue!(out, SetAttribute(Attribute::Reset));
}
}
let _ = queue!(out, cursor::RestorePosition);
}
fn restore(&mut self) {
if self.region.take().is_some() {
let (rows, _) = self.screen.size();
let out = &mut self.device.out;
let _ = write!(out, "\x1b[r");
let _ = queue!(out, cursor::MoveTo(0, rows.saturating_sub(1) as u16));
let _ = writeln!(out);
let _ = out.flush();
}
}
}
impl Drop for Terminal {
fn drop(&mut self) {
self.restore();
}
}
fn size() -> (usize, usize) {
terminal::size().map_or((24, 80), |(columns, rows)| {
(usize::from(rows), usize::from(columns))
})
}
fn supports_control_sequences() -> bool {
#[cfg(windows)]
{
crossterm::ansi_support::supports_ansi()
}
#[cfg(not(windows))]
{
std::env::var("TERM").map_or(true, |term| term != "dumb")
}
}
fn read_key(panes: bool) -> Key {
if terminal::enable_raw_mode().is_err() {
return Key::End;
}
let key = loop {
match crossterm::event::read() {
Ok(Event::Key(event)) if event.kind != KeyEventKind::Release => {
if interrupts(&event) {
let _ = terminal::disable_raw_mode();
if panes {
let (rows, _) = size();
print!("\x1b[r");
let _ = execute!(
std::io::stdout(),
cursor::MoveTo(0, rows.saturating_sub(1) as u16)
);
}
println!();
std::process::exit(130);
}
if let Some(name) = key_name(&event) {
break Key::Named(name);
}
}
Ok(_) => {}
Err(_) => break Key::End,
}
};
let _ = terminal::disable_raw_mode();
key
}
fn interrupts(event: &KeyEvent) -> bool {
event.modifiers.contains(KeyModifiers::CONTROL)
&& matches!(event.code, KeyCode::Char('c' | 'C'))
}
fn key_name(event: &KeyEvent) -> Option<String> {
let control = event.modifiers.contains(KeyModifiers::CONTROL);
let alt = event.modifiers.contains(KeyModifiers::ALT);
let name = match event.code {
KeyCode::Char(c) if control && alt => c.to_string(),
KeyCode::Char(c) if control && c.is_ascii_alphabetic() => {
format!("[ctrl-{}]", c.to_ascii_lowercase())
}
KeyCode::Char(c) if alt && c.is_ascii_alphanumeric() => {
format!("[alt-{}]", c.to_ascii_lowercase())
}
KeyCode::Char(c) => c.to_string(),
KeyCode::Enter => "\n".to_owned(),
KeyCode::Tab => "\t".to_owned(),
KeyCode::Esc => "[esc]".to_owned(),
KeyCode::Backspace => "[bksp]".to_owned(),
KeyCode::Delete => "[del]".to_owned(),
KeyCode::Up => "[up]".to_owned(),
KeyCode::Down => "[down]".to_owned(),
KeyCode::Left => "[left]".to_owned(),
KeyCode::Right => "[right]".to_owned(),
KeyCode::Home => "[home]".to_owned(),
KeyCode::End => "[end]".to_owned(),
KeyCode::PageUp => "[page up]".to_owned(),
KeyCode::PageDown => "[page down]".to_owned(),
KeyCode::Insert => "[insert]".to_owned(),
KeyCode::F(number) => format!("[f{number}]"),
_ => return None,
};
Some(name)
}
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
use std::rc::Rc;
use hexicon_host::pane::{align, kind, units};
#[derive(Clone, Default)]
struct Shared(Rc<RefCell<Vec<u8>>>);
impl Write for Shared {
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
self.0.borrow_mut().extend_from_slice(data);
Ok(data.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
struct Vt {
grid: Vec<Vec<char>>,
x: usize,
y: usize,
top: usize,
bottom: usize,
saved: (usize, usize),
}
impl Vt {
fn new(rows: usize, columns: usize) -> Self {
Self {
grid: vec![vec![' '; columns]; rows],
x: 0,
y: 0,
top: 0,
bottom: rows - 1,
saved: (0, 0),
}
}
fn scroll_up(&mut self, lines: usize) {
for _ in 0..lines {
self.grid.remove(self.top);
let columns = self.grid[0].len();
self.grid.insert(self.bottom, vec![' '; columns]);
}
}
fn run(&mut self, bytes: &[u8]) {
let text = String::from_utf8_lossy(bytes);
let mut chars = text.chars().peekable();
while let Some(c) = chars.next() {
match c {
'\x1b' => match chars.next() {
Some('7') => self.saved = (self.x, self.y),
Some('8') => (self.x, self.y) = self.saved,
Some('[') => {
let mut params = String::new();
let mut end = ' ';
for c in chars.by_ref() {
if c.is_ascii_digit() || c == ';' {
params.push(c);
} else {
end = c;
break;
}
}
let n: Vec<usize> =
params.split(';').map(|p| p.parse().unwrap_or(0)).collect();
let arg = |i: usize, default: usize| match n.get(i) {
Some(&0) | None => default,
Some(&v) => v,
};
match end {
'H' => {
self.y = arg(0, 1) - 1;
self.x = arg(1, 1) - 1;
}
'r' => {
self.top = arg(0, 1) - 1;
self.bottom = arg(1, self.grid.len()) - 1;
(self.x, self.y) = (0, 0);
}
'S' => self.scroll_up(arg(0, 1)),
'K' => self.grid[self.y].fill(' '),
'J' => self.grid.iter_mut().for_each(|line| line.fill(' ')),
_ => {}
}
}
_ => {}
},
'\r' => self.x = 0,
'\n' => {
self.x = 0;
if self.y == self.bottom {
self.scroll_up(1);
} else if self.y + 1 < self.grid.len() {
self.y += 1;
}
}
c => {
if let Some(cell) = self.grid[self.y].get_mut(self.x) {
*cell = c;
}
self.x += 1;
}
}
}
}
fn lines(&self) -> Vec<String> {
self.grid
.iter()
.map(|line| line.iter().collect::<String>().trim_end().to_owned())
.collect()
}
}
fn stand_in(rows: usize, columns: usize) -> (Terminal, Shared) {
let out = Shared::default();
let seen = out.clone();
let device = Device {
out: Box::new(out.clone()),
size: Box::new(move || (rows, columns)),
cursor: Box::new(move || {
let mut vt = Vt::new(rows, columns);
vt.run(&seen.0.borrow());
Some((vt.x, vt.y))
}),
};
(Terminal::new(device, false, true), out)
}
fn screen_of(out: &Shared, rows: usize, columns: usize) -> Vec<String> {
let mut vt = Vt::new(rows, columns);
vt.run(&out.0.borrow());
vt.lines()
}
#[test]
fn a_status_pane_stays_while_text_scrolls_below_it() {
let (mut terminal, out) = stand_in(6, 20);
for n in 1..=6 {
terminal.print(&format!("line {n}\n"));
}
let spec = PaneSpec {
kind: kind::TEXT,
align: align::TOP,
size: 0,
units: units::CELLS,
style: 0,
};
let id = terminal.create_pane(None, Placement::First, spec).unwrap();
terminal.panes(|screen| {
screen.write(id, "Kitchen\n");
screen.size_to_contents(id);
});
assert_eq!(
screen_of(&out, 6, 20),
["Kitchen", "line 3", "line 4", "line 5", "line 6", ""]
);
terminal.print("line 7\nline 8\n");
assert_eq!(
screen_of(&out, 6, 20),
["Kitchen", "line 5", "line 6", "line 7", "line 8", ""]
);
terminal.panes_later(|screen| {
screen.clear(id);
screen.write(id, "Hall\n");
});
assert_eq!(
screen_of(&out, 6, 20)[0],
"Kitchen",
"not drawn until flushed"
);
terminal.flush_panes();
assert_eq!(screen_of(&out, 6, 20)[0], "Hall");
terminal.panes(|screen| screen.delete(id));
assert_eq!(screen_of(&out, 6, 20)[0], "");
terminal.print("line 9\n");
drop(terminal);
let lines = screen_of(&out, 6, 20);
assert!(lines.contains(&"line 9".to_owned()), "{lines:?}");
}
#[test]
fn a_bottom_pane_pushes_the_text_up() {
let (mut terminal, out) = stand_in(5, 20);
for n in 1..=5 {
terminal.print(&format!("line {n}\n"));
}
let spec = PaneSpec {
kind: kind::TEXT,
align: align::BOTTOM,
size: 1,
units: units::CELLS,
style: 0,
};
let id = terminal.create_pane(None, Placement::Last, spec).unwrap();
terminal.panes(|screen| screen.write(id, "Exits: north"));
assert_eq!(
screen_of(&out, 5, 20),
["line 3", "line 4", "line 5", "", "Exits: north"]
);
}
#[test]
fn the_old_status_line_is_shown_at_the_top() {
let (mut terminal, out) = stand_in(4, 20);
terminal.print("Welcome.\n");
terminal.set_status(Some("Cellar"), None);
terminal.set_status(None, Some("3/10"));
assert_eq!(screen_of(&out, 4, 20)[0], " Cellar 3/10");
}
fn name(code: KeyCode, modifiers: KeyModifiers) -> Option<String> {
key_name(&KeyEvent::new(code, modifiers))
}
#[test]
fn keys_go_by_the_names_programs_know() {
let none = KeyModifiers::NONE;
assert_eq!(name(KeyCode::Char('a'), none).as_deref(), Some("a"));
assert_eq!(
name(KeyCode::Char('A'), KeyModifiers::SHIFT).as_deref(),
Some("A")
);
assert_eq!(name(KeyCode::Enter, none).as_deref(), Some("\n"));
assert_eq!(name(KeyCode::PageUp, none).as_deref(), Some("[page up]"));
assert_eq!(name(KeyCode::F(5), none).as_deref(), Some("[f5]"));
assert_eq!(
name(KeyCode::Char('x'), KeyModifiers::CONTROL).as_deref(),
Some("[ctrl-x]")
);
assert_eq!(
name(KeyCode::Char('x'), KeyModifiers::ALT).as_deref(),
Some("[alt-x]")
);
assert_eq!(
name(
KeyCode::Char('@'),
KeyModifiers::CONTROL | KeyModifiers::ALT
)
.as_deref(),
Some("@")
);
assert_eq!(name(KeyCode::CapsLock, none), None);
}
#[test]
fn only_control_c_interrupts() {
let event = |code, modifiers| KeyEvent::new(code, modifiers);
assert!(interrupts(&event(
KeyCode::Char('c'),
KeyModifiers::CONTROL
)));
assert!(!interrupts(&event(KeyCode::Char('c'), KeyModifiers::NONE)));
assert!(!interrupts(&event(
KeyCode::Char('x'),
KeyModifiers::CONTROL
)));
}
}