aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-03-21 01:14:23 -0500
committerJesse Luehrs <doy@tozt.net>2013-03-21 01:14:23 -0500
commit50db2cec5f96d2f26ab5363a1beb870ed6482abc (patch)
tree3e3668fcdf96ead4e06dbf176b6b2a17bd5175de /src
parent0896e60afd21c9c6724fb06c6328bb890c4f911d (diff)
downloadrust-term-50db2cec5f96d2f26ab5363a1beb870ed6482abc.tar.gz
rust-term-50db2cec5f96d2f26ab5363a1beb870ed6482abc.zip
turn this into an object
Diffstat (limited to 'src')
-rw-r--r--src/info.rs44
-rw-r--r--src/term.rs58
2 files changed, 64 insertions, 38 deletions
diff --git a/src/info.rs b/src/info.rs
index d785d1c..b4a5d60 100644
--- a/src/info.rs
+++ b/src/info.rs
@@ -1,55 +1,23 @@
use core::libc::{c_char,c_int};
-use util::guard;
pub fn init () {
unsafe { c::setupterm(ptr::null(), 1, ptr::null()) };
}
-pub fn clear () {
- write_escape("clear");
-}
-
-pub fn move (col: uint, row: uint) {
- if col == 0u && row == 0u {
- write_escape("home");
- }
- else {
- write_escape2("cup", row as int, col as int);
- }
-}
-
-pub fn cursor (enabled: bool) {
- if enabled {
- write_escape("civis");
- }
- else {
- write_escape("cnorm");
- }
-}
-
-pub fn with_alternate_screen<T> (body: &fn () -> T) -> T {
- do guard(|| { write_escape("rmcup") }) {
- write_escape("smcup");
- body()
- }
-}
-
-fn write_escape (name: &str) {
- let output = do str::as_c_str(name) |c_name| {
+pub fn escape (name: &str) -> ~str {
+ do str::as_c_str(name) |c_name| {
unsafe {
str::raw::from_c_str(tigetstr(c_name))
}
- };
- io::print(output);
+ }
}
-fn write_escape2 (name: &str, p1: int, p2: int) {
- let output = do str::as_c_str(name) |c_name| {
+pub fn escape2 (name: &str, p1: int, p2: int) -> ~str {
+ do str::as_c_str(name) |c_name| {
unsafe {
str::raw::from_c_str(tiparm2(tigetstr(c_name), p1, p2))
}
- };
- io::print(output);
+ }
}
unsafe fn tigetstr (name: *c_char) -> *c_char {
diff --git a/src/term.rs b/src/term.rs
index a2cd24f..faceb12 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -3,6 +3,64 @@
#[crate_type = "lib"];
pub use ios::{cooked,cbreak,raw,echo,size,isatty};
+use info::{init,escape,escape2};
+
+struct Writer {
+ priv cleanup: bool,
+ priv alternate: bool,
+}
+
+pub fn Writer (cleanup: bool) -> Writer {
+ init();
+ Writer { cleanup: cleanup, alternate: false }
+}
+
+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 (&mut self, enable: bool) {
+ if enable {
+ io::print(escape("smcup"));
+ self.alternate = true;
+ }
+ else {
+ io::print(escape("rmcup"));
+ self.alternate = false;
+ }
+ }
+}
+
+impl Drop for Writer {
+ fn finalize (&self) {
+ if self.cleanup {
+ if self.alternate {
+ io::print(escape("rmcup"));
+ }
+ io::print(escape("sgr0"));
+ io::print(escape("cnorm"));
+ }
+ }
+}
pub mod ios;
pub mod info;