Skip to content

Commit 4486795

Browse files
committed
Remove unused stuff and switch to pub(crate) whenever possible.
1 parent 950f569 commit 4486795

File tree

7 files changed

+58
-299
lines changed

7 files changed

+58
-299
lines changed

library/test/src/term.rs

+19-108
Original file line numberDiff line numberDiff line change
@@ -6,123 +6,62 @@
66
//! a [terminfo][ti] database, and `WinConsole`, which uses the [Win32 Console
77
//! API][win].
88
//!
9-
//! ```
10-
//!
119
//! [ansi]: https://en.wikipedia.org/wiki/ANSI_escape_code
1210
//! [win]: https://docs.microsoft.com/en-us/windows/console/character-mode-applications
1311
//! [ti]: https://en.wikipedia.org/wiki/Terminfo
1412
1513
#![deny(missing_docs)]
1614

17-
use std::io::prelude::*;
18-
use std::io::{self, Stderr, Stdout};
15+
use std::io::{self, prelude::*};
1916

20-
pub use terminfo::TerminfoTerminal;
17+
pub(crate) use terminfo::TerminfoTerminal;
2118
#[cfg(windows)]
22-
pub use win::WinConsole;
19+
pub(crate) use win::WinConsole;
2320

24-
pub mod terminfo;
21+
pub(crate) mod terminfo;
2522

2623
#[cfg(windows)]
2724
mod win;
2825

2926
/// Alias for stdout terminals.
30-
pub type StdoutTerminal = dyn Terminal<Output = Stdout> + Send;
31-
/// Alias for stderr terminals.
32-
pub type StderrTerminal = dyn Terminal<Output = Stderr> + Send;
27+
pub(crate) type StdoutTerminal = dyn Terminal + Send;
3328

