#[cfg(test)]
mod tests;
pub use crate::renderer::Optimizations;
use std::io::{self, Write};
use crate::ansi::mode;
use crate::buffer::{Bounded, Surface, SurfaceMut};
use crate::cell::Cell;
use crate::layout::{Position, Rect, Size};
use crate::renderer::{RenderBuffer, Renderer};
use crate::text::{TextSurface, WidthMode};
pub struct Screen<W: Write> {
writer: W,
front_buf: RenderBuffer,
renderer: Renderer,
out_buf: Vec<u8>,
width: u16,
height: u16,
eaw_wide: bool,
fullscreen: bool,
cursor_visible: bool,
sync_updates: bool,
grapheme_clusters: bool,
desired_cursor: Option<Position>,
}
impl<W: Write> Screen<W> {
pub fn new(writer: W, size: impl Into<Size>) -> Self {
let mut renderer = Renderer::new();
renderer.set_fullscreen(false);
renderer.set_relative_cursor(true);
let mut screen = Self {
writer,
front_buf: RenderBuffer::new(0, 0),
renderer,
out_buf: Vec::with_capacity(4096),
width: 0,
height: 0,
eaw_wide: false,
fullscreen: false,
cursor_visible: true,
sync_updates: false,
grapheme_clusters: false,
desired_cursor: None,
};
let size = size.into();
if size.width != 0 || size.height != 0 {
screen.resize(size);
}
screen
}
pub fn writer(&self) -> &W {
&self.writer
}
pub fn writer_mut(&mut self) -> &mut W {
&mut self.writer
}
pub fn into_writer(self) -> W {
self.writer
}
pub fn set_cell(&mut self, pos: impl Into<Position>, cell: &Cell) {
self.front_buf.set_cell(pos.into(), cell);
}
pub fn cell_mut(&mut self, pos: impl Into<Position>) -> Option<&mut Cell> {
self.front_buf.cell_mut(pos.into())
}
pub fn size(&self) -> Size {
Size::new(self.width, self.height)
}
pub fn render(&mut self) -> io::Result<()> {
let changed = self.renderer.sync_front(&mut self.front_buf);
if changed || self.cursor_move_pending() {
self.write_frame();
}
self.flush()
}
pub fn invalidate(&mut self) {
self.renderer.request_clear();
}
pub fn resize(&mut self, size: impl Into<Size>) {
let size = size.into();
if self.width == size.width && self.height == size.height {
return;
}
self.width = size.width;
self.height = size.height;
self.front_buf.resize(size.width, size.height);
self.renderer.request_clear();
}
pub fn insert_above(&mut self, content: &str) -> io::Result<()> {
if content.is_empty() {
return Ok(());
}
let width = self.width;
let height = self.height;
let y = self.renderer.cursor_position().y;
self.out_buf.write_all(b"\r").unwrap();
let down = height.saturating_sub(y).saturating_sub(1);
if down > 0 {
crate::ansi::cursor::write_cud(&mut self.out_buf, down).unwrap();
}
let lines: Vec<&str> = content.split('\n').collect();
let mut offset: u16 = lines.len() as u16;
let width_mode = self.width_mode();
for line in &lines {
let lw =
crate::ansi::text::string_width(line.as_bytes(), width_mode, self.eaw_wide) as u16;
if let Some(n) = lw.checked_div(width) {
offset = offset.saturating_add(n);
}
}
for _ in 0..offset {
self.out_buf.write_all(b"\n").unwrap();
}
let up = offset.saturating_add(height).saturating_sub(1);
if up > 0 {
crate::ansi::cursor::write_cuu(&mut self.out_buf, up).unwrap();
}
crate::ansi::screen::write_insert_lines(&mut self.out_buf, offset).unwrap();
for line in &lines {
self.out_buf.write_all(line.as_bytes()).unwrap();
self.out_buf
.write_all(crate::ansi::screen::ERASE_LINE_RIGHT)
.unwrap();
self.out_buf.write_all(b"\r\n").unwrap();
}
self.renderer.set_cursor_position(Position { y: 0, x: 0 });
self.flush()
}
pub fn move_cursor_to(&mut self, pos: impl Into<Position>) -> io::Result<()> {
self.stage_move_cursor_to(pos.into());
self.flush()
}
pub fn move_cursor_by(&mut self, dx: i16, dy: i16) -> io::Result<()> {
let cur = self.tracked_cursor().unwrap_or(Position::ORIGIN);
let x = cur.x.saturating_add_signed(dx);
let y = cur.y.saturating_add_signed(dy);
self.move_cursor_to((x, y))
}
pub fn set_cursor_position(&mut self, pos: impl Into<Position>) {
self.desired_cursor = Some(pos.into());
}
pub fn clear_cursor_position(&mut self) {
self.desired_cursor = None;
}
pub fn tracked_cursor(&self) -> Option<Position> {
self.renderer
.cursor_known()
.then(|| self.renderer.cursor_position())
}
pub fn invalidate_tracked_cursor(&mut self) {
self.renderer.invalidate_cursor();
}
pub fn set_tracked_cursor(&mut self, pos: impl Into<Position>) {
self.renderer.set_cursor_position(pos.into());
}
fn clamp_to_surface(&self, pos: Position) -> Position {
Position {
x: pos.x.min(self.width.saturating_sub(1)),
y: pos.y.min(self.height.saturating_sub(1)),
}
}
fn cursor_move_pending(&self) -> bool {
match self.desired_cursor {
Some(pos) => {
let pos = self.clamp_to_surface(pos);
!self.renderer.cursor_known() || self.renderer.cursor_position() != pos
}
None => false,
}
}
pub fn set_fullscreen(&mut self, fullscreen: bool) {
if self.fullscreen == fullscreen {
return;
}
self.fullscreen = fullscreen;
self.renderer.set_fullscreen(fullscreen);
self.renderer.set_relative_cursor(!fullscreen);
if fullscreen {
self.renderer.save_cursor();
} else {
self.renderer.restore_cursor();
}
self.renderer.request_clear();
}
pub fn fullscreen(&self) -> bool {
self.fullscreen
}
pub fn set_cursor_visible(&mut self, visible: bool) {
self.cursor_visible = visible;
}
pub fn cursor_visible(&self) -> bool {
self.cursor_visible
}
pub fn set_grapheme_clusters(&mut self, enabled: bool) {
if self.grapheme_clusters == enabled {
return;
}
self.grapheme_clusters = enabled;
self.invalidate();
}
pub fn grapheme_clusters(&self) -> bool {
self.grapheme_clusters
}
pub fn set_synchronized_output(&mut self, enabled: bool) {
self.sync_updates = enabled;
}
pub fn synchronized_output(&self) -> bool {
self.sync_updates
}
pub fn set_color_profile(&mut self, profile: crate::color::Profile) {
self.renderer.set_color_profile(profile);
}
pub fn color_profile(&self) -> crate::color::Profile {
self.renderer.color_profile()
}
pub fn set_optimizations(&mut self, optimizations: Optimizations) {
self.renderer.set_optimizations(optimizations);
}
pub fn optimizations(&self) -> Optimizations {
self.renderer.optimizations()
}
fn write_frame(&mut self) {
let bracket_cursor = self.cursor_visible && !self.sync_updates;
if self.sync_updates {
mode::Mode::SYNCHRONIZED_OUTPUT
.set(&mut self.out_buf)
.unwrap();
}
if bracket_cursor {
mode::Mode::CURSOR_VISIBLE.reset(&mut self.out_buf).unwrap();
}
self.renderer.render_back(&mut self.out_buf).unwrap();
if let Some(pos) = self.desired_cursor {
let pos = self.clamp_to_surface(pos);
self.renderer
.move_to(&mut self.out_buf, &self.front_buf, pos.y, pos.x)
.unwrap();
}
if bracket_cursor {
mode::Mode::CURSOR_VISIBLE.set(&mut self.out_buf).unwrap();
}
if self.sync_updates {
mode::Mode::SYNCHRONIZED_OUTPUT
.reset(&mut self.out_buf)
.unwrap();
}
}
pub(crate) fn stage_move_cursor_to(&mut self, target: Position) {
if self.renderer.cursor_known() && self.renderer.cursor_position() == target {
return;
}
self.renderer
.move_to(&mut self.out_buf, &self.front_buf, target.y, target.x)
.unwrap();
}
pub(crate) fn park_cursor(&mut self) -> io::Result<()> {
let (_, last_height) = self.renderer.last_size();
if last_height > 0 {
self.renderer
.move_to(&mut self.out_buf, &self.front_buf, last_height - 1, 0)?;
}
Ok(())
}
pub(crate) fn save_cursor(&mut self) {
self.renderer.save_cursor();
}
pub(crate) fn restore_cursor(&mut self) {
self.renderer.restore_cursor();
}
pub(crate) fn invalidate_cursor(&mut self) {
self.renderer.invalidate_cursor();
}
}
impl<W: Write> Write for Screen<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.out_buf.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
if !self.out_buf.is_empty() {
#[cfg(debug_assertions)]
crate::trace::tee_output(&self.out_buf);
self.writer.write_all(&self.out_buf)?;
self.out_buf.clear();
}
self.writer.flush()
}
}
impl<W: Write> Bounded for Screen<W> {
fn bounds(&self) -> Rect {
self.front_buf.bounds()
}
}
impl<W: Write> Surface for Screen<W> {
fn cell(&self, pos: Position) -> Option<&Cell> {
self.front_buf.cell(pos)
}
}
impl<W: Write> SurfaceMut for Screen<W> {
fn set_cell(&mut self, pos: Position, cell: &Cell) {
self.front_buf.set_cell(pos, cell);
}
fn cell_mut(&mut self, pos: Position) -> Option<&mut Cell> {
self.front_buf.cell_mut(pos)
}
fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
self.front_buf.insert_lines(y, n, bounds_bottom, fill);
}
fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
self.front_buf.delete_lines(y, n, bounds_bottom, fill);
}
fn insert_cells(&mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell) {
self.front_buf.insert_cells(pos, n, bounds_right, fill);
}
fn delete_cells(&mut self, pos: Position, n: u16, bounds_right: u16, fill: &Cell) {
self.front_buf.delete_cells(pos, n, bounds_right, fill);
}
}
impl<W: Write> TextSurface for Screen<W> {
fn width_mode(&self) -> WidthMode {
if self.grapheme_clusters {
WidthMode::Grapheme
} else {
WidthMode::Wc
}
}
fn eaw_wide(&self) -> bool {
self.eaw_wide
}
}