Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ appveyor = { repository = "Stebalien/term" }
winapi = "0.2"
kernel32-sys = "0.2"

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[features]
default=[]
13 changes: 13 additions & 0 deletions examples/kitchen_sink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// An example of the avaiable functionality in term

extern crate term;

fn main() {
let mut t = term::stdout().unwrap();

print!("Dims: ");
t.fg(term::color::GREEN).unwrap();
print!("{:?}", t.dims().unwrap());
t.reset().unwrap();
println!();
}
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
//!
//! and this to your crate root:
//!
//! ```rust
//! ```
//! # #[allow(unused_extern_crates)]
//! extern crate term;
//! ```
//!
Expand Down Expand Up @@ -75,6 +76,8 @@ pub mod terminfo;

#[cfg(windows)]
mod win;
#[cfg(unix)]
mod unix;

/// Alias for stdout terminals.
pub type StdoutTerminal = Terminal<Output = Stdout> + Send;
Expand Down Expand Up @@ -168,6 +171,20 @@ pub enum Attr {
BackgroundColor(color::Color),
}

/// A struct representing the number of columns and rows of the terminal, and, if available, the
/// width and height of the terminal in pixels
#[derive(Debug, PartialEq, Hash, Eq, Copy, Clone, Default)]
pub struct Dims {
/// The number of rows in the terminal
pub rows: u16,
/// The number of columns in the terminal
pub columns: u16,
/// If available, the pixel width of the terminal
pub pixel_width: Option<u32>,
/// If available, the pixel height of the terminal
pub pixel_height: Option<u32>,
}

/// An error arising from interacting with the terminal.
#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -390,6 +407,9 @@ pub trait Terminal: Write {
/// Returns `Ok(true)` if the deletion code was printed, or `Err(e)` if there was an error.
fn carriage_return(&mut self) -> Result<()>;

/// Gets the size of the terminal window, if available
fn dims(&self) -> Result<Dims>;

/// Gets an immutable reference to the stream inside
fn get_ref(&self) -> &Self::Output;

Expand Down
91 changes: 91 additions & 0 deletions src/terminfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::io::BufReader;
use std::path::Path;

use Attr;
use Dims;
use color;
use Terminal;
use Result;
Expand All @@ -27,6 +28,11 @@ use self::parser::compiled::parse;
use self::parm::{expand, Variables, Param};
use self::Error::*;

#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(unix)]
use unix::win_size;


