aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-03-20 00:36:08 -0500
committerJesse Luehrs <doy@tozt.net>2013-03-20 00:36:08 -0500
commit8fc473d033a4c38f7c0024bd259ff54b9867d50d (patch)
tree945a6323eb4d457871c7e2e26fc2684cc26ddcaa
parent28eeecd93029db4b756844852e3608eba83bf455 (diff)
downloadrust-term-8fc473d033a4c38f7c0024bd259ff54b9867d50d.tar.gz
rust-term-8fc473d033a4c38f7c0024bd259ff54b9867d50d.zip
implement getting terminal size
-rw-r--r--src/ios.rs13
-rw-r--r--src/termios_wrapper.c9
-rw-r--r--test/termios3.rs3
3 files changed, 23 insertions, 2 deletions
diff --git a/src/ios.rs b/src/ios.rs
index 7a9c33f..5945f38 100644
--- a/src/ios.rs
+++ b/src/ios.rs
@@ -1,4 +1,4 @@
-use core::libc::c_int;
+use core::libc::{c_int,c_uint};
pub fn cooked () -> int {
unsafe { c::cooked() as int }
@@ -25,6 +25,15 @@ pub fn isatty() -> bool {
unsafe { c_isatty(0) as bool }
}
+pub fn size() -> (uint, uint) {
+ let rows: c_uint = 0;
+ let cols: c_uint = 0;
+ unsafe {
+ c::size(&rows, &cols)
+ }
+ (rows as uint, cols as uint)
+}
+
enum struct_termios {}
struct PreserveTermios {
@@ -50,6 +59,8 @@ extern mod c {
fn get() -> *struct_termios;
fn set(t: *struct_termios);
+
+ fn size(rows: *c_uint, cols: *c_uint);
}
extern {
diff --git a/src/termios_wrapper.c b/src/termios_wrapper.c
index a245322..097f470 100644
--- a/src/termios_wrapper.c
+++ b/src/termios_wrapper.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
+#include <sys/ioctl.h>
/* very simplistic, ignores a lot of the settings that i don't understand,
* patches welcome */
@@ -87,3 +88,11 @@ void set(struct termios *t)
tcsetattr(0, TCSANOW, t);
free(t);
}
+
+void size(unsigned int *rows, unsigned int *cols)
+{
+ struct winsize ws;
+ ioctl(0, TIOCGWINSZ, &ws);
+ *rows = ws.ws_row;
+ *cols = ws.ws_col;
+}
diff --git a/test/termios3.rs b/test/termios3.rs
index d051258..51f6bb2 100644
--- a/test/termios3.rs
+++ b/test/termios3.rs
@@ -2,7 +2,8 @@ extern mod term;
fn main () {
if term::ios::isatty() {
- io::println("tty");
+ let (rows, cols) = term::ios::size();
+ io::println(fmt!("tty: %d %d", rows as int, cols as int));
}
else {
io::println("not tty");