3429
#[cfg(not(windows))]
3530
/// Returns a Terminal wrapping stdout, or None if a terminal couldn't be
3631
/// opened.
37-
pub fn stdout() -> Option<Box<StdoutTerminal>> {
32+
pub(crate) fn stdout() -> Option<Box<StdoutTerminal>> {
3833
TerminfoTerminal::new(io::stdout()).map(|t| Box::new(t) as Box<StdoutTerminal>)
3934
}
4035

4136
#[cfg(windows)]
4237
/// Returns a Terminal wrapping stdout, or None if a terminal couldn't be
4338
/// opened.
44-
pub fn stdout() -> Option<Box<StdoutTerminal>> {
39+
pub(crate) fn stdout() -> Option<Box<StdoutTerminal>> {
4540
TerminfoTerminal::new(io::stdout())
4641
.map(|t| Box::new(t) as Box<StdoutTerminal>)
4742
.or_else(|| WinConsole::new(io::stdout()).ok().map(|t| Box::new(t) as Box<StdoutTerminal>))
4843
}
4944

50-
#[cfg(not(windows))]
51-
/// Returns a Terminal wrapping stderr, or None if a terminal couldn't be
52-
/// opened.
53-
pub fn stderr() -> Option<Box<StderrTerminal>> {
54-
TerminfoTerminal::new(io::stderr()).map(|t| Box::new(t) as Box<StderrTerminal>)
55-
}
56-
57-
#[cfg(windows)]
58-
/// Returns a Terminal wrapping stderr, or None if a terminal couldn't be
59-
/// opened.
60-
pub fn stderr() -> Option<Box<StderrTerminal>> {
61-
TerminfoTerminal::new(io::stderr())
62-
.map(|t| Box::new(t) as Box<StderrTerminal>)
63-
.or_else(|| WinConsole::new(io::stderr()).ok().map(|t| Box::new(t) as Box<StderrTerminal>))
64-
}
65-
6645
/// Terminal color definitions
6746
#[allow(missing_docs)]
68-
pub mod color {
47+
#[cfg_attr(not(windows), allow(dead_code))]
48+
pub(crate) mod color {
6949
/// Number for a terminal color
70-
pub type Color = u32;
71-
72-
pub const BLACK: Color = 0;
73-
pub const RED: Color = 1;
74-
pub const GREEN: Color = 2;
75-
pub const YELLOW: Color = 3;
76-
pub const BLUE: Color = 4;
77-
pub const MAGENTA: Color = 5;
78-
pub const CYAN: Color = 6;
79-
pub const WHITE: Color = 7;
80-
81-
pub const BRIGHT_BLACK: Color = 8;
82-
pub const BRIGHT_RED: Color = 9;
83-
pub const BRIGHT_GREEN: Color = 10;
84-
pub const BRIGHT_YELLOW: Color = 11;
85-
pub const BRIGHT_BLUE: Color = 12;
86-
pub const BRIGHT_MAGENTA: Color = 13;
87-
pub const BRIGHT_CYAN: Color = 14;
88-
pub const BRIGHT_WHITE: Color = 15;
89-
}
90-
91-
/// Terminal attributes for use with term.attr().
92-
///
93-
/// Most attributes can only be turned on and must be turned off with term.reset().
94-
/// The ones that can be turned off explicitly take a boolean value.
95-
/// Color is also represented as an attribute for convenience.
96-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
97-
pub enum Attr {
98-
/// Bold (or possibly bright) mode
99-
Bold,
100-
/// Dim mode, also called faint or half-bright. Often not supported
101-
Dim,
102-
/// Italics mode. Often not supported
103-
Italic(bool),
104-
/// Underline mode
105-
Underline(bool),
106-
/// Blink mode
107-
Blink,
108-
/// Standout mode. Often implemented as Reverse, sometimes coupled with Bold
109-
Standout(bool),
110-
/// Reverse mode, inverts the foreground and background colors
111-
Reverse,
112-
/// Secure mode, also called invis mode. Hides the printed text
113-
Secure,
114-
/// Convenience attribute to set the foreground color
115-
ForegroundColor(color::Color),
116-
/// Convenience attribute to set the background color
117-
BackgroundColor(color::Color),
50+
pub(crate) type Color = u32;
51+
52+
pub(crate) const BLACK: Color = 0;
53+
pub(crate) const RED: Color = 1;
54+
pub(crate) const GREEN: Color = 2;
55+
pub(crate) const YELLOW: Color = 3;
56+
pub(crate) const BLUE: Color = 4;
57+
pub(crate) const MAGENTA: Color = 5;
58+
pub(crate) const CYAN: Color = 6;
59+
pub(crate) const WHITE: Color = 7;
11860
}
11961

12062
/// A terminal with similar capabilities to an ANSI Terminal
12163
/// (foreground/background colors etc).
12264
pub trait Terminal: Write {
123-
/// The terminal's output writer type.
124-
type Output: Write;
125-
12665
/// Sets the foreground color to the given color.
12766
///
12867
/// If the color is a bright color, but the terminal only supports 8 colors,
@@ -132,23 +71,6 @@ pub trait Terminal: Write {
13271
/// if there was an I/O error.
13372
fn fg(&mut self, color: color::Color) -> io::Result<bool>;
13473

135-
/// Sets the background color to the given color.
136-
///
137-
/// If the color is a bright color, but the terminal only supports 8 colors,
138-
/// the corresponding normal color will be used instead.
139-
///
140-
/// Returns `Ok(true)` if the color was set, `Ok(false)` otherwise, and `Err(e)`
141-
/// if there was an I/O error.
142-
fn bg(&mut self, color: color::Color) -> io::Result<bool>;
143-
144-
/// Sets the given terminal attribute, if supported. Returns `Ok(true)`
145-
/// if the attribute was supported, `Ok(false)` otherwise, and `Err(e)` if
146-
/// there was an I/O error.
147-
fn attr(&mut self, attr: Attr) -> io::Result<bool>;
148-
149-
/// Returns `true` if the given terminal attribute is supported.
150-
fn supports_attr(&self, attr: Attr) -> bool;
151-
15274
/// Resets all terminal attributes and colors to their defaults.
15375
///
15476
/// Returns `Ok(true)` if the terminal was reset, `Ok(false)` otherwise, and `Err(e)` if there
@@ -160,15 +82,4 @@ pub trait Terminal: Write {
16082
/// else that might flush stdout's buffer (e.g., writing a line of text), you should flush after
16183
/// calling reset.
16284
fn reset(&mut self) -> io::Result<bool>;
163-
164-
/// Gets an immutable reference to the stream inside
165-
fn get_ref(&self) -> &Self::Output;
166-
167-
/// Gets a mutable reference to the stream inside
168-
fn get_mut(&mut self) -> &mut Self::Output;
169-
170-
/// Returns the contained stream, destroying the `Terminal`
171-
fn into_inner(self) -> Self::Output
172-
where
173-
Self: Sized;
17485
}

library/test/src/term/terminfo/mod.rs

+16-77
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::io::{self, prelude::*, BufReader};
99
use std::path::Path;
1010

1111
use super::color;
12-
use super::Attr;
1312
use super::Terminal;
1413

1514
use parm::{expand, Param, Variables};
@@ -18,20 +17,20 @@ use searcher::get_dbpath_for_term;
1817

1918
/// A parsed terminfo database entry.
2019
#[derive(Debug)]
21-
pub struct TermInfo {
20+
pub(crate) struct TermInfo {
2221
/// Names for the terminal
23-
pub names: Vec<String>,
22+
pub(crate) names: Vec<String>,
2423
/// Map of capability name to boolean value
25-
pub bools: HashMap<String, bool>,
24+
pub(crate) bools: HashMap<String, bool>,
2625
/// Map of capability name to numeric value
27-
pub numbers: HashMap<String, u32>,
26+
pub(crate) numbers: HashMap<String, u32>,
2827
/// Map of capability name to raw (unexpanded) string
29-
pub strings: HashMap<String, Vec<u8>>,
28+
pub(crate) strings: HashMap<String, Vec<u8>>,
3029
}
3130

3231
/// A terminfo creation error.
3332
#[derive(Debug)]
34-
pub enum Error {
33+
pub(crate) enum Error {
3534
/// TermUnset Indicates that the environment doesn't include enough information to find
3635
/// the terminfo entry.
3736
TermUnset,
@@ -64,7 +63,7 @@ impl fmt::Display for Error {
6463

6564
impl TermInfo {
6665
/// Creates a TermInfo based on current environment.
67-
pub fn from_env() -> Result<TermInfo, Error> {
66+
pub(crate) fn from_env() -> Result<TermInfo, Error> {
6867
let term = match env::var("TERM") {
6968
Ok(name) => TermInfo::from_name(&name),
7069
Err(..) => return Err(Error::TermUnset),
@@ -79,7 +78,7 @@ impl TermInfo {
7978
}
8079

8180
/// Creates a TermInfo for the named terminal.
82-
pub fn from_name(name: &str) -> Result<TermInfo, Error> {
81+
pub(crate) fn from_name(name: &str) -> Result<TermInfo, Error> {
8382
get_dbpath_for_term(name)
8483
.ok_or_else(|| {
8584
Error::IoError(io::Error::new(io::ErrorKind::NotFound, "terminfo file not found"))
@@ -88,7 +87,7 @@ impl TermInfo {
8887
}
8988

9089
/// Parse the given TermInfo.
91-
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<TermInfo, Error> {
90+
pub(crate) fn from_path<P: AsRef<Path>>(path: P) -> Result<TermInfo, Error> {
9291
Self::_from_path(path.as_ref())
9392
}
9493
// Keep the metadata small
@@ -99,43 +98,24 @@ impl TermInfo {
9998
}
10099
}
101100

102-
pub mod searcher;
101+
pub(crate) mod searcher;
103102

104103
/// TermInfo format parsing.
105-
pub mod parser {
104+
pub(crate) mod parser {
106105
//! ncurses-compatible compiled terminfo format parsing (term(5))
107-
pub mod compiled;
108-
}
109-
pub mod parm;
110-
111-
fn cap_for_attr(attr: Attr) -> &'static str {
112-
match attr {
113-
Attr::Bold => "bold",
114-
Attr::Dim => "dim",
115-
Attr::Italic(true) => "sitm",
116-
Attr::Italic(false) => "ritm",
117-
Attr::Underline(true) => "smul",
118-
Attr::Underline(false) => "rmul",
119-
Attr::Blink => "blink",
120-
Attr::Standout(true) => "smso",
121-
Attr::Standout(false) => "rmso",
122-
Attr::Reverse => "rev",
123-
Attr::Secure => "invis",
124-
Attr::ForegroundColor(_) => "setaf",
125-
Attr::BackgroundColor(_) => "setab",
126-
}
106+
pub(crate) mod compiled;
127107
}
108+
pub(crate) mod parm;
128109

129110
/// A Terminal that knows how many colors it supports, with a reference to its
130111
/// parsed Terminfo database record.
131-
pub struct TerminfoTerminal<T> {
112+
pub(crate) struct TerminfoTerminal<T> {
132113
num_colors: u32,
133114
out: T,
134115
ti: TermInfo,
135116
}
136117

137118
impl<T: Write + Send> Terminal for TerminfoTerminal<T> {
138-
type Output = T;
139119
fn fg(&mut self, color: color::Color) -> io::Result<bool> {
140120
let color = self.dim_if_necessary(color);
141121
if self.num_colors > color {
@@ -144,32 +124,6 @@ impl<T: Write + Send> Terminal for TerminfoTerminal<T> {
144124
Ok(false)
145125
}
146126

147-
fn bg(&mut self, color: color::Color) -> io::Result<bool> {
148-
let color = self.dim_if_necessary(color);
149-
if self.num_colors > color {
150-
return self.apply_cap("setab", &[Param::Number(color as i32)]);
151-
}
152-
Ok(false)
153-
}
154-
155-
fn attr(&mut self, attr: Attr) -> io::Result<bool> {
156-
match attr {
157-
Attr::ForegroundColor(c) => self.fg(c),
158-
Attr::BackgroundColor(c) => self.bg(c),
159-
_ => self.apply_cap(cap_for_attr(attr), &[]),
160-
}
161-
}
162-
163-
fn supports_attr(&self, attr: Attr) -> bool {
164-
match attr {
165-
Attr::ForegroundColor(_) | Attr::BackgroundColor(_) => self.num_colors > 0,
166-
_ => {
167-
let cap = cap_for_attr(attr);
168-
self.ti.strings.get(cap).is_some()
169-
}
170-
}
171-
}
172-
173127
fn reset(&mut self) -> io::Result<bool> {
174128
// are there any terminals that have color/attrs and not sgr0?
175129
// Try falling back to sgr, then op
@@ -182,26 +136,11 @@ impl<T: Write + Send> Terminal for TerminfoTerminal<T> {
182136
};
183137
self.out.write_all(&cmd).and(Ok(true))
184138
}
185-
186-
fn get_ref(&self) -> &T {
187-
&self.out
188-
}
189-
190-
fn get_mut(&mut self) -> &mut T {
191-
&mut self.out
192-
}
193-
194-
fn into_inner(self) -> T
195-
where
196-
Self: Sized,
197-
{
198-
self.out
199-
}
200139
}
201140

202141
impl<T: Write + Send> TerminfoTerminal<T> {
203142
/// Creates a new TerminfoTerminal with the given TermInfo and Write.
204-
pub fn new_with_terminfo(out: T, terminfo: TermInfo) -> TerminfoTerminal<T> {
143+
pub(crate) fn new_with_terminfo(out: T, terminfo: TermInfo) -> TerminfoTerminal<T> {
205144
let nc = if terminfo.strings.contains_key("setaf") && terminfo.strings.contains_key("setab")
206145
{
207146
terminfo.numbers.get("colors").map_or(0, |&n| n)
@@ -215,7 +154,7 @@ impl<T: Write + Send> TerminfoTerminal<T> {
215154
/// Creates a new TerminfoTerminal for the current environment with the given Write.
216155
///
217156
/// Returns `None` when the terminfo cannot be found or parsed.
218-
pub fn new(out: T) -> Option<TerminfoTerminal<T>> {
157+
pub(crate) fn new(out: T) -> Option<TerminfoTerminal<T>> {
219158
TermInfo::from_env().map(move |ti| TerminfoTerminal::new_with_terminfo(out, ti)).ok()
220159
}
221160

0 commit comments

Comments
 (0)