Skip to content

Commit ab5dd64

Browse files
committed
💥 Future-proof UnsupportedTerminal error
1 parent 90a27ae commit ab5dd64

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

crates/benchmark/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() -> Result<()> {
4242

4343
let supported = match color_palette(QueryOptions::default()) {
4444
Ok(_) => true,
45-
Err(Error::UnsupportedTerminal) => false,
45+
Err(Error::UnsupportedTerminal(_)) => false,
4646
Err(e) => return Err(e),
4747
};
4848

@@ -54,7 +54,7 @@ fn main() -> Result<()> {
5454
fn bench() -> Result<Duration> {
5555
let start = Instant::now();
5656
match black_box(color_palette(QueryOptions::default())) {
57-
Ok(_) | Err(Error::UnsupportedTerminal) => Ok(start.elapsed()),
57+
Ok(_) | Err(Error::UnsupportedTerminal(_)) => Ok(start.elapsed()),
5858
Err(err) => Err(err),
5959
}
6060
}

crates/terminal-colorsaurus/src/error.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ pub enum Error {
1919
/// [`QueryOptions::require_terminal_on_stdout`]: `crate::QueryOptions::require_terminal_on_stdout`
2020
NotATerminal(NotATerminalError),
2121
/// The terminal does not support querying for the foreground or background color.
22-
UnsupportedTerminal,
22+
UnsupportedTerminal(UnsupportedTerminalError),
2323
}
2424

2525
impl error::Error for Error {
2626
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
2727
match self {
2828
Error::Io(source) => Some(source),
2929
Error::NotATerminal(source) => Some(source),
30+
Error::UnsupportedTerminal(source) => Some(source),
3031
_ => None,
3132
}
3233
}
@@ -47,9 +48,7 @@ impl fmt::Display for Error {
4748
write!(f, "operation did not complete within {timeout:?}")
4849
}
4950
Error::NotATerminal(e) => fmt::Display::fmt(e, f),
50-
Error::UnsupportedTerminal {} => {
51-
write!(f, "the terminal does not support querying for its colors")
52-
}
51+
Error::UnsupportedTerminal(e) => fmt::Display::fmt(e, f),
5352
}
5453
}
5554
}
@@ -60,6 +59,12 @@ impl From<io::Error> for Error {
6059
}
6160
}
6261

62+
impl Error {
63+
pub(crate) fn unsupported() -> Self {
64+
Error::UnsupportedTerminal(UnsupportedTerminalError)
65+
}
66+
}
67+
6368
#[derive(Debug)]
6469
#[non_exhaustive]
6570
pub struct NotATerminalError;
@@ -68,6 +73,18 @@ impl error::Error for NotATerminalError {}
6873

6974
impl fmt::Display for NotATerminalError {
7075
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
write!(f, "stdout is not connected to a terminal")
76+
f.write_str("stdout is not connected to a terminal")
77+
}
78+
}
79+
80+
#[derive(Debug)]
81+
#[non_exhaustive]
82+
pub struct UnsupportedTerminalError;
83+
84+
impl error::Error for UnsupportedTerminalError {}
85+
86+
impl fmt::Display for UnsupportedTerminalError {
87+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88+
f.write_str("the terminal does not support querying for its colors")
7289
}
7390
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::{Color, ColorPalette, Error, QueryOptions, Result};
22

33
pub(crate) fn color_palette(_options: QueryOptions) -> Result<ColorPalette> {
4-
Err(Error::UnsupportedTerminal)
4+
Err(Error::unsupported())
55
}
66

77
pub(crate) fn foreground_color(_options: QueryOptions) -> Result<Color> {
8-
Err(Error::UnsupportedTerminal)
8+
Err(Error::unsupported())
99
}
1010

1111
pub(crate) fn background_color(_options: QueryOptions) -> Result<Color> {
12-
Err(Error::UnsupportedTerminal)
12+
Err(Error::unsupported())
1313
}

crates/terminal-colorsaurus/src/xterm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn query<T>(
105105
ensure_is_terminal(options.require_terminal_on_stdout)?;
106106

107107
if quirks.is_known_unsupported() {
108-
return Err(Error::UnsupportedTerminal);
108+
return Err(Error::unsupported());
109109
}
110110

111111
let mut tty = terminal()?;
@@ -143,7 +143,7 @@ fn read_color_response(r: &mut Reader<'_>) -> Result<Vec<u8>> {
143143
// the terminal does not recocgnize the color query.
144144
if !r.buffer().starts_with(b"]") {
145145
_ = consume_da1_response(r, false);
146-
return Err(Error::UnsupportedTerminal);
146+
return Err(Error::unsupported());
147147
}
148148

149149
// Some terminals always respond with BEL (see terminal survey).

0 commit comments

Comments
 (0)