From c9e234a1ae7b631ca058f2331a3986630b5d1b64 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 13 Jun 2013 18:16:25 -0700 Subject: [PATCH] Tweak terminfo::parm::expand function signature Take a new struct Variables instead of two &mut [] vectors for static and dynamic variables. --- src/libextra/term.rs | 8 ++++---- src/libextra/terminfo/parm.rs | 35 +++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/libextra/term.rs b/src/libextra/term.rs index 455cc0b7450..f09c00ccce2 100644 --- a/src/libextra/term.rs +++ b/src/libextra/term.rs @@ -20,7 +20,7 @@ use core::os; use terminfo::*; use terminfo::searcher::open; use terminfo::parser::compiled::parse; -use terminfo::parm::{expand, Number}; +use terminfo::parm::{expand, Number, Variables}; // FIXME (#2807): Windows support. @@ -84,7 +84,7 @@ impl Terminal { pub fn fg(&self, color: u8) { if self.color_supported { 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() { self.out.write(s.get()); } else { @@ -95,7 +95,7 @@ impl Terminal { pub fn bg(&self, color: u8) { if self.color_supported { 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() { self.out.write(s.get()); } else { @@ -105,7 +105,7 @@ impl Terminal { } pub fn reset(&self) { 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() { self.out.write(s.get()); } else { diff --git a/src/libextra/terminfo/parm.rs b/src/libextra/terminfo/parm.rs index 4c6aea9ea43..c6bb4e60628 100644 --- a/src/libextra/terminfo/parm.rs +++ b/src/libextra/terminfo/parm.rs @@ -34,26 +34,37 @@ pub enum Param { 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 # Arguments * `cap` - string to expand * `params` - vector of params for %p1 etc - * `sta` - vector of params corresponding to static variables - * `dyn` - vector of params corresponding to stativ variables + * `vars` - Variables struct for %Pa etc - 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. */ -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> { assert!(cap.len() != 0, "expanding an empty capability makes no sense"); 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 i = 0; @@ -170,10 +181,10 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa SetVar => { if cur >= 'A' && cur <= 'Z' { let idx = (cur as u8) - ('A' as u8); - sta[idx] = stack.pop(); + vars.sta[idx] = stack.pop(); } else if cur >= 'a' && cur <= 'z' { let idx = (cur as u8) - ('a' as u8); - dyn[idx] = stack.pop(); + vars.dyn[idx] = stack.pop(); } else { 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 => { if cur >= 'A' && cur <= 'Z' { 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' { let idx = (cur as u8) - ('a' as u8); - stack.push(copy dyn[idx]); + stack.push(copy vars.dyn[idx]); } else { return Err(~"bad variable name in %g"); } @@ -222,6 +233,6 @@ mod test { #[test] fn test_basic_setabf() { 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()); } }