use crate::color::Rgb;
use crate::key::Key;
use crate::mode::{KeyboardEnhancements, KeyboardEnhancementsReport, ModeSetting, ReportedMode};
use crate::mouse::{Mouse, MouseMode};
use std::fmt;
use std::io;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Msg<M> {
KeyPress(Key),
KeyRelease(Key),
MouseClick(Mouse),
MouseRelease(Mouse),
MouseWheel(Mouse),
MouseMotion(Mouse),
Paste(String),
PasteStart,
PasteEnd,
Focus,
Blur,
CursorPosition {
x: i32,
y: i32,
},
KeyboardEnhancements(KeyboardEnhancementsReport),
ModeReport {
mode: ReportedMode,
value: ModeSetting,
},
TerminalVersion(String),
ForegroundColor(Rgb),
BackgroundColor(Rgb),
CursorColor(Rgb),
WindowSize(Size),
App(M),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Size {
pub width: u16,
pub height: u16,
}
impl Size {
#[must_use]
pub const fn new(width: u16, height: u16) -> Self {
Self { width, height }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct View {
pub content: String,
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_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)]
#[non_exhaustive]
pub enum Error {
Killed,
Interrupted,
Panic(String),
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Killed => f.write_str("program was killed"),
Self::Interrupted => f.write_str("program was interrupted"),
Self::Panic(message) => write!(f, "a command panicked: {message}"),
Self::Io(err) => write!(f, "terminal error: {err}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn views_come_from_strings() {
let mut view: View = "hello".into();
assert_eq!(view, View::new(String::from("hello")));
view.set_content("bye");
assert_eq!(view.content, "bye");
}
#[test]
fn errors_say_what_happened() {
assert_eq!(Error::Killed.to_string(), "program was killed");
assert_eq!(Error::Interrupted.to_string(), "program was interrupted");
assert_eq!(
Error::Panic("boom".into()).to_string(),
"a command panicked: boom"
);
let io: Error = io::Error::other("gone").into();
assert_eq!(io.to_string(), "terminal error: gone");
assert!(std::error::Error::source(&io).is_some());
}
}