Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup shell module #9178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions crates/common/src/io/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ impl TtyWidth {
pub fn get() -> Self {
// use stderr
#[cfg(unix)]
#[allow(clippy::useless_conversion)]
let opt = terminal_size::terminal_size_of(unsafe {
std::os::fd::BorrowedFd::borrow_raw(2.into())
});
let opt = terminal_size::terminal_size_of(std::io::stderr());
#[cfg(not(unix))]
let opt = terminal_size::terminal_size();
match opt {
Expand Down Expand Up @@ -190,25 +187,17 @@ impl Shell {
}

/// Get a static reference to the global shell.
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
///
/// Initializes the global shell with the default values if it has not been set yet.
pub fn get() -> impl DerefMut<Target = Self> + 'static {
#[inline(never)]
#[cold]
#[cfg_attr(debug_assertions, track_caller)]
fn shell_get_fail() -> Mutex<Shell> {
Mutex::new(Shell::new())
}

GLOBAL_SHELL.get_or_init(shell_get_fail).lock().unwrap_or_else(PoisonError::into_inner)
GLOBAL_SHELL.get_or_init(Default::default).lock().unwrap_or_else(PoisonError::into_inner)
}

/// Set the global shell.
///
/// # Panics
///
/// Panics if the global shell has already been set.
#[inline]
#[track_caller]
pub fn set(self) {
if GLOBAL_SHELL.get().is_some() {
Expand Down Expand Up @@ -290,21 +279,18 @@ impl Shell {
}

/// Gets a reference to the underlying stdout writer.
#[inline]
pub fn out(&mut self) -> &mut dyn Write {
self.maybe_err_erase_line();
self.output.stdout()
}

/// Gets a reference to the underlying stderr writer.
#[inline]
pub fn err(&mut self) -> &mut dyn Write {
self.maybe_err_erase_line();
self.output.stderr()
}

/// Erase from cursor to end of line if needed.
#[inline]
pub fn maybe_err_erase_line(&mut self) {
if self.err_supports_color() && self.set_needs_clear(false) {
// This is the "EL - Erase in Line" sequence. It clears from the cursor
Expand Down Expand Up @@ -336,7 +322,6 @@ impl Shell {
/// This will render a message in [ERROR] style with a bold `Error: ` prefix.
///
/// **Note**: will log regardless of the verbosity level.
#[inline]
pub fn error(&mut self, message: impl fmt::Display) -> Result<()> {
self.maybe_err_erase_line();
self.output.message_stderr(&"Error", &ERROR, Some(&message), false)
Expand All @@ -346,7 +331,6 @@ impl Shell {
/// This will render a message in [WARN] style with a bold `Warning: `prefix.
///
/// **Note**: if `verbosity` is set to `Quiet`, this is a no-op.
#[inline]
pub fn warn(&mut self, message: impl fmt::Display) -> Result<()> {
match self.verbosity {
Verbosity::Quiet => Ok(()),
Expand All @@ -357,15 +341,13 @@ impl Shell {
/// Write a styled fragment.
///
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
#[inline]
pub fn write_stdout(&mut self, fragment: impl fmt::Display, color: &Style) -> Result<()> {
self.output.write_stdout(fragment, color)
}

/// Write a styled fragment with the default color. Use the [`sh_print!`] macro instead.
///
/// **Note**: if `verbosity` is set to `Quiet`, this is a no-op.
#[inline]
pub fn print_out(&mut self, fragment: impl fmt::Display) -> Result<()> {
if self.verbosity == Verbosity::Quiet {
Ok(())
Expand All @@ -377,15 +359,13 @@ impl Shell {
/// Write a styled fragment
///
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
#[inline]
pub fn write_stderr(&mut self, fragment: impl fmt::Display, color: &Style) -> Result<()> {
self.output.write_stderr(fragment, color)
}

/// Write a styled fragment with the default color. Use the [`sh_eprint!`] macro instead.
///
/// **Note**: if `verbosity` is set to `Quiet`, this is a no-op.
#[inline]
pub fn print_err(&mut self, fragment: impl fmt::Display) -> Result<()> {
if self.verbosity == Verbosity::Quiet {
Ok(())
Expand Down Expand Up @@ -499,6 +479,7 @@ impl ShellOut {

impl ColorChoice {
/// Converts our color choice to [`anstream`]'s version.
#[inline]
fn to_anstream_color_choice(self) -> anstream::ColorChoice {
match self {
Self::Always => anstream::ColorChoice::Always,
Expand All @@ -508,6 +489,7 @@ impl ColorChoice {
}
}

#[inline]
fn supports_color(choice: anstream::ColorChoice) -> bool {
match choice {
anstream::ColorChoice::Always |
Expand Down
Loading