Skip to content

Commit cb19b9f

Browse files
committed
modernize rust code
* rustfmt * try -> questionmark
1 parent 65712fc commit cb19b9f

File tree

7 files changed

+368
-356
lines changed

7 files changed

+368
-356
lines changed

src/lib.rs

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
5959
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
6060
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
61-
html_root_url = "https://stebalien.github.io/doc/term/term/",
62-
test(attr(deny(warnings))))]
61+
html_root_url = "https://stebalien.github.io/doc/term/term/", test(attr(deny(warnings))))]
6362
#![deny(missing_docs)]
6463
#![cfg_attr(test, deny(warnings))]
6564

@@ -71,7 +70,7 @@ pub use terminfo::TerminfoTerminal;
7170
#[cfg(windows)]
7271
pub use win::WinConsole;
7372

74-
use std::io::{self, Stdout, Stderr};
73+
use std::io::{self, Stderr, Stdout};
7574

7675
pub mod terminfo;
7776

@@ -96,7 +95,11 @@ pub fn stdout() -> Option<Box<StdoutTerminal>> {
9695
pub fn stdout() -> Option<Box<StdoutTerminal>> {
9796
TerminfoTerminal::new(io::stdout())
9897
.map(|t| Box::new(t) as Box<StdoutTerminal>)
99-
.or_else(|| WinConsole::new(io::stdout()).ok().map(|t| Box::new(t) as Box<StdoutTerminal>))
98+
.or_else(|| {
99+
WinConsole::new(io::stdout())
100+
.ok()
101+
.map(|t| Box::new(t) as Box<StdoutTerminal>)
102+
})
100103
}
101104

102105
#[cfg(not(windows))]
@@ -112,10 +115,13 @@ pub fn stderr() -> Option<Box<StderrTerminal>> {
112115
pub fn stderr() -> Option<Box<StderrTerminal>> {
113116
TerminfoTerminal::new(io::stderr())
114117
.map(|t| Box::new(t) as Box<StderrTerminal>)
115-
.or_else(|| WinConsole::new(io::stderr()).ok().map(|t| Box::new(t) as Box<StderrTerminal>))
118+
.or_else(|| {
119+
WinConsole::new(io::stderr())
120+
.ok()
121+
.map(|t| Box::new(t) as Box<StderrTerminal>)
122+
})
116123
}
117124

118-
119125
/// Terminal color definitions
120126
#[allow(missing_docs)]
121127
pub mod color {
@@ -204,54 +210,38 @@ impl std::cmp::PartialEq for Error {
204210
use Error::*;
205211
match *self {
206212
Io(_) => false,
207-
TerminfoParsing(ref inner1) => {
208-
match *other {
209-
TerminfoParsing(ref inner2) => inner1 == inner2,
210-
_ => false,
211-
}
212-
}
213-
ParameterizedExpansion(ref inner1) => {
214-
match *other {
215-
ParameterizedExpansion(ref inner2) => inner1 == inner2,
216-
_ => false,
217-
}
218-
}
219-
NotSupported => {
220-
match *other {
221-
NotSupported => true,
222-
_ => false,
223-
}
224-
}
225-
TermUnset => {
226-
match *other {
227-
TermUnset => true,
228-
_ => false,
229-
}
230-
}
231-
TerminfoEntryNotFound => {
232-
match *other {
233-
TerminfoEntryNotFound => true,
234-
_ => false,
235-
}
236-
}
237-
CursorDestinationInvalid => {
238-
match *other {
239-
CursorDestinationInvalid => true,
240-
_ => false,
241-
}
242-
}
243-
ColorOutOfRange => {
244-
match *other {
245-
ColorOutOfRange => true,
246-
_ => false,
247-
}
248-
}
249-
__Nonexhaustive => {
250-
match *other {
251-
__Nonexhaustive => true,
252-
_ => false,
253-
}
254-
}
213+
TerminfoParsing(ref inner1) => match *other {
214+
TerminfoParsing(ref inner2) => inner1 == inner2,
215+
_ => false,
216+
},
217+
ParameterizedExpansion(ref inner1) => match *other {
218+
ParameterizedExpansion(ref inner2) => inner1 == inner2,
219+
_ => false,
220+
},
221+
NotSupported => match *other {
222+
NotSupported => true,
223+
_ => false,
224+
},
225+
TermUnset => match *other {
226+
TermUnset => true,
227+
_ => false,
228+
},
229+
TerminfoEntryNotFound => match *other {
230+
TerminfoEntryNotFound => true,
231+
_ => false,
232+
},
233+
CursorDestinationInvalid => match *other {
234+
CursorDestinationInvalid => true,
235+
_ => false,
236+
},
237+
ColorOutOfRange => match *other {
238+
ColorOutOfRange => true,
239+
_ => false,
240+
},
241+
__Nonexhaustive => match *other {
242+
__Nonexhaustive => true,
243+
_ => false,
244+
},
255245
}
256246
}
257247
}
@@ -399,5 +389,7 @@ pub trait Terminal: Write {
399389
fn get_mut(&mut self) -> &mut Self::Output;
400390

401391
/// Returns the contained stream, destroying the `Terminal`
402-
fn into_inner(self) -> Self::Output where Self: Sized;
392+
fn into_inner(self) -> Self::Output
393+
where
394+
Self: Sized;
403395
}

src/terminfo/mod.rs

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,15 @@ use Terminal;
2424
use Result;
2525
use self::searcher::get_dbpath_for_term;
2626
use self::parser::compiled::parse;
27-
use self::parm::{expand, Variables, Param};
27+
use self::parm::{expand, Param, Variables};
2828
use self::Error::*;
2929

30-
3130
/// Returns true if the named terminal supports basic ANSI escape codes.
3231
fn is_ansi(name: &str) -> bool {
3332
// SORTED! We binary search this.
3433
static ANSI_TERM_PREFIX: &'static [&'static str] = &[
35-
"Eterm",
36-
"ansi",
37-
"eterm",
38-
"iterm",
39-
"konsole",
40-
"linux",
41-
"mrxvt",
42-
"msyscon",
43-
"rxvt",
44-
"screen",
45-
"tmux",
46-
"xterm",
34+
"Eterm", "ansi", "eterm", "iterm", "konsole", "linux", "mrxvt", "msyscon", "rxvt",
35+
"screen", "tmux", "xterm",
4736
];
4837
match ANSI_TERM_PREFIX.binary_search(&name) {
4938
Ok(_) => true,
@@ -52,7 +41,6 @@ fn is_ansi(name: &str) -> bool {
5241
}
5342
}
5443

55-
5644
/// A parsed terminfo database entry.
5745
#[derive(Debug, Clone)]
5846
pub struct TermInfo {
@@ -70,16 +58,15 @@ impl TermInfo {
7058
/// Create a `TermInfo` based on current environment.
7159
pub fn from_env() -> Result<TermInfo> {
7260
let term_var = env::var("TERM").ok();
73-
let term_name = term_var
74-
.as_ref()
75-
.map(|s| &**s)
76-
.or_else(|| env::var("MSYSCON")
77-
.ok()
78-
.and_then(|s| if s == "mintty.exe" {
79-
Some("msyscon")
80-
} else {
81-
None
82-
}));
61+
let term_name = term_var.as_ref().map(|s| &**s).or_else(|| {
62+
env::var("MSYSCON").ok().and_then(|s| {
63+
if s == "mintty.exe" {
64+
Some("msyscon")
65+
} else {
66+
None
67+
}
68+
})
69+
});
8370
if let Some(term_name) = term_name {
8471
return TermInfo::from_name(term_name);
8572
} else {
@@ -93,7 +80,7 @@ impl TermInfo {
9380
match TermInfo::from_path(&path) {
9481
Ok(term) => return Ok(term),
9582
// Skip IO Errors (e.g., permission denied).
96-
Err(::Error::Io(_)) => {},
83+
Err(::Error::Io(_)) => {}
9784
// Don't ignore malformed terminfo databases.
9885
Err(e) => return Err(e),
9986
}
@@ -131,23 +118,21 @@ impl TermInfo {
131118
// might do this for
132119
// us. Alas. )
133120
fn _from_path(path: &Path) -> Result<TermInfo> {
134-
let file = try!(File::open(path).map_err(::Error::Io));
121+
let file = File::open(path).map_err(::Error::Io)?;
135122
let mut reader = BufReader::new(file);
136123
parse(&mut reader, false)
137124
}
138125

139126
/// Retrieve a capability `cmd` and expand it with `params`, writing result to `out`.
140127
pub fn apply_cap(&self, cmd: &str, params: &[Param], out: &mut io::Write) -> Result<()> {
141128
match self.strings.get(cmd) {
142-
Some(cmd) => {
143-
match expand(cmd, params, &mut Variables::new()) {
144-
Ok(s) => {
145-
try!(out.write_all(&s));
146-
Ok(())
147-
}
148-
Err(e) => Err(e.into()),
129+
Some(cmd) => match expand(cmd, params, &mut Variables::new()) {
130+
Ok(s) => {
131+
out.write_all(&s)?;
132+
Ok(())
149133
}
150-
}
134+
Err(e) => Err(e.into()),
135+
},
151136
None => Err(::Error::NotSupported),
152137
}
153138
}
@@ -156,24 +141,23 @@ impl TermInfo {
156141
pub fn reset(&self, out: &mut io::Write) -> Result<()> {
157142
// are there any terminals that have color/attrs and not sgr0?
158143
// Try falling back to sgr, then op
159-
let cmd = match [("sgr0", &[] as &[Param]), ("sgr", &[Param::Number(0)]), ("op", &[])]
160-
.iter()
161-
.filter_map(|&(cap, params)| {
162-
self.strings.get(cap).map(|c| (c, params))
163-
})
164-
.next() {
165-
Some((op, params)) => {
166-
match expand(op, params, &mut Variables::new()) {
167-
Ok(cmd) => cmd,
168-
Err(e) => return Err(e.into()),
169-
}
170-
}
144+
let cmd = match [
145+
("sgr0", &[] as &[Param]),
146+
("sgr", &[Param::Number(0)]),
147+
("op", &[]),
148+
].iter()
149+
.filter_map(|&(cap, params)| self.strings.get(cap).map(|c| (c, params)))
150+
.next()
151+
{
152+
Some((op, params)) => match expand(op, params, &mut Variables::new()) {
153+
Ok(cmd) => cmd,
154+
Err(e) => return Err(e.into()),
155+
},
171156
None => return Err(::Error::NotSupported),
172157
};
173-
try!(out.write_all(&cmd));
158+
out.write_all(&cmd)?;
174159
Ok(())
175160
}
176-
177161
}
178162

179163
#[derive(Debug, Eq, PartialEq)]
@@ -255,7 +239,6 @@ pub mod parser {
255239
}
256240
pub mod parm;
257241

258-
259242
fn cap_for_attr(attr: Attr) -> &'static str {
260243
match attr {
261244
Attr::Bold => "bold",
@@ -288,15 +271,17 @@ impl<T: Write> Terminal for TerminfoTerminal<T> {
288271
fn fg(&mut self, color: color::Color) -> Result<()> {
289272
let color = self.dim_if_necessary(color);
290273
if self.num_colors > color {
291-
return self.ti.apply_cap("setaf", &[Param::Number(color as i32)], &mut self.out);
274+
return self.ti
275+
.apply_cap("setaf", &[Param::Number(color as i32)], &mut self.out);
292276
}
293277
Err(::Error::ColorOutOfRange)
294278
}
295279

296280
fn bg(&mut self, color: color::Color) -> Result<()> {
297281
let color = self.dim_if_necessary(color);
298282
if self.num_colors > color {
299-
return self.ti.apply_cap("setab", &[Param::Number(color as i32)], &mut self.out);
283+
return self.ti
284+
.apply_cap("setab", &[Param::Number(color as i32)], &mut self.out);
300285
}
301286
Err(::Error::ColorOutOfRange)
302287
}
@@ -324,7 +309,9 @@ impl<T: Write> Terminal for TerminfoTerminal<T> {
324309
}
325310

326311
fn supports_reset(&self) -> bool {
327-
["sgr0", "sgr", "op"].iter().any(|&cap| self.ti.strings.get(cap).is_some())
312+
["sgr0", "sgr", "op"]
313+
.iter()
314+
.any(|&cap| self.ti.strings.get(cap).is_some())
328315
}
329316

330317
fn supports_color(&self) -> bool {
@@ -352,7 +339,8 @@ impl<T: Write> Terminal for TerminfoTerminal<T> {
352339
}
353340

354341
fn into_inner(self) -> T
355-
where Self: Sized
342+
where
343+
Self: Sized,
356344
{
357345
self.out
358346
}
@@ -361,8 +349,8 @@ impl<T: Write> Terminal for TerminfoTerminal<T> {
361349
impl<T: Write> TerminfoTerminal<T> {
362350
/// Create a new TerminfoTerminal with the given TermInfo and Write.
363351
pub fn new_with_terminfo(out: T, terminfo: TermInfo) -> TerminfoTerminal<T> {
364-
let nc = if terminfo.strings.contains_key("setaf") &&
365-
terminfo.strings.contains_key("setab") {
352+
let nc = if terminfo.strings.contains_key("setaf") && terminfo.strings.contains_key("setab")
353+
{
366354
terminfo.numbers.get("colors").map_or(0, |&n| n)
367355
} else {
368356
0
@@ -379,7 +367,9 @@ impl<T: Write> TerminfoTerminal<T> {
379367
///
380368
/// Returns `None` when the terminfo cannot be found or parsed.
381369
pub fn new(out: T) -> Option<TerminfoTerminal<T>> {
382-
TermInfo::from_env().map(move |ti| TerminfoTerminal::new_with_terminfo(out, ti)).ok()
370+
TermInfo::from_env()
371+
.map(move |ti| TerminfoTerminal::new_with_terminfo(out, ti))
372+
.ok()
383373
}
384374

385375
fn dim_if_necessary(&self, color: color::Color) -> color::Color {
@@ -391,7 +381,6 @@ impl<T: Write> TerminfoTerminal<T> {
391381
}
392382
}
393383

394-
395384
impl<T: Write> Write for TerminfoTerminal<T> {
396385
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
397386
self.out.write(buf)

0 commit comments

Comments
 (0)