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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::env;
use std::fmt::Display;
use std::fs;
use std::io;
use std::io::{BufRead, BufReader};
use std::os::unix::io::AsRawFd;
use std::str;

use crate::kb::Key;
use crate::term::Term;

pub use crate::common_term::*;

pub const DEFAULT_WIDTH: u16 = 80;

#[inline]
pub fn is_a_terminal(out: &Term) -> bool {
    unsafe { libc::isatty(out.as_raw_fd()) != 0 }
}

pub fn is_a_color_terminal(out: &Term) -> bool {
    if !is_a_terminal(out) {
        return false;
    }

    match env::var("TERM") {
        Ok(term) => term != "dumb",
        Err(_) => false,
    }
}

#[inline]
pub fn terminal_size() -> Option<(u16, u16)> {
    terminal_size::terminal_size().map(|x| ((x.1).0, (x.0).0))
}

pub fn read_secure() -> io::Result<String> {
    let f_tty;
    let fd = unsafe {
        if libc::isatty(libc::STDIN_FILENO) == 1 {
            f_tty = None;
            libc::STDIN_FILENO
        } else {
            let f = fs::File::open("/dev/tty")?;
            let fd = f.as_raw_fd();
            f_tty = Some(BufReader::new(f));
            fd
        }
    };

    let mut termios = termios::Termios::from_fd(fd)?;
    let original = termios;
    termios.c_lflag &= !termios::ECHO;
    termios::tcsetattr(fd, termios::TCSAFLUSH, &termios)?;
    let mut rv = String::new();

    let read_rv = if let Some(mut f) = f_tty {
        f.read_line(&mut rv)
    } else {
        io::stdin().read_line(&mut rv)
    };

    termios::tcsetattr(fd, termios::TCSAFLUSH, &original)?;

    read_rv.map(|_| {
        let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
        rv.truncate(len);
        rv
    })
}

pub fn read_single_key() -> io::Result<Key> {
    let tty_f;
    let fd = unsafe {
        if libc::isatty(libc::STDIN_FILENO) == 1 {
            libc::STDIN_FILENO
        } else {
            tty_f = fs::File::open("/dev/tty")?;
            tty_f.as_raw_fd()
        }
    };
    let mut buf = [0u8; 20];
    let mut termios = termios::Termios::from_fd(fd)?;
    let original = termios;
    termios::cfmakeraw(&mut termios);
    termios::tcsetattr(fd, termios::TCSADRAIN, &termios)?;
    let rv = unsafe {
        let read = libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, 1);
        if read < 0 {
            Err(io::Error::last_os_error())
        } else if buf[0] == b'\x1b' {
            // read 19 more bytes if the first byte was the ESC code
            let read = libc::read(fd, buf[1..].as_mut_ptr() as *mut libc::c_void, 19);
            if read < 0 {
                Err(io::Error::last_os_error())
            } else if buf[1] == b'\x03' {
                Err(io::Error::new(
                    io::ErrorKind::Interrupted,
                    "read interrupted",
                ))
            } else {
                Ok(key_from_escape_codes(&buf[..(read+1) as usize]))
            }
        } else if buf[0] & 224u8 == 192u8 { 
            // a two byte unicode character
            let read = libc::read(fd, buf[1..].as_mut_ptr() as *mut libc::c_void, 1);
            if read < 0 {
                Err(io::Error::last_os_error())
            } else if buf[1] == b'\x03' {
                Err(io::Error::new(
                    io::ErrorKind::Interrupted,
                    "read interrupted",
                ))
            } else {
                Ok(key_from_escape_codes(&buf[..2 as usize]))
            }
        } else if buf[0] & 240u8 == 224u8 { 
            // a three byte unicode character
            let read = libc::read(fd, buf[1..].as_mut_ptr() as *mut libc::c_void, 2);
            if read < 0 {
                Err(io::Error::last_os_error())
            } else if buf[1] == b'\x03' {
                Err(io::Error::new(
                    io::ErrorKind::Interrupted,
                    "read interrupted",
                ))
            } else {
                Ok(key_from_escape_codes(&buf[..3 as usize]))
            }
        } else if buf[0] & 248u8 == 240u8 { 
            // a four byte unicode character
            let read = libc::read(fd, buf[1..].as_mut_ptr() as *mut libc::c_void, 3);
            if read < 0 {
                Err(io::Error::last_os_error())
            } else if buf[1] == b'\x03' {
                Err(io::Error::new(
                    io::ErrorKind::Interrupted,
                    "read interrupted",
                ))
            } else {
                Ok(key_from_escape_codes(&buf[..4 as usize]))
            }
        } else if buf[0] == b'\x03' {
            Err(io::Error::new(
                io::ErrorKind::Interrupted,
                "read interrupted",
            ))
        } else {
            Ok(key_from_escape_codes(&buf[..read as usize]))
        }
    };
    termios::tcsetattr(fd, termios::TCSADRAIN, &original)?;

    // if the user hit ^C we want to signal SIGINT to outselves.
    if let Err(ref err) = rv {
        if err.kind() == io::ErrorKind::Interrupted {
            unsafe {
                libc::raise(libc::SIGINT);
            }
        }
    }

    rv
}

pub fn key_from_escape_codes(buf: &[u8]) -> Key {
    match buf {
        b"\x1b[D" => Key::ArrowLeft,
        b"\x1b[C" => Key::ArrowRight,
        b"\x1b[A" => Key::ArrowUp,
        b"\x1b[B" => Key::ArrowDown,
        b"\n" | b"\r" => Key::Enter,
        b"\x1b" => Key::Escape,
        b"\x7f" => Key::Backspace,
        b"\x1b[H" => Key::Home,
        b"\x1b[F" => Key::End,
        b"\t" => Key::Tab,
        b"\x1b[3~" => Key::Del,
        buf => {
            if let Ok(s) = str::from_utf8(buf) {
                if let Some(c) = s.chars().next() {
                    return Key::Char(c);
                }
            }
            Key::Unknown
        }
    }
}

#[cfg(not(target_os = "macos"))]
lazy_static::lazy_static! {
    static ref IS_LANG_UTF8: bool = {
        match std::env::var("LANG") {
            Ok(lang) => lang.to_uppercase().ends_with("UTF-8"),
            _ => false,
        }
    };
}

#[cfg(target_os = "macos")]
pub fn wants_emoji() -> bool {
    true
}

#[cfg(not(target_os = "macos"))]
pub fn wants_emoji() -> bool {
    *IS_LANG_UTF8
}

pub fn set_title<T: Display>(title: T) {
    print!("\x1b]0;{}\x07", title);
}