aboutsummaryrefslogtreecommitdiffstats
path: root/src/term.rs
blob: 27fc8c973c66e7736130c733b37b91525c8038de (plain) (blame)
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
#[link(name = "term", vers = "0.0.1", author = "doy")];

#[crate_type = "lib"];

use core::libc::c_int;

pub use ios::{cooked,cbreak,raw,echo,size};
use info::{init,escape,escape2};

struct Writer {
    priv cleanup: bool,
}

pub fn Writer (cleanup: bool) -> Writer {
    init();
    Writer { cleanup: cleanup }
}

impl Writer {
    pub fn clear (&self) {
        io::print(escape("clear"));
    }

    pub fn move (&self, col: uint, row: uint) {
        if col == 0u && row == 0u {
            io::print(escape("home"));
        }
        else {
            io::print(escape2("cup", row as int, col as int));
        }
    }

    pub fn cursor (&self, enabled: bool) {
        if enabled {
            io::print(escape("civis"));
        }
        else {
            io::print(escape("cnorm"));
        }
    }

    pub fn alternate_screen (&self, enable: bool) {
        if enable {
            io::print(escape("smcup"));
        }
        else {
            io::print(escape("rmcup"));
        }
    }
}

impl Drop for Writer {
    fn finalize (&self) {
        if self.cleanup {
            io::print(escape("rmcup"));
            io::print(escape("sgr0"));
            io::print(escape("cnorm"));
        }
    }
}

pub fn isatty() -> bool {
    unsafe { c_isatty(0) as bool }
}

pub mod ios;
pub mod info;
mod util;

extern {
    #[link_name = "isatty"]
    fn c_isatty(fd: c_int) -> c_int;
}