Skip to main content

uncurses/screen/
commit.rs

1use std::io::{self, Write};
2
3use super::Screen;
4use crate::layout::Position;
5
6impl<W: Write> Screen<W> {
7    /// Render and release the top `count` inline rows without erasing them.
8    ///
9    /// Retained rows keep their displayed contents and receive new coordinates.
10    /// Releasing every row starts an empty area on the next line.
11    /// Counts are clamped to the managed height. Fullscreen screens and zero
12    /// counts leave pending cells and output unchanged.
13    ///
14    /// A render error leaves the rows managed. The rows are released before
15    /// the final flush, even if it fails. Use [`Write::flush`] to retry output
16    /// without releasing more rows.
17    pub fn commit_rows(&mut self, count: u16) -> io::Result<()> {
18        let count = count.min(self.height);
19        if self.fullscreen || count == 0 {
20            return Ok(());
21        }
22        self.render()?;
23        if count == self.height {
24            self.stage_move_cursor_to(Position::new(0, self.height - 1));
25            self.out_buf.extend_from_slice(b"\r\n");
26            self.renderer
27                .set_cursor_position(Position::new(0, self.height));
28        } else if !self.renderer.cursor_known() || self.renderer.cursor_position().y < count {
29            self.stage_move_cursor_to(Position::new(0, count));
30        }
31        self.renderer.commit_rows(&mut self.front_buf, count);
32        self.height -= count;
33        if let Some(position) = &mut self.desired_cursor {
34            position.y = position.y.saturating_sub(count);
35        }
36        self.cursor_rested_at = None;
37        self.flush()
38    }
39}