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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! termfest is a thread-safe TUI library that provides simple APIs to render texts in terminal,
//! heavily inspired by [nsf/termbox-go](https://github.com/nsf/termbox-go).
//! Currently, termfest doesn't support windows because of my poor windows experience.
//!
//! termfest has internal buffer for efficient rendering.
//! Applications can render everything to the buffer every time, and termfest flushes the buffer
//! and renders only the difference between the terminal state and the buffer.
//!
//! ## Example
//!
//! ```no_run
//! use termfest::{Termfest, Event};
//! use termfest::attr::*;
//! use termfest::key::*;
//!
//! // first, initialize termfest.
//! let (fest, events) = Termfest::hold().unwrap();
//!
//! let mut y = 0;
//!
//! // events is a receiver of a channel that accepts terminal events like key input.
//! for ev in events.iter() {
//!     {
//!         // lock the screen.
//!         let mut screen = fest.lock_screen();
//!         // clear the buffer. you can render everything every time.
//!         // termfest can provide efficient rendering.
//!         screen.clear();
//!         // write to the buffer.
//!         let attr = Attribute { fg: Color::Red, ..Attribute::default() };
//!         screen.print(0, y, "Hello, world!", attr);
//!         // when the screen lock is released, the buffer is flushed.
//!         // (you can flush the buffer with explicit `flush` call.)
//!     }
//!     match ev {
//!         Event::Key(ESC) | Event::Char('q') => break,
//!         Event::Key(ArrowUp) => if y > 0 { y -= 1; },
//!         Event::Key(ArrowDown) => y += 1,
//!         _ => {}
//!     }
//! }
//! ```

#[macro_use]
extern crate bitflags;
extern crate libc;
extern crate num;
#[macro_use]
extern crate num_derive;
extern crate signal_notify;
extern crate term;
extern crate unicode_width;

use std::io::prelude::*;
use std::io::{self, BufWriter};
use std::fs::{File, OpenOptions};
use std::ops::Drop;
use std::sync::{mpsc, Arc, Mutex, MutexGuard};

use std::os::unix::io::{AsRawFd, RawFd};

use signal_notify::{notify, Signal};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

pub mod key;
mod event;
mod screen;
use screen::Screen;
mod terminal;
use terminal::Terminal;
pub mod attr;

use key::Key;
pub use event::Event;
pub use screen::Cell;
use attr::Attribute;

/// `Termfest` holds termfest states.
/// It is created by `Termfest::hold`.
/// When it is dropped, termfest finalizes and restores every terminal states.
pub struct Termfest {
    ttyout_fd: RawFd,
    ttyout: Mutex<BufWriter<File>>,
    orig_tios: libc::termios,

    terminal: Arc<Terminal>,
    /// `screen` should be guraded with `Mutex` because SIGWINCH watcher thread will modify width
    /// and height of `screen`
    screen: Arc<Mutex<Screen>>,
}

impl Termfest {
    /// `hold` initialize terminal state and termfest state.
    /// If succeeded, it returns a tuple of `Termfest` object and `Receiver<Event>`.
    /// When the returned `Termfest` object is dropped, the terminal state will be restored.
    ///
    /// ```no_run
    /// # fn main() -> Result<(), std::io::Error> {
    /// use termfest::Termfest;
    /// let (fest, events) = Termfest::hold()?;
    /// // do something widht fest and events.
    /// # Ok(())
    /// # }
    /// ```
    pub fn hold() -> Result<(Termfest, mpsc::Receiver<Event>), io::Error> {
        let mut ttyout = OpenOptions::new()
            .write(true)
            .read(false)
            .create(false)
            .open("/dev/tty")?;

        let orig_tios = setup_tios(ttyout.as_raw_fd())?;

        let terminal = Arc::new(Terminal::from_env()?);
        terminal.enter_ca(&mut ttyout)?;
        terminal.enter_keypad(&mut ttyout)?;
        terminal.clear(&mut ttyout)?;

        let (tx, rx) = mpsc::channel();

        spawn_ttyin_reader(tx.clone(), terminal.clone())?;

        let (width, height) = terminal::size(ttyout.as_raw_fd());
        let screen = Arc::new(Mutex::new(Screen::new(width, height)));
        {
            let ttyout_fd = ttyout.as_raw_fd();
            let screen = screen.clone();
            let tx = tx.clone();
            let sigwinch = notify(&[Signal::WINCH]);
            ::std::thread::spawn(move || loop {
                if sigwinch.recv().is_err() {
                    break;
                }
                let (w, h) = terminal::size(ttyout_fd);
                let mut screen = screen.lock().unwrap();
                screen.resize(w, h);
                if tx.send(Event::Resize {
                    width: w,
                    height: h,
                }).is_err()
                {
                    break;
                }
            });
        }

        let fest = Termfest {
            ttyout_fd: ttyout.as_raw_fd(),
            ttyout: Mutex::new(BufWriter::new(ttyout)),
            orig_tios: orig_tios,
            terminal: terminal,
            screen: screen,
        };
        Ok((fest, rx))
    }

    /// acquire the lock of screen, and returns `ScreenLock`.
    /// It will block if the lock is already acquired.
    pub fn lock_screen(&self) -> ScreenLock {
        ScreenLock {
            flushed: false,
            screen: self.screen.lock().unwrap(),
            ttyout: &self.ttyout,
            terminal: &self.terminal,
        }
    }
}

impl Drop for Termfest {
    fn drop(&mut self) {
        // ignore errors in drop
        if let Ok(mut ttyout) = self.ttyout.lock() {
            let _ = self.terminal.show_cursor(&mut *ttyout);
            let _ = self.terminal.exit_keypad(&mut *ttyout);
            let _ = self.terminal.exit_ca(&mut *ttyout);
            let _ = self.terminal.reset_attr(&mut *ttyout);
            unsafe {
                libc::tcsetattr(self.ttyout_fd, libc::TCSANOW, &self.orig_tios);
            }
        }
    }
}

