use crate::color::Rgb;
use crate::mode::KeyboardEnhancements;
use crate::mouse::MouseMode;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct View {
pub content: String,
pub alt_screen: bool,
pub cursor: Option<Cursor>,
pub window_title: String,
pub foreground_color: Option<Rgb>,
pub background_color: Option<Rgb>,
pub progress_bar: Option<ProgressBar>,
pub mouse_mode: MouseMode,
pub report_focus: bool,
pub disable_bracketed_paste_mode: bool,
pub keyboard_enhancements: KeyboardEnhancements,
}
impl View {
#[must_use]
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
..Self::default()
}
}
pub fn set_content(&mut self, content: impl Into<String>) {
self.content = content.into();
}
#[must_use]
pub fn with_alt_screen(mut self, on: bool) -> Self {
self.alt_screen = on;
self
}
#[must_use]
pub fn with_cursor(mut self, cursor: Cursor) -> Self {
self.cursor = Some(cursor);
self
}
#[must_use]
pub fn with_window_title(mut self, title: impl Into<String>) -> Self {
self.window_title = title.into();
self
}
#[must_use]
pub fn with_foreground_color(mut self, color: Rgb) -> Self {
self.foreground_color = Some(color);
self
}
#[must_use]
pub fn with_background_color(mut self, color: Rgb) -> Self {
self.background_color = Some(color);
self
}
#[must_use]
pub fn with_progress_bar(mut self, bar: ProgressBar) -> Self {
self.progress_bar = Some(bar);
self
}
#[must_use]
pub fn with_mouse_mode(mut self, mode: MouseMode) -> Self {
self.mouse_mode = mode;
self
}
#[must_use]
pub fn with_report_focus(mut self, on: bool) -> Self {
self.report_focus = on;
self
}
#[must_use]
pub fn with_bracketed_paste(mut self, on: bool) -> Self {
self.disable_bracketed_paste_mode = !on;
self
}
#[must_use]
pub fn with_keyboard_enhancements(mut self, enhancements: KeyboardEnhancements) -> Self {
self.keyboard_enhancements = enhancements;
self
}
}
impl From<String> for View {
fn from(content: String) -> Self {
Self::new(content)
}
}
impl From<&str> for View {
fn from(content: &str) -> Self {
Self::new(content)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum CursorShape {
#[default]
Block,
Underline,
Bar,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Cursor {
pub x: u16,
pub y: u16,
pub shape: CursorShape,
pub blink: bool,
pub color: Option<Rgb>,
}
impl Cursor {
#[must_use]
pub const fn new(x: u16, y: u16) -> Self {
Self {
x,
y,
shape: CursorShape::Block,
blink: true,
color: None,
}
}
#[must_use]
pub const fn with_shape(mut self, shape: CursorShape) -> Self {
self.shape = shape;
self
}
#[must_use]
pub const fn with_blink(mut self, blink: bool) -> Self {
self.blink = blink;
self
}
#[must_use]
pub const fn with_color(mut self, color: Rgb) -> Self {
self.color = Some(color);
self
}
pub(crate) const fn style(&self) -> u8 {
let base = match self.shape {
CursorShape::Block => 1,
CursorShape::Underline => 3,
CursorShape::Bar => 5,
};
if self.blink { base } else { base + 1 }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum ProgressBarState {
#[default]
None,
Default,
Error,
Indeterminate,
Warning,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub struct ProgressBar {
pub state: ProgressBarState,
pub value: u8,
}
impl ProgressBar {
#[must_use]
pub fn new(state: ProgressBarState, value: u8) -> Self {
Self {
state,
value: value.min(100),
}
}
pub(crate) fn sequence(self) -> String {
let v = self.value.min(100);
match self.state {
ProgressBarState::None => "\x1b]9;4;0\x07".to_string(),
ProgressBarState::Default => format!("\x1b]9;4;1;{v}\x07"),
ProgressBarState::Error => format!("\x1b]9;4;2;{v}\x07"),
ProgressBarState::Indeterminate => "\x1b]9;4;3\x07".to_string(),
ProgressBarState::Warning => format!("\x1b]9;4;4;{v}\x07"),
}
}
}
#[cfg(test)]
mod tests;