Tweak terminfo::parm::expand function signature

Take a new struct Variables instead of two &mut [] vectors for static
and dynamic variables.
This commit is contained in:
Kevin Ballard 2013-06-13 18:16:25 -07:00
parent 9f9e505405
commit c9e234a1ae
2 changed files with 27 additions and 16 deletions

View File

@ -20,7 +20,7 @@ use core::os;
use terminfo::*; use terminfo::*;
use terminfo::searcher::open; use terminfo::searcher::open;
use terminfo::parser::compiled::parse; use terminfo::parser::compiled::parse;
use terminfo::parm::{expand, Number}; use terminfo::parm::{expand, Number, Variables};
// FIXME (#2807): Windows support. // FIXME (#2807): Windows support.
@ -84,7 +84,7 @@ impl Terminal {
pub fn fg(&self, color: u8) { pub fn fg(&self, color: u8) {
if self.color_supported { if self.color_supported {
let s = expand(*self.ti.strings.find_equiv(&("setaf")).unwrap(), let s = expand(*self.ti.strings.find_equiv(&("setaf")).unwrap(),
[Number(color as int)], [], []); [Number(color as int)], &mut Variables::new());
if s.is_ok() { if s.is_ok() {
self.out.write(s.get()); self.out.write(s.get());
} else { } else {
@ -95,7 +95,7 @@ impl Terminal {
pub fn bg(&self, color: u8) { pub fn bg(&self, color: u8) {
if self.color_supported { if self.color_supported {
let s = expand(*self.ti.strings.find_equiv(&("setab")).unwrap(), let s = expand(*self.ti.strings.find_equiv(&("setab")).unwrap(),
[Number(color as int)], [], []); [Number(color as int)], &mut Variables::new());
if s.is_ok() { if s.is_ok() {
self.out.write(s.get()); self.out.write(s.get());
} else { } else {
@ -105,7 +105,7 @@ impl Terminal {
} }
pub fn reset(&self) { pub fn reset(&self) {
if self.color_supported { if self.color_supported {
let s = expand(*self.ti.strings.find_equiv(&("op")).unwrap(), [], [], []); let s = expand(*self.ti.strings.find_equiv(&("op")).unwrap(), [], &mut Variables::new());
if s.is_ok() { if s.is_ok() {
self.out.write(s.get()); self.out.write(s.get());
} else { } else {

View File

@ -34,26 +34,37 @@ pub enum Param {
Number(int) Number(int)
} }
/// Container for static and dynamic variable arrays
pub struct Variables {
/// Static variables A-Z
sta: [Param, ..26],
/// Dynamic variables a-z
dyn: [Param, ..26]
}
impl Variables {
/// Return a new zero-initialized Variables
pub fn new() -> Variables {
Variables{ sta: [Number(0), ..26], dyn: [Number(0), ..26] }
}
}
/** /**
Expand a parameterized capability Expand a parameterized capability
# Arguments # Arguments
* `cap` - string to expand * `cap` - string to expand
* `params` - vector of params for %p1 etc * `params` - vector of params for %p1 etc
* `sta` - vector of params corresponding to static variables * `vars` - Variables struct for %Pa etc
* `dyn` - vector of params corresponding to stativ variables
To be compatible with ncurses, `sta` and `dyn` should be the same between calls to `expand` for To be compatible with ncurses, `vars` should be the same between calls to `expand` for
multiple capabilities for the same terminal. multiple capabilities for the same terminal.
*/ */
pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Param]) pub fn expand(cap: &[u8], params: &mut [Param], vars: &mut Variables)
-> Result<~[u8], ~str> { -> Result<~[u8], ~str> {
assert!(cap.len() != 0, "expanding an empty capability makes no sense"); assert!(cap.len() != 0, "expanding an empty capability makes no sense");
assert!(params.len() <= 9, "only 9 parameters are supported by capability strings"); assert!(params.len() <= 9, "only 9 parameters are supported by capability strings");
assert!(sta.len() <= 26, "only 26 static vars are able to be used by capability strings");
assert!(dyn.len() <= 26, "only 26 dynamic vars are able to be used by capability strings");
let mut state = Nothing; let mut state = Nothing;
let mut i = 0; let mut i = 0;
@ -170,10 +181,10 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa
SetVar => { SetVar => {
if cur >= 'A' && cur <= 'Z' { if cur >= 'A' && cur <= 'Z' {
let idx = (cur as u8) - ('A' as u8); let idx = (cur as u8) - ('A' as u8);
sta[idx] = stack.pop(); vars.sta[idx] = stack.pop();
} else if cur >= 'a' && cur <= 'z' { } else if cur >= 'a' && cur <= 'z' {
let idx = (cur as u8) - ('a' as u8); let idx = (cur as u8) - ('a' as u8);
dyn[idx] = stack.pop(); vars.dyn[idx] = stack.pop();
} else { } else {
return Err(~"bad variable name in %P"); return Err(~"bad variable name in %P");
} }
@ -181,10 +192,10 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa
GetVar => { GetVar => {
if cur >= 'A' && cur <= 'Z' { if cur >= 'A' && cur <= 'Z' {
let idx = (cur as u8) - ('A' as u8); let idx = (cur as u8) - ('A' as u8);
stack.push(copy sta[idx]); stack.push(copy vars.sta[idx]);
} else if cur >= 'a' && cur <= 'z' { } else if cur >= 'a' && cur <= 'z' {
let idx = (cur as u8) - ('a' as u8); let idx = (cur as u8) - ('a' as u8);
stack.push(copy dyn[idx]); stack.push(copy vars.dyn[idx]);
} else { } else {
return Err(~"bad variable name in %g"); return Err(~"bad variable name in %g");
} }
@ -222,6 +233,6 @@ mod test {
#[test] #[test]
fn test_basic_setabf() { fn test_basic_setabf() {
let s = bytes!("\\E[48;5;%p1%dm"); let s = bytes!("\\E[48;5;%p1%dm");
assert_eq!(expand(s, [Number(1)], [], []).unwrap(), bytes!("\\E[48;5;1m").to_owned()); assert_eq!(expand(s, [Number(1)], &mut Variables::new()).unwrap(), bytes!("\\E[48;5;1m").to_owned());
} }
} }