Skip to content

Commit 45d24a5

Browse files
committed
auto merge of #11031 : jhasse/rust/patch-msys-3, r=cmr
Enable ANSI colors if TERM is set to cygwin and terminfo is not available (msys terminal on Windows). See #2807
2 parents 9d1de0b + 5cf2f54 commit 45d24a5

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

src/libextra/term.rs

+13-45
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
use std::io::{Decorator, Writer};
1717

18-
#[cfg(not(target_os = "win32"))] use std::os;
19-
#[cfg(not(target_os = "win32"))] use terminfo::*;
20-
#[cfg(not(target_os = "win32"))] use terminfo::searcher::open;
21-
#[cfg(not(target_os = "win32"))] use terminfo::parser::compiled::parse;
22-
#[cfg(not(target_os = "win32"))] use terminfo::parm::{expand, Number, Variables};
18+
use std::os;
19+
use terminfo::*;
20+
use terminfo::searcher::open;
21+
use terminfo::parser::compiled::{parse, msys_terminfo};
22+
use terminfo::parm::{expand, Number, Variables};
2323

2424
// FIXME (#2807): Windows support.
2525

@@ -74,7 +74,6 @@ pub mod attr {
7474
}
7575
}
7676

77-
#[cfg(not(target_os = "win32"))]
7877
fn cap_for_attr(attr: attr::Attr) -> &'static str {
7978
match attr {
8079
attr::Bold => "bold",
@@ -93,29 +92,24 @@ fn cap_for_attr(attr: attr::Attr) -> &'static str {
9392
}
9493
}
9594

96-
#[cfg(not(target_os = "win32"))]
9795
pub struct Terminal<T> {
9896
priv num_colors: u16,
9997
priv out: T,
10098
priv ti: ~TermInfo
10199
}
102100

103-
#[cfg(target_os = "win32")]
104-
pub struct Terminal<T> {
105-
priv num_colors: u16,
106-
priv out: T,
107-
}
108-
109-
#[cfg(not(target_os = "win32"))]
110101
impl<T: Writer> Terminal<T> {
111102
pub fn new(out: T) -> Result<Terminal<T>, ~str> {
112-
let term = os::getenv("TERM");
113-
if term.is_none() {
114-
return Err(~"TERM environment variable undefined");
115-
}
103+
let term = match os::getenv("TERM") {
104+
Some(t) => t,
105+
None => return Err(~"TERM environment variable undefined")
106+
};
116107

117-
let entry = open(term.unwrap());
108+
let entry = open(term);
118109
if entry.is_err() {
110+
if "cygwin" == term { // msys terminal
111+
return Ok(Terminal {out: out, ti: msys_terminfo(), num_colors: 8});
112+
}
119113
return Err(entry.unwrap_err());
120114
}
121115

@@ -241,32 +235,6 @@ impl<T: Writer> Terminal<T> {
241235
}
242236
}
243237

244-
#[cfg(target_os = "win32")]
245-
impl<T: Writer> Terminal<T> {
246-
pub fn new(out: T) -> Result<Terminal<T>, ~str> {
247-
return Ok(Terminal {out: out, num_colors: 0});
248-
}
249-
250-
pub fn fg(&mut self, _color: color::Color) -> bool {
251-
false
252-
}
253-
254-
pub fn bg(&mut self, _color: color::Color) -> bool {
255-
false
256-
}
257-
258-
pub fn attr(&mut self, _attr: attr::Attr) -> bool {
259-
false
260-
}
261-
262-
pub fn supports_attr(&self, _attr: attr::Attr) -> bool {
263-
false
264-
}
265-
266-
pub fn reset(&self) {
267-
}
268-
}
269-
270238
impl<T: Writer> Decorator<T> for Terminal<T> {
271239
fn inner(self) -> T {
272240
self.out

src/libextra/terminfo/parser/compiled.rs

+15
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@ pub fn parse(file: &mut io::Reader,
316316
Ok(~TermInfo {names: term_names, bools: bools_map, numbers: numbers_map, strings: string_map })
317317
}
318318

319+
/// Create a dummy TermInfo struct for msys terminals
320+
pub fn msys_terminfo() -> ~TermInfo {
321+
let mut strings = HashMap::new();
322+
strings.insert(~"sgr0", bytes!("\x1b[0m").to_owned());
323+
strings.insert(~"bold", bytes!("\x1b[1m;").to_owned());
324+
strings.insert(~"setaf", bytes!("\x1b[3%p1%dm").to_owned());
325+
strings.insert(~"setab", bytes!("\x1b[4%p1%dm").to_owned());
326+
~TermInfo {
327+
names: ~[~"cygwin"], // msys is a fork of an older cygwin version
328+
bools: HashMap::new(),
329+
numbers: HashMap::new(),
330+
strings: strings
331+
}
332+
}
333+
319334
#[cfg(test)]
320335
mod test {
321336
use super::*;

0 commit comments

Comments
 (0)