#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub enum CursorStyle {
#[default]
DefaultUserShape,
BlinkingBlock,
SteadyBlock,
BlinkingUnderScore,
SteadyUnderScore,
BlinkingBar,
SteadyBar,
}
#[cfg(any(feature = "crossterm", doc))]
#[allow(clippy::must_use_candidate)]
pub fn size() -> Option<(u32, u32)> {
crossterm::terminal::size()
.ok()
.map(|(cols, rows)| (u32::from(cols), u32::from(rows)))
}
#[cfg(any(feature = "crossterm", doc))]
#[allow(clippy::must_use_candidate)]
pub fn size_or() -> (u32, u32) {
crossterm::terminal::size()
.map(|(cols, rows)| (u32::from(cols), u32::from(rows)))
.unwrap_or((80, 24))
}
pub fn set_cursor_style(style: CursorStyle) {
match style {
CursorStyle::DefaultUserShape => print!("\x1b[0 q"),
CursorStyle::BlinkingBlock => print!("\x1b[1 q"),
CursorStyle::SteadyBlock => print!("\x1b[2 q"),
CursorStyle::BlinkingUnderScore => print!("\x1b[3 q"),
CursorStyle::SteadyUnderScore => print!("\x1b[4 q"),
CursorStyle::BlinkingBar => print!("\x1b[5 q"),
CursorStyle::SteadyBar => print!("\x1b[6 q"),
}
}
pub fn secondary_screen() {
print!("\x1b[?1049h");
}
pub fn primary_screen() {
print!("\x1b[?1049l");
}
#[cfg(any(feature = "crossterm", doc))]
pub fn enable_raw_mode() {
crossterm::terminal::enable_raw_mode().expect("failed to enable raw mode");
}
#[cfg(any(feature = "crossterm", doc))]
pub fn disable_raw_mode() {
crossterm::terminal::disable_raw_mode().expect("failed to disable raw mode");
}
#[must_use = "does nothing unless stored, consider `let _guard = ...`"]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ScreenGuard;
impl ScreenGuard {
pub fn enter() -> Self {
secondary_screen();
Self
}
}
impl Drop for ScreenGuard {
fn drop(&mut self) {
primary_screen();
}
}
#[cfg(any(feature = "crossterm", doc))]
#[must_use = "does nothing unless stored, consider `let _guard = ...`"]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RawModeGuard;
#[cfg(any(feature = "crossterm", doc))]
impl RawModeGuard {
pub fn enter() -> Self {
enable_raw_mode();
Self
}
}
#[cfg(any(feature = "crossterm", doc))]
impl Drop for RawModeGuard {
fn drop(&mut self) {
disable_raw_mode();
}
}