papyrus 0.17.2

A rust repl and script runner
use super::*;

impl<S> Output<S> {
    /// Full buffer, includes input buffer.
    pub fn buffer(&self) -> &str {
        &self.buf
    }

    /// Get the contents of the line at index `idx`.
    pub fn line(&self, idx: usize) -> Option<&str> {
        use std::cmp::Ordering::*;

        let lines_len = self.lines_pos.len();

        let end = match idx.cmp(&lines_len) {
            Less => self.lines_pos.get(idx).cloned(),
            Equal => Some(self.buf.len()),
            Greater => None,
        };

        end.map(|end| {
            let start = if idx == 0 {
                0
            } else {
                self.lines_pos.get(idx - 1).unwrap() + 1
            };

            &self.buf[start..end]
        })
    }

    /// The number of lines. Always >= 1 as the first line, even if empty, counts.
    pub fn lines_len(&self) -> usize {
        self.lines_pos.len() + 1
    }

    /// Create a channel to listen to line change events.
    pub fn listen(&mut self) -> channel::Receiver<OutputChange> {
        let (tx, rx) = channel::unbounded();

        self.tx = Some(tx);

        rx
    }

    /// Close the sending channel.
    pub fn close(&mut self) {
        self.tx = None;
    }
}

impl Default for Output<Read> {
    fn default() -> Self {
        Self::new()
    }
}