use super::*;
use crate::terminal::{cells::*, Color};
use melib::error::{MeliError, Result};
use melib::text_processing::wcwidth;
use nix::sys::wait::WaitStatus;
use nix::sys::wait::{waitpid, WaitPidFlag};
#[derive(Debug)]
pub struct EmbedGrid {
cursor: (usize, usize),
scroll_region: ScrollRegion,
pub grid: CellBuffer,
pub state: State,
pub stdin: std::fs::File,
pub child_pid: nix::unistd::Pid,
pub terminal_size: (usize, usize),
fg_color: Color,
bg_color: Color,
prev_fg_color: Option<Color>,
prev_bg_color: Option<Color>,
show_cursor: bool,
origin_mode: bool,
auto_wrap_mode: bool,
wrap_next: bool,
codepoints: CodepointBuf,
}
#[derive(Debug, PartialEq)]
enum CodepointBuf {
None,
TwoCodepoints(u8),
ThreeCodepoints(u8, Option<u8>),
FourCodepoints(u8, Option<u8>, Option<u8>),
}
impl EmbedGrid {
pub fn new(stdin: std::fs::File, child_pid: nix::unistd::Pid) -> Self {
EmbedGrid {
cursor: (0, 0),
scroll_region: ScrollRegion {
top: 0,
bottom: 0,
left: 0,
..Default::default()
},
terminal_size: (0, 0),
grid: CellBuffer::default(),
state: State::Normal,
stdin,
child_pid,
fg_color: Color::Default,
bg_color: Color::Default,
prev_fg_color: None,
prev_bg_color: None,
show_cursor: true,
auto_wrap_mode: true,
wrap_next: false,
origin_mode: false,
codepoints: CodepointBuf::None,
}
}
pub fn set_terminal_size(&mut self, new_val: (usize, usize)) {
if new_val == self.terminal_size {
return;
}
self.scroll_region.top = 0;
self.scroll_region.bottom = new_val.1.saturating_sub(1);
self.terminal_size = new_val;
self.grid.resize(new_val.0, new_val.1, Cell::default());
self.grid.clear(Cell::default());
self.cursor = (0, 0);
self.wrap_next = false;
let winsize = Winsize {
ws_row: <u16>::try_from(new_val.1).unwrap(),
ws_col: <u16>::try_from(new_val.0).unwrap(),
ws_xpixel: 0,
ws_ypixel: 0,
};
let master_fd = self.stdin.as_raw_fd();
let _ = unsafe { set_window_size(master_fd, &winsize) };
let _ = nix::sys::signal::kill(self.child_pid, nix::sys::signal::SIGWINCH);
}
pub fn wake_up(&self) {
let _ = nix::sys::signal::kill(self.child_pid, nix::sys::signal::SIGCONT);
}
pub fn stop(&self) {
let _ = nix::sys::signal::kill(debug!(self.child_pid), nix::sys::signal::SIGSTOP);
}
pub fn is_active(&self) -> Result<WaitStatus> {
debug!(waitpid(self.child_pid, Some(WaitPidFlag::WNOHANG),))
.map_err(|e| MeliError::new(e.to_string()))
}
pub fn process_byte(&mut self, byte: u8) {
let EmbedGrid {
ref mut cursor,
ref mut scroll_region,
ref terminal_size,
ref mut grid,
ref mut state,
ref mut stdin,
ref mut fg_color,
ref mut bg_color,
ref mut prev_fg_color,
ref mut prev_bg_color,
ref mut codepoints,
ref mut show_cursor,
ref mut auto_wrap_mode,
ref mut wrap_next,
ref mut origin_mode,
child_pid: _,
} = self;
macro_rules! increase_cursor_x {
() => {
if cursor.0 + 1 < terminal_size.0 {
cursor.0 += 1;
} else if *auto_wrap_mode {
*wrap_next = true;
}
};
}
macro_rules! cursor_x {
() => {{
if cursor.0 >= terminal_size.0 {
cursor.0 = terminal_size.0.saturating_sub(1);
}
cursor.0
}};
}
macro_rules! cursor_y {
() => {
std::cmp::min(
cursor.1 + scroll_region.top,
terminal_size.1.saturating_sub(1),
)
};
}
macro_rules! cursor_val {
() => {
(cursor_x!(), cursor_y!())
};
}
let mut state = state;
match (byte, &mut state) {
(b'\x1b', State::Normal) => {
*state = State::ExpectingControlChar;
}
(b']', State::ExpectingControlChar) => {
let buf1 = SmallVec::new();
*state = State::Osc1(buf1);
}
(b'[', State::ExpectingControlChar) => {
*state = State::Csi;
}
(b'(', State::ExpectingControlChar) => {
*state = State::G0;
}
(b'D', State::ExpectingControlChar) => {
if cursor.1 == scroll_region.bottom {
grid.scroll_up(scroll_region, scroll_region.top, 1);
} else {
cursor.1 += 1;
}
*wrap_next = false;
*state = State::Normal;
}
(b'J', State::ExpectingControlChar) => {
for y in cursor.1..terminal_size.1 {
for x in cursor.0..terminal_size.0 {
grid[(x, y)] = Cell::default();
}
}
*state = State::Normal;
}
(b'K', State::ExpectingControlChar) => {
for x in cursor.0..terminal_size.0 {
grid[(x, cursor.1)] = Cell::default();
}
*state = State::Normal;
}
(_, State::ExpectingControlChar) => {
*state = State::Normal;
}
(b'?', State::Csi) => {
let buf1 = SmallVec::new();
*state = State::CsiQ(buf1);
}
(c, State::Osc1(ref mut buf)) if (c >= b'0' && c <= b'9') || c == b'?' => {
buf.push(c);
}
(b';', State::Osc1(ref mut buf1_p)) => {
let buf1 = std::mem::replace(buf1_p, SmallVec::new());
let buf2 = SmallVec::new();
*state = State::Osc2(buf1, buf2);
}
(c, State::Osc2(_, ref mut buf)) if (c >= b'0' && c <= b'9') || c == b'?' => {
buf.push(c);
}
(b'\r', State::Normal) => {
cursor.0 = 0;
*wrap_next = false;
}
(b'\n', State::Normal) => {
if cursor.1 + 1 < terminal_size.1 {
if cursor.1 == scroll_region.bottom {
grid.scroll_up(scroll_region, cursor.1, 1);
} else {
cursor.1 += 1;
}
}
*wrap_next = false;
}
(b'', State::Normal) => {
}
(0x08, State::Normal) => {
if cursor.0 > 0 {
cursor.0 -= 1;
}
}
(c, State::Normal) => {
let c = if *codepoints == CodepointBuf::None && c & 0x80 == 0 {
c as char
} else {
match codepoints {
CodepointBuf::None if c & 0b1110_0000 == 0b1100_0000 => {
*codepoints = CodepointBuf::TwoCodepoints(c);
return;
}
CodepointBuf::None if c & 0b1111_0000 == 0b1110_0000 => {
*codepoints = CodepointBuf::ThreeCodepoints(c, None);
return;
}
CodepointBuf::None if c & 0b1111_1000 == 0b1111_0000 => {
*codepoints = CodepointBuf::FourCodepoints(c, None, None);
return;
}
CodepointBuf::TwoCodepoints(b) => {
unsafe { std::str::from_utf8_unchecked(&[*b, c]) }
.chars()
.next()
.unwrap()
}
CodepointBuf::ThreeCodepoints(b, Some(b1)) => {
unsafe { std::str::from_utf8_unchecked(&[*b, *b1, c]) }
.chars()
.next()
.unwrap()
}
CodepointBuf::ThreeCodepoints(_, ref mut b @ None) => {
*b = Some(c);
return;
}
CodepointBuf::FourCodepoints(b, Some(b1), Some(b2)) => {
unsafe { std::str::from_utf8_unchecked(&[*b, *b1, *b2, c]) }
.chars()
.next()
.unwrap()
}
CodepointBuf::FourCodepoints(_, ref mut b1 @ None, None) => {
*b1 = Some(c);
return;
}
CodepointBuf::FourCodepoints(_, _, ref mut b2 @ None) => {
*b2 = Some(c);
return;
}
_ => {
*codepoints = CodepointBuf::None;
return;
}
}
};
*codepoints = CodepointBuf::None;
if *auto_wrap_mode && *wrap_next {
*wrap_next = false;
if cursor.1 == scroll_region.bottom {
grid.scroll_up(scroll_region, scroll_region.top, 1);
} else {
cursor.1 += 1;
}
cursor.0 = 0;
}
grid[cursor_val!()].set_ch(c);
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
match wcwidth(u32::from(c)) {
Some(0) | None => {
grid[cursor_val!()].set_empty(true);
}
Some(1) => {}
Some(n) => {
for _ in 1..n {
increase_cursor_x!();
grid[cursor_val!()].set_empty(true);
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
}
}
}
increase_cursor_x!();
}
(b'u', State::Csi) => {
*show_cursor = true;
*state = State::Normal;
}
(b'm', State::Csi) => {
*fg_color = Color::Default;
*bg_color = Color::Default;
grid[cursor_val!()].set_fg(Color::Default);
grid[cursor_val!()].set_bg(Color::Default);
*state = State::Normal;
}
(b'C', State::Csi) => {
cursor.0 = std::cmp::min(cursor.0 + 1, terminal_size.0.saturating_sub(1));
*wrap_next = false;
*state = State::Normal;
}
(c, State::CsiQ(ref mut buf)) if c >= b'0' && c <= b'9' => {
buf.push(c);
}
(b'h', State::CsiQ(ref buf)) => {
match buf.as_slice() {
b"6" => {
*origin_mode = true;
}
b"7" => {
*auto_wrap_mode = true;
}
b"25" => {
*show_cursor = true;
*prev_fg_color = Some(grid[cursor_val!()].fg());
*prev_bg_color = Some(grid[cursor_val!()].bg());
grid[cursor_val!()].set_fg(Color::Black);
grid[cursor_val!()].set_bg(Color::White);
}
_ => {}
}
*state = State::Normal;
}
(b'l', State::CsiQ(ref mut buf)) => {
match buf.as_slice() {
b"6" => {
*origin_mode = false;
}
b"7" => {
*auto_wrap_mode = false;
}
b"25" => {
*show_cursor = false;
if let Some(fg_color) = prev_fg_color.take() {
grid[cursor_val!()].set_fg(fg_color);
} else {
grid[cursor_val!()].set_fg(*fg_color);
}
if let Some(bg_color) = prev_bg_color.take() {
grid[cursor_val!()].set_bg(bg_color);
} else {
grid[cursor_val!()].set_bg(*bg_color);
}
}
_ => {}
}
*state = State::Normal;
}
(c, State::Csi) if c >= b'0' && c <= b'9' => {
let mut buf1 = SmallVec::new();
buf1.push(c);
*state = State::Csi1(buf1);
}
(b'J', State::Csi) => {
clear_area(
grid,
(
(
0,
std::cmp::min(
cursor.1 + 1 + scroll_region.top,
terminal_size.1.saturating_sub(1),
),
),
(
terminal_size.0.saturating_sub(1),
terminal_size.1.saturating_sub(1),
),
),
Default::default(),
);
*state = State::Normal;
}
(b'K', State::Csi) => {
for x in cursor.0..terminal_size.0 {
grid[(x, cursor.1)] = Cell::default();
}
*state = State::Normal;
}
(b'L', State::Csi) | (b'L', State::Csi1(_)) => {
let n = if let State::Csi1(ref buf1) = state {
unsafe { std::str::from_utf8_unchecked(buf1) }
.parse::<usize>()
.unwrap()
} else {
1
};
grid.scroll_down(scroll_region, cursor.1, n);
*state = State::Normal;
}
(b'M', State::Csi) | (b'M', State::Csi1(_)) => {
let n = if let State::Csi1(ref buf1) = state {
unsafe { std::str::from_utf8_unchecked(buf1) }
.parse::<usize>()
.unwrap()
} else {
1
};
grid.scroll_up(scroll_region, cursor.1, n);
*state = State::Normal;
}
(b'A', State::Csi) => {
if cursor.1 > 0 {
cursor.1 -= 1;
} else {
}
*wrap_next = false;
*state = State::Normal;
}
(b'K', State::Csi1(buf)) if buf.as_ref() == b"0" => {
for x in cursor.0..terminal_size.0 {
grid[(x, cursor.1)] = Cell::default();
}
*state = State::Normal;
}
(b'K', State::Csi1(buf)) if buf.as_ref() == b"1" => {
for x in 0..=cursor.0 {
grid[(x, cursor.1)] = Cell::default();
}
*state = State::Normal;
}
(b'K', State::Csi1(buf)) if buf.as_ref() == b"2" => {
for y in 0..terminal_size.1 {
for x in 0..terminal_size.0 {
grid[(x, y)] = Cell::default();
}
}
clear_area(
grid,
((0, 0), pos_dec(*terminal_size, (1, 1))),
Default::default(),
);
*state = State::Normal;
}
(b'J', State::Csi1(ref buf)) if buf.as_ref() == b"0" => {
clear_area(
grid,
(
(
0,
std::cmp::min(
cursor.1 + 1 + scroll_region.top,
terminal_size.1.saturating_sub(1),
),
),
(
terminal_size.0.saturating_sub(1),
terminal_size.1.saturating_sub(1),
),
),
Default::default(),
);
*state = State::Normal;
}
(b'J', State::Csi1(ref buf)) if buf.as_ref() == b"1" => {
clear_area(
grid,
(
(0, 0),
(
terminal_size.0.saturating_sub(1),
cursor.1.saturating_sub(1) + scroll_region.top,
),
),
Default::default(),
);
*state = State::Normal;
}
(b'J', State::Csi1(ref buf)) if buf.as_ref() == b"2" => {
clear_area(
grid,
((0, 0), pos_dec(*terminal_size, (1, 1))),
Default::default(),
);
*state = State::Normal;
}
(b'X', State::Csi1(ref buf)) => {
let ps = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
let mut ctr = 0;
let (mut cur_x, mut cur_y) = cursor_val!();
while ctr < ps {
if cur_x >= terminal_size.0 {
cur_y += 1;
cur_x = 0;
if cur_y >= terminal_size.1 {
break;
}
}
grid[(cur_x, cur_y)] = Cell::default();
cur_x += 1;
ctr += 1;
}
*state = State::Normal;
}
(b't', State::Csi1(buf)) => {
if buf.as_ref() == b"18" || buf.as_ref() == b"19" {
stdin.write_all(b"\x1b[8;").unwrap();
stdin
.write_all((terminal_size.1).to_string().as_bytes())
.unwrap();
stdin.write_all(&[b';']).unwrap();
stdin
.write_all((terminal_size.0).to_string().as_bytes())
.unwrap();
stdin.write_all(&[b't']).unwrap();
stdin.flush().unwrap();
} else {
}
*state = State::Normal;
}
(b'n', State::Csi1(_)) => {
stdin.write_all(&[b'\x1b', b'[']).unwrap();
stdin
.write_all((cursor.1 + 1).to_string().as_bytes())
.unwrap();
stdin.write_all(&[b';']).unwrap();
stdin
.write_all((cursor.0 + 1).to_string().as_bytes())
.unwrap();
stdin.write_all(&[b'R']).unwrap();
stdin.flush().unwrap();
*state = State::Normal;
}
(b'A', State::Csi1(buf)) => {
let offset = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
if cursor.1 >= offset {
cursor.1 -= offset;
} else {
}
*state = State::Normal;
}
(b'B', State::Csi1(buf)) => {
let offset = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
if cursor.1 == scroll_region.bottom {
for y in scroll_region.top..scroll_region.bottom {
for x in 0..terminal_size.1 {
grid[(x, y)] = grid[(x, y + 1)];
}
}
for x in 0..terminal_size.1 {
grid[(x, scroll_region.bottom)] = Cell::default();
}
} else if offset + cursor.1 < terminal_size.1 {
cursor.1 += offset;
}
if scroll_region.top + cursor.1 >= terminal_size.1 {
cursor.1 = terminal_size.1.saturating_sub(1);
}
*wrap_next = false;
*state = State::Normal;
}
(b'D', State::Csi1(buf)) => {
let offset = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
if cursor.0 >= offset {
cursor.0 -= offset;
}
*state = State::Normal;
}
(b'E', State::Csi1(buf)) => {
let offset = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
if offset + cursor.1 < terminal_size.1 {
cursor.1 += offset;
}
if scroll_region.top + cursor.1 >= terminal_size.1 {
cursor.1 = terminal_size.1.saturating_sub(1);
}
cursor.0 = 0;
*wrap_next = false;
*state = State::Normal;
}
(b'F', State::Csi1(buf)) => {
let offset = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
cursor.1 = cursor.1.saturating_sub(offset);
cursor.0 = 0;
*wrap_next = false;
*state = State::Normal;
}
(b'G', State::Csi1(_)) | (b'G', State::Csi) => {
let new_col = if let State::Csi1(buf) = state {
unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap()
} else {
1
};
if new_col < terminal_size.0 {
cursor.0 = new_col.saturating_sub(1);
} else {
}
*wrap_next = false;
*state = State::Normal;
}
(b'C', State::Csi1(buf)) => {
let offset = unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap();
if cursor.0 + offset < terminal_size.0 {
cursor.0 += offset;
}
*state = State::Normal;
}
(b'P', State::Csi1(_)) | (b'P', State::Csi) => {
let offset = if let State::Csi1(buf) = state {
unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap()
} else {
1
};
for i in 0..(terminal_size.0 - cursor.0 - offset) {
grid[(cursor.0 + i, cursor.1)] = grid[(cursor.0 + i + offset, cursor.1)];
}
for x in (terminal_size.0 - offset)..terminal_size.0 {
grid[(x, cursor.1)].set_ch(' ');
}
*state = State::Normal;
}
(b'd', State::Csi1(_)) | (b'd', State::Csi) => {
let row = if let State::Csi1(buf) = state {
unsafe { std::str::from_utf8_unchecked(buf) }
.parse::<usize>()
.unwrap()
} else {
1
};
cursor.1 = row.saturating_sub(1);
if scroll_region.top + cursor.1 >= terminal_size.1 {
cursor.1 = terminal_size.1.saturating_sub(1);
}
*wrap_next = false;
*state = State::Normal;
}
(b';', State::Csi1(ref mut buf1_p)) => {
let buf1 = std::mem::replace(buf1_p, SmallVec::new());
let buf2 = SmallVec::new();
*state = State::Csi2(buf1, buf2);
}
(b'm', State::Csi1(ref buf1)) => {
match buf1.as_slice() {
b"0" => {
*fg_color = Color::Default;
*bg_color = Color::Default;
}
b"1" => { }
b"7" => {
let temp = *fg_color;
*fg_color = *bg_color;
*bg_color = temp;
}
b"27" => {
let temp = *fg_color;
*fg_color = *bg_color;
*bg_color = temp;
}
b"30" => *fg_color = Color::Black,
b"31" => *fg_color = Color::Red,
b"32" => *fg_color = Color::Green,
b"33" => *fg_color = Color::Yellow,
b"34" => *fg_color = Color::Blue,
b"35" => *fg_color = Color::Magenta,
b"36" => *fg_color = Color::Cyan,
b"37" => *fg_color = Color::White,
b"39" => *fg_color = Color::Default,
b"40" => *bg_color = Color::Black,
b"41" => *bg_color = Color::Red,
b"42" => *bg_color = Color::Green,
b"43" => *bg_color = Color::Yellow,
b"44" => *bg_color = Color::Blue,
b"45" => *bg_color = Color::Magenta,
b"46" => *bg_color = Color::Cyan,
b"47" => *bg_color = Color::White,
b"49" => *bg_color = Color::Default,
b"90" => *fg_color = Color::Black,
b"91" => *fg_color = Color::Red,
b"92" => *fg_color = Color::Green,
b"93" => *fg_color = Color::Yellow,
b"94" => *fg_color = Color::Blue,
b"95" => *fg_color = Color::Magenta,
b"96" => *fg_color = Color::Cyan,
b"97" => *fg_color = Color::White,
b"100" => *bg_color = Color::Black,
b"101" => *bg_color = Color::Red,
b"102" => *bg_color = Color::Green,
b"103" => *bg_color = Color::Yellow,
b"104" => *bg_color = Color::Blue,
b"105" => *bg_color = Color::Magenta,
b"106" => *bg_color = Color::Cyan,
b"107" => *bg_color = Color::White,
_ => {}
}
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
*state = State::Normal;
}
(b'm', State::Csi2(ref buf1, ref buf2)) => {
for b in &[buf1, buf2] {
match b.as_slice() {
b"0" => {
*fg_color = Color::Default;
*bg_color = Color::Default;
}
b"1" => { }
b"7" => {
let temp = *fg_color;
*fg_color = *bg_color;
*bg_color = temp;
}
b"27" => {
let temp = *fg_color;
*fg_color = *bg_color;
*bg_color = temp;
}
b"30" => *fg_color = Color::Black,
b"31" => *fg_color = Color::Red,
b"32" => *fg_color = Color::Green,
b"33" => *fg_color = Color::Yellow,
b"34" => *fg_color = Color::Blue,
b"35" => *fg_color = Color::Magenta,
b"36" => *fg_color = Color::Cyan,
b"37" => *fg_color = Color::White,
b"39" => *fg_color = Color::Default,
b"40" => *bg_color = Color::Black,
b"41" => *bg_color = Color::Red,
b"42" => *bg_color = Color::Green,
b"43" => *bg_color = Color::Yellow,
b"44" => *bg_color = Color::Blue,
b"45" => *bg_color = Color::Magenta,
b"46" => *bg_color = Color::Cyan,
b"47" => *bg_color = Color::White,
b"49" => *bg_color = Color::Default,
b"90" => *fg_color = Color::Black,
b"91" => *fg_color = Color::Red,
b"92" => *fg_color = Color::Green,
b"93" => *fg_color = Color::Yellow,
b"94" => *fg_color = Color::Blue,
b"95" => *fg_color = Color::Magenta,
b"96" => *fg_color = Color::Cyan,
b"97" => *fg_color = Color::White,
b"100" => *bg_color = Color::Black,
b"101" => *bg_color = Color::Red,
b"102" => *bg_color = Color::Green,
b"103" => *bg_color = Color::Yellow,
b"104" => *bg_color = Color::Blue,
b"105" => *bg_color = Color::Magenta,
b"106" => *bg_color = Color::Cyan,
b"107" => *bg_color = Color::White,
_ => {}
}
}
grid[cursor_val!()].set_fg(*fg_color);
grid[cursor_val!()].set_bg(*bg_color);
*state = State::Normal;
}
(c, State::Csi1(ref mut buf)) if (c >= b'0' && c <= b'9') || c == b' ' => {
buf.push(c);
}
(b';', State::Csi2(ref mut buf1_p, ref mut buf2_p)) => {
let buf1 = std::mem::replace(buf1_p, SmallVec::new());
let buf2 = std::mem::replace(buf2_p, SmallVec::new());
let buf3 = SmallVec::new();
*state = State::Csi3(buf1, buf2, buf3);
}
(b't', State::Csi2(_, _)) => {
*state = State::Normal;
}
(b'H', State::Csi2(_, _)) | (b'H', State::Csi) => {
let (orig_x, mut orig_y) = if let State::Csi2(ref y, ref x) = state {
(
unsafe { std::str::from_utf8_unchecked(x) }
.parse::<usize>()
.unwrap_or(1),
unsafe { std::str::from_utf8_unchecked(y) }
.parse::<usize>()
.unwrap_or(1),
)
} else {
(1, 1)
};
let (min_y, max_y) = if *origin_mode {
orig_y += scroll_region.top;
(scroll_region.top, scroll_region.bottom)
} else {
(0, terminal_size.1.saturating_sub(1))
};
cursor.0 = std::cmp::min(orig_x - 1, terminal_size.0.saturating_sub(1));
cursor.1 = std::cmp::max(min_y, std::cmp::min(max_y, orig_y - 1));
*wrap_next = false;
*state = State::Normal;
}
(c, State::Csi2(_, ref mut buf)) if c >= b'0' && c <= b'9' => {
buf.push(c);
}
(b'r', State::Csi2(_, _)) | (b'r', State::Csi) => {
let (top, bottom) = if let State::Csi2(ref top, ref bottom) = state {
(
unsafe { std::str::from_utf8_unchecked(top) }
.parse::<usize>()
.unwrap_or(1),
unsafe { std::str::from_utf8_unchecked(bottom) }
.parse::<usize>()
.unwrap_or(1),
)
} else {
(1, terminal_size.1)
};
if bottom > top {
scroll_region.top = top - 1;
scroll_region.bottom = bottom - 1;
*cursor = (0, 0);
*wrap_next = false;
}
*state = State::Normal;
}
(b't', State::Csi3(_, _, _)) => {
*state = State::Normal;
}
(c, State::Csi3(_, _, ref mut buf)) if c >= b'0' && c <= b'9' => {
buf.push(c);
}
(b'm', State::Csi3(ref buf1, ref buf2, ref buf3))
if buf1.as_ref() == b"38" && buf2.as_ref() == b"5" =>
{
*fg_color = if let Ok(byte) =
u8::from_str_radix(unsafe { std::str::from_utf8_unchecked(buf3) }, 10)
{
Color::Byte(byte)
} else {
Color::Default
};
grid[cursor_val!()].set_fg(*fg_color);
*state = State::Normal;
}
(b'm', State::Csi3(ref buf1, ref buf2, ref buf3))
if buf1.as_ref() == b"48" && buf2.as_ref() == b"5" =>
{
*bg_color = if let Ok(byte) =
u8::from_str_radix(unsafe { std::str::from_utf8_unchecked(buf3) }, 10)
{
Color::Byte(byte)
} else {
Color::Default
};
grid[cursor_val!()].set_bg(*bg_color);
*state = State::Normal;
}
(_, State::Csi) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::Csi1(_)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::Csi2(_, _)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::Csi3(_, _, _)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::Osc1(_)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::Osc2(_, _)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::CsiQ(_)) => {
debug!("ignoring unknown code {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
(_, State::G0) => {
debug!("ignoring {}", EscCode::from((&(*state), byte)));
*state = State::Normal;
}
}
}
}