-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement getting terminal size for windows and linux. #77
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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!(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ | |
//! | ||
//! and this to your crate root: | ||
//! | ||
//! ```rust | ||
//! ``` | ||
//! # #[allow(unused_extern_crates)] | ||
//! extern crate term; | ||
//! ``` | ||
//! | ||
|
@@ -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; | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind just calling this Dimensions (or Size)? |
||
/// 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 { | ||
|
@@ -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>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I'd call this either |
||
|
||
/// Gets an immutable reference to the stream inside | ||
fn get_ref(&self) -> &Self::Output; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ use std::io::BufReader; | |
use std::path::Path; | ||
|
||
use Attr; | ||
use Dims; | ||
use color; | ||
use Terminal; | ||
use Result; | ||
|
@@ -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 { | ||
|
@@ -283,6 +289,87 @@ pub struct TerminfoTerminal<T> { | |
ti: TermInfo, | ||
} | ||
|
||
#[cfg(unix)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't actually have to duplicate this entire implementation; you can just |
||
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<()> { | ||
|
@@ -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 | ||
} | ||
|
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to implement this? I can't see users actually needing to call this outside of term. I'd just do this conversion directly (i.e., implement a function or inline the conversion) instead of implementing |
||
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), | ||
} | ||
} | ||
} |
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(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not entirely accurate... (to me, this implies that this is all available functionality).