summary refs log tree commit diff
path: root/src/terminal.rs
blob: 041a8841f2e5dc522f9294a040ba1c8c853fd174 (plain)
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
use crate::terminal::decoding::CharBufReader;
use crate::terminal::error::TerminalError;

use nix::sys::termios;
use std::io::Read;
use std::os::unix::io::AsRawFd;

pub mod decoding;
pub mod error;


pub fn handle_input_terminal(input_stream: impl Read + AsRawFd)
  -> std::result::Result<(), TerminalError>
{
  let fd = input_stream.as_raw_fd();
  let mut termios = termios::tcgetattr(fd)
      .map_err(error::mode_setting)?;
  termios.local_flags.insert(termios::LocalFlags::ECHO);
  termios.local_flags.remove(termios::LocalFlags::ECHOCTL);
  termios.local_flags.remove(termios::LocalFlags::ICANON);
  termios.control_chars[termios::SpecialCharacterIndices::VMIN as usize] = 1;
  termios.control_chars[termios::SpecialCharacterIndices::VTIME as usize] = 0;
  termios::tcsetattr(fd, termios::SetArg::TCSANOW, &termios)
      .map_err(error::mode_setting)?;

  handle_input(input_stream)
}


pub fn handle_input(input_stream: impl Read)
  -> std::result::Result<(), TerminalError>
{
  let mut reader = CharBufReader::new(input_stream);

  let string = reader.fill_buf().map_err(error::input)?;
  println!("top level {:?}", string);

  let n_to_consume = string.len();
  reader.consume(n_to_consume);

  Ok(())
}


#[cfg(test)]
mod tests {
  use super::*;
  use std::io::Cursor;

  #[test]
  fn test_empty_input() {
    let buffer = Cursor::new(vec![]);
    let result = handle_input(buffer);
    assert!(result.is_ok());
  }
}