/// `ScreenLock` is a locked screen buffer, created by `Termfest::lock_screen`.
/// When it is dropped, the buffered state will be flushed to the terminal.
/// All rendering manipulation is implemented in `ScreenLock`.
pub struct ScreenLock<'a> {
    flushed: bool,
    screen: MutexGuard<'a, Screen>,
    ttyout: &'a Mutex<BufWriter<File>>,
    terminal: &'a Terminal,
}

impl<'a> ScreenLock<'a> {
    /// flushes the internal buffer states to the terminal.
    /// Even if this function is not called, the buffer will be flushed when `self` is dropped.
    pub fn flush(&mut self) -> io::Result<()> {
        let mut ttyout = self.ttyout.lock().unwrap();
        for command in self.screen.flush_commands() {
            self.terminal.write(&mut *ttyout, command)?;
        }
        ttyout.flush()?;
        self.flushed = true;
        Ok(())
    }

    /// clear the internal buffer states.
    /// If clear and flush is called, the terminal will be cleared (nothing will be rendered).
    pub fn clear(&mut self) {
        self.screen.clear();
    }

    pub fn move_cursor(&mut self, x: usize, y: usize) {
        self.screen.cursor.x = x;
        self.screen.cursor.y = y;
    }

    pub fn hide_cursor(&mut self) {
        self.screen.cursor.visible = false;
    }

    pub fn show_cursor(&mut self) {
        self.screen.cursor.visible = true;
    }

    /// print string with the given attribute.
    /// It is equal to `put_cell` calls with each character.
    pub fn print(&mut self, x: usize, y: usize, s: &str, attr: Attribute) {
        self.screen.print(x, y, s, attr)
    }

    pub fn put_cell(&mut self, x: usize, y: usize, cell: Cell) {
        self.screen.put_cell(x, y, cell);
    }

    /// returns the width and height of the terminal.
    pub fn size(&self) -> (usize, usize) {
        self.screen.size()
    }
}

impl<'a> Drop for ScreenLock<'a> {
    fn drop(&mut self) {
        if self.flushed {
            return;
        }
        let _ = self.flush();
        self.flushed = true;
    }
}

fn setup_tios(fd: ::libc::c_int) -> io::Result<libc::termios> {
    unsafe {
        let mut orig_tios: libc::termios = ::std::mem::uninitialized();
        if libc::tcgetattr(fd, &mut orig_tios as *mut _) < 0 {
            return Err(io::Error::last_os_error());
        }
        let mut tios = orig_tios;
        tios.c_iflag &= !(libc::IGNBRK | libc::BRKINT | libc::PARMRK | libc::ISTRIP | libc::INLCR
            | libc::IGNCR | libc::ICRNL | libc::IXON);
        tios.c_lflag &= !(libc::ECHO | libc::ECHONL | libc::ICANON | libc::ISIG | libc::IEXTEN);
        tios.c_cflag &= !(libc::CSIZE | libc::PARENB);
        tios.c_cflag |= libc::CS8;
        tios.c_cc[libc::VMIN] = 1;
        tios.c_cc[libc::VTIME] = 0;
        if libc::tcsetattr(fd, libc::TCSANOW, &tios) < 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(orig_tios)
    }
}

fn spawn_ttyin_reader(tx: mpsc::Sender<Event>, term: Arc<Terminal>) -> io::Result<()> {
    let mut ttyin = OpenOptions::new()
        .write(false)
        .read(true)
        .create(false)
        .open("/dev/tty")?;
    unsafe {
        let r = libc::fcntl(
            ttyin.as_raw_fd(),
            libc::F_SETFL,
            libc::O_ASYNC | libc::O_NONBLOCK,
        );
        if r < 0 {
            return Err(io::Error::last_os_error());
        }
    }
    if cfg!(linux) {
        unsafe {
            let r = libc::fcntl(ttyin.as_raw_fd(), libc::F_SETOWN, libc::getpid());
            if r < 0 {
                return Err(io::Error::last_os_error());
            }
        }
    }
    let sigio = notify(&[Signal::IO]);
    ::std::thread::spawn(move || {
        let mut buf = Vec::new();
        for _ in sigio.iter() {
            let mut tmpbuf = [0; 64];
            match ttyin.read(&mut tmpbuf) {
                Ok(n) => buf.extend(&tmpbuf[..n]),
                Err(e) => match e.kind() {
                    io::ErrorKind::WouldBlock | io::ErrorKind::InvalidInput => continue,
                    _ => panic!("failed to read from tty: {}", e),
                },
            };
            let mut from = 0;
            loop {
                if let Some((read_byte, ev)) = event::parse(&buf[from..], &*term) {
                    from += read_byte;
                    if tx.send(ev).is_err() {
                        break;
                    }
                } else {
                    break;
                }
            }
            buf = buf[from..].to_vec();
        }
    });
    Ok(())
}

/// `DisplayWidth` provides a way to determine display width of characters or strings.
///
/// ```
/// use termfest::DisplayWidth;
///
/// assert_eq!('あ'.display_width(), 2);
/// assert_eq!('a'.display_width(), 1);
///
/// assert_eq!("abc".display_width(), 3);
/// assert_eq!("あいう".display_width(), 6);
/// ```
pub trait DisplayWidth {
    fn display_width(&self) -> usize;
}

impl DisplayWidth for char {
    fn display_width(&self) -> usize {
        self.width().unwrap_or(1)
    }
}

impl DisplayWidth for str {
    fn display_width(&self) -> usize {
        self.width()
    }
}