/// Returns true if the named terminal supports basic ANSI escape codes.
fn is_ansi(name: &str) -> bool {
Expand Down Expand Up @@ -283,6 +289,87 @@ pub struct TerminfoTerminal<T> {
ti: TermInfo,
}

#[cfg(unix)]
impl<T: Write + AsRawFd> Terminal for TerminfoTerminal<T> {
type Output = T;
fn fg(&mut self, color: color::Color) -> Result<()> {
let color = self.dim_if_necessary(color);
if self.num_colors > color {
return self.ti.apply_cap("setaf", &[Param::Number(color as i32)], &mut self.out);
}
Err(::Error::ColorOutOfRange)
}

fn bg(&mut self, color: color::Color) -> Result<()> {
let color = self.dim_if_necessary(color);
if self.num_colors > color {
return self.ti.apply_cap("setab", &[Param::Number(color as i32)], &mut self.out);
}
Err(::Error::ColorOutOfRange)
}

fn attr(&mut self, attr: Attr) -> Result<()> {
match attr {
Attr::ForegroundColor(c) => self.fg(c),
Attr::BackgroundColor(c) => self.bg(c),
_ => self.ti.apply_cap(cap_for_attr(attr), &[], &mut self.out),
}
}

fn supports_attr(&self, attr: Attr) -> bool {
match attr {
Attr::ForegroundColor(_) | Attr::BackgroundColor(_) => self.num_colors > 0,
_ => {
let cap = cap_for_attr(attr);
self.ti.strings.get(cap).is_some()
}
}
}

fn reset(&mut self) -> Result<()> {
self.ti.reset(&mut self.out)
}

fn supports_reset(&self) -> bool {
["sgr0", "sgr", "op"].iter().any(|&cap| self.ti.strings.get(cap).is_some())
}

fn supports_color(&self) -> bool {
self.num_colors > 0 && self.supports_reset()
}

fn cursor_up(&mut self) -> Result<()> {
self.ti.apply_cap("cuu1", &[], &mut self.out)
}

fn delete_line(&mut self) -> Result<()> {
self.ti.apply_cap("el", &[], &mut self.out)
}

fn carriage_return(&mut self) -> Result<()> {
self.ti.apply_cap("cr", &[], &mut self.out)
}

fn dims(&self) -> Result<Dims> {
win_size(self.out.as_raw_fd()).map(|s| s.into())
}

fn get_ref(&self) -> &T {
&self.out
}

fn get_mut(&mut self) -> &mut T {
&mut self.out
}

fn into_inner(self) -> T
where Self: Sized
{
self.out
}
}

#[cfg(windows)]
impl<T: Write> Terminal for TerminfoTerminal<T> {
type Output = T;
fn fg(&mut self, color: color::Color) -> Result<()> {
Expand Down Expand Up @@ -343,6 +430,10 @@ impl<T: Write> Terminal for TerminfoTerminal<T> {
self.ti.apply_cap("cr", &[], &mut self.out)
}

fn dims(&self) -> Result<Dims> {
Err(::Error::NotSupported)
}

fn get_ref(&self) -> &T {
&self.out
}
Expand Down
32 changes: 32 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extern crate libc;

use std::os::unix::io::RawFd;
use std::io;
use std::mem;
use Dims;
use Result;

/// Get the window size from a file descriptor (0 for stdout)
///
/// Option is returned as there is no distinction between errors (all ENOSYS)
pub fn win_size(fd: RawFd) -> Result<libc::winsize> {
unsafe {
let ws: libc::winsize = mem::uninitialized();
if libc::ioctl(fd, libc::TIOCGWINSZ, &ws) == 0 {
Ok(ws)
} else {
Err(io::Error::last_os_error().into())
}
}
}

impl From<libc::winsize> for Dims {
fn from(sz: libc::winsize) -> Dims {
Dims {
rows: sz.ws_row as u16,
columns: sz.ws_col as u16,
pixel_width: Some(sz.ws_xpixel as u32),
pixel_height: Some(sz.ws_ypixel as u32),
}
}
}
17 changes: 17 additions & 0 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use Error;
use Result;
use Terminal;
use color;
use Dims;

/// A Terminal implementation which uses the Win32 Console API.
pub struct WinConsole<T> {
Expand Down Expand Up @@ -285,6 +286,22 @@ impl<T: Write + Send> Terminal for WinConsole<T> {
}
}

fn dims(&self) -> Result<Dims> {
let handle = try!(conout());
unsafe {
let mut buffer_info = ::std::mem::uninitialized();
if kernel32::GetConsoleScreenBufferInfo(handle, &mut buffer_info) != 0 {
Ok(Dims {
rows: (buffer_info.srWindow.Bottom - buffer_info.srWindow.Top + 1) as u16,
columns: (buffer_info.srWindow.Right - buffer_info.srWindow.Left + 1) as u16,
..Default::default()
})
} else {
Err(io::Error::last_os_error().into())
}
}
}

fn get_ref<'a>(&'a self) -> &'a T {
&self.buf
}
Expand Down
10 changes: 10 additions & 0 deletions tests/winsize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate term;

#[cfg(unix)]
#[test]
fn test_winsize() {
let t = term::stdout().unwrap();
// This makes sure we don't try to provide dims on an incorrect platform, it also may trigger
// any memory errors.
let _dims = t.dims().unwrap();
}