1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
extern crate crossterm;
extern crate futures;
extern crate unicode_segmentation;
extern crate unicode_width;

use std::{
    io::{stdout, Write},
    pin::Pin,
    task::{Context, Poll},
};

use crossterm::{
    event::{Event, EventStream},
    terminal::{disable_raw_mode, enable_raw_mode},
};
use futures::{future::poll_fn, Stream};
use unicode_segmentation::UnicodeSegmentation;

pub use crossterm::Result;

pub struct ReadLines {
    stream: EventStream,
    buffer: String,
}

impl ReadLines {
    pub fn new() -> Result<Self> {
        enable_raw_mode()?;
        Ok(Self {
            stream: EventStream::new(),
            buffer: String::new(),
        })
    }

    pub async fn next(&mut self) -> Result<String> {
        poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
    }

    pub fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<String>> {
        let res = Stream::poll_next(self, cx);
        match res {
            Poll::Pending => Poll::Pending,
            Poll::Ready(None) => Poll::Ready(Err(crossterm::ErrorKind::IoError(
                std::io::ErrorKind::UnexpectedEof.into(),
            ))),
            Poll::Ready(Some(res)) => Poll::Ready(res),
        }
    }
}

impl Drop for ReadLines {
    fn drop(&mut self) {
        disable_raw_mode().ok();
    }
}

fn print_flush<S: AsRef<str>>(string: S) -> std::io::Result<()> {
    let mut stdout = stdout();
    stdout.write(string.as_ref().as_bytes())?;
    stdout.flush()
}

macro_rules! print_flush {
    ($($arg:tt)*) => (print_flush(format!($($arg)*)).unwrap());
}

fn delete_string<S: AsRef<str>>(string: S) -> std::io::Result<()> {
    let len = unicode_width::UnicodeWidthStr::width(string.as_ref());
    let bs = "\x08".repeat(len);
    let sp = " ".repeat(len);
    print_flush(format!("{}{}{}", bs, sp, bs))
}

fn pop_grapheme(s: &mut String) -> Option<String> {
    match s.graphemes(true).last().map(std::borrow::ToOwned::to_owned) {
        None => None,
        Some(g) => {
            s.replace_range((s.len() - g.len()).., "");
            Some(g)
        }
    }
}

impl Stream for ReadLines {
    type Item = Result<String>;

    fn poll_next(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<<Self as futures::Stream>::Item>> {
        let this = Pin::get_mut(self);

        loop {
            match Pin::new(&mut this.stream).poll_next(cx) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(None) => {
                    if this.buffer.is_empty() {
                        return Poll::Ready(None);
                    } else {
                        let res = this.buffer.clone();
                        this.buffer.clear();
                        return Poll::Ready(Some(Ok(res)));
                    }
                }
                Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))),
                Poll::Ready(Some(Ok(Event::Key(key)))) => match key.code {
                    crossterm::event::KeyCode::Backspace => {
                        pop_grapheme(&mut this.buffer).map(|x| delete_string(x).unwrap());
                    }
                    crossterm::event::KeyCode::Enter => {
                        let res = this.buffer.clone();
                        this.buffer.clear();
                        print_flush!("\r\n");
                        return Poll::Ready(Some(Ok(res)));
                    }
                    crossterm::event::KeyCode::Tab => {
                        this.buffer.push('\t');
                        print_flush!("\t");
                    }
                    crossterm::event::KeyCode::Char(c) => {
                        this.buffer.push(c);
                        print_flush!("{}", c);
                    }
                    _ => {}
                },
                _ => {}
            }
        }
    }
}