Skip to content

Commit 87d8465

Browse files
committed
---
yaml --- r: 146799 b: refs/heads/try2 c: 2431ac3 h: refs/heads/master i: 146797: f416284 146795: 4c58140 146791: c28ccc0 146783: f15cb6a v: v3
1 parent 36368a7 commit 87d8465

File tree

9 files changed

+166
-154
lines changed

9 files changed

+166
-154
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 861cced1193b13be87c6098cca93264e15cac351
8+
refs/heads/try2: 2431ac3080859e158f2a7379f939c40f27081ea7
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libextra/term.rs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[allow(missing_doc)];
1414

1515

16-
use std::io;
16+
use std::io::{Decorator, Writer};
1717

1818
#[cfg(not(target_os = "win32"))] use std::os;
1919
#[cfg(not(target_os = "win32"))] use terminfo::*;
@@ -94,21 +94,21 @@ fn cap_for_attr(attr: attr::Attr) -> &'static str {
9494
}
9595

9696
#[cfg(not(target_os = "win32"))]
97-
pub struct Terminal {
97+
pub struct Terminal<T> {
9898
priv num_colors: u16,
99-
priv out: @mut io::Writer,
99+
priv out: T,
100100
priv ti: ~TermInfo
101101
}
102102

103103
#[cfg(target_os = "win32")]
104-
pub struct Terminal {
104+
pub struct Terminal<T> {
105105
priv num_colors: u16,
106-
priv out: @mut io::Writer,
106+
priv out: T,
107107
}
108108

109109
#[cfg(not(target_os = "win32"))]
110-
impl Terminal {
111-
pub fn new(out: @mut io::Writer) -> Result<Terminal, ~str> {
110+
impl<T: Writer> Terminal<T> {
111+
pub fn new(out: T) -> Result<Terminal<T>, ~str> {
112112
let term = os::getenv("TERM");
113113
if term.is_none() {
114114
return Err(~"TERM environment variable undefined");
@@ -138,7 +138,7 @@ impl Terminal {
138138
/// the corresponding normal color will be used instead.
139139
///
140140
/// Returns true if the color was set, false otherwise.
141-
pub fn fg(&self, color: color::Color) -> bool {
141+
pub fn fg(&mut self, color: color::Color) -> bool {
142142
let color = self.dim_if_necessary(color);
143143
if self.num_colors > color {
144144
let s = expand(*self.ti.strings.find_equiv(&("setaf")).unwrap(),
@@ -158,7 +158,7 @@ impl Terminal {
158158
/// the corresponding normal color will be used instead.
159159
///
160160
/// Returns true if the color was set, false otherwise.
161-
pub fn bg(&self, color: color::Color) -> bool {
161+
pub fn bg(&mut self, color: color::Color) -> bool {
162162
let color = self.dim_if_necessary(color);
163163
if self.num_colors > color {
164164
let s = expand(*self.ti.strings.find_equiv(&("setab")).unwrap(),
@@ -175,7 +175,7 @@ impl Terminal {
175175

176176
/// Sets the given terminal attribute, if supported.
177177
/// Returns true if the attribute was supported, false otherwise.
178-
pub fn attr(&self, attr: attr::Attr) -> bool {
178+
pub fn attr(&mut self, attr: attr::Attr) -> bool {
179179
match attr {
180180
attr::ForegroundColor(c) => self.fg(c),
181181
attr::BackgroundColor(c) => self.bg(c),
@@ -210,7 +210,7 @@ impl Terminal {
210210
}
211211

212212
/// Resets all terminal attributes and color to the default.
213-
pub fn reset(&self) {
213+
pub fn reset(&mut self) {
214214
let mut cap = self.ti.strings.find_equiv(&("sgr0"));
215215
if cap.is_none() {
216216
// are there any terminals that have color/attrs and not sgr0?
@@ -242,20 +242,20 @@ impl Terminal {
242242
}
243243

244244
#[cfg(target_os = "win32")]
245-
impl Terminal {
246-
pub fn new(out: @mut io::Writer) -> Result<Terminal, ~str> {
245+
impl<T: Writer> Terminal<T> {
246+
pub fn new(out: T) -> Result<Terminal<T>, ~str> {
247247
return Ok(Terminal {out: out, num_colors: 0});
248248
}
249249

250-
pub fn fg(&self, _color: color::Color) -> bool {
250+
pub fn fg(&mut self, _color: color::Color) -> bool {
251251
false
252252
}
253253

254-
pub fn bg(&self, _color: color::Color) -> bool {
254+
pub fn bg(&mut self, _color: color::Color) -> bool {
255255
false
256256
}
257257

258-
pub fn attr(&self, _attr: attr::Attr) -> bool {
258+
pub fn attr(&mut self, _attr: attr::Attr) -> bool {
259259
false
260260
}
261261

@@ -266,3 +266,27 @@ impl Terminal {
266266
pub fn reset(&self) {
267267
}
268268
}
269+
270+
impl<T: Writer> Decorator<T> for Terminal<T> {
271+
fn inner(self) -> T {
272+
self.out
273+
}
274+
275+
fn inner_ref<'a>(&'a self) -> &'a T {
276+
&self.out
277+
}
278+
279+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut T {
280+
&mut self.out
281+
}
282+
}
283+
284+
impl<T: Writer> Writer for Terminal<T> {
285+
fn write(&mut self, buf: &[u8]) {
286+
self.out.write(buf);
287+
}
288+
289+
fn flush(&mut self) {
290+
self.out.flush();
291+
}
292+
}

0 commit comments

Comments
 (0)