From 89b4595ed9228df2ce976e4b0630f5dba1208474 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 8 Mar 2025 19:05:38 +0100 Subject: [PATCH 1/3] Print bell character --- CHANGELOG.md | 3 +++ src/frame.rs | 5 +++++ src/terminal.rs | 11 ++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48e4615..c43f3dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Procedure when bumping the version number: ## Unreleased +### Added +- `Frame::set_bell` to print a bell character when the frame is displayed + ## v0.3.3 - 2025-02-28 ### Fixed diff --git a/src/frame.rs b/src/frame.rs index 03fbb04..e42ba6b 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -8,6 +8,7 @@ pub struct Frame { pub(crate) widthdb: WidthDb, pub(crate) buffer: Buffer, pub(crate) title: Option, + pub(crate) bell: bool, } impl Frame { @@ -48,6 +49,10 @@ impl Frame { self.title = title; } + pub fn set_bell(&mut self, bell: bool) { + self.bell = bell; + } + pub fn widthdb(&mut self) -> &mut WidthDb { &mut self.widthdb } diff --git a/src/terminal.rs b/src/terminal.rs index c26b0fc..07fe686 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -8,7 +8,7 @@ use crossterm::event::{ DisableBracketedPaste, EnableBracketedPaste, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags, }; -use crossterm::style::{PrintStyledContent, StyledContent}; +use crossterm::style::{Print, PrintStyledContent, StyledContent}; use crossterm::terminal::{ BeginSynchronizedUpdate, Clear, ClearType, EndSynchronizedUpdate, EnterAlternateScreen, LeaveAlternateScreen, SetTitle, @@ -274,6 +274,7 @@ impl Terminal { self.draw_differences()?; self.update_cursor()?; self.update_title()?; + self.ring_bell()?; Ok(()) } @@ -315,4 +316,12 @@ impl Terminal { } Ok(()) } + + fn ring_bell(&mut self) -> io::Result<()> { + if self.frame.bell { + self.out.queue(Print('\x07'))?; + } + self.frame.bell = false; + Ok(()) + } } From e3af509358dd57e24e3073de8a32640d6a3832d5 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 8 Mar 2025 19:10:29 +0100 Subject: [PATCH 2/3] Add bell widget --- CHANGELOG.md | 1 + src/widgets.rs | 2 ++ src/widgets/bell.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/widgets/bell.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index c43f3dc..c30abdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Procedure when bumping the version number: ### Added - `Frame::set_bell` to print a bell character when the frame is displayed +- `widgets::bell` ## v0.3.3 - 2025-02-28 diff --git a/src/widgets.rs b/src/widgets.rs index 28b44d8..cbbff7c 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -1,4 +1,5 @@ pub mod background; +pub mod bell; pub mod border; pub mod boxed; pub mod cursor; @@ -16,6 +17,7 @@ pub mod text; pub mod title; pub use background::*; +pub use bell::*; pub use border::*; pub use boxed::*; pub use cursor::*; diff --git a/src/widgets/bell.rs b/src/widgets/bell.rs new file mode 100644 index 0000000..b37fb67 --- /dev/null +++ b/src/widgets/bell.rs @@ -0,0 +1,55 @@ +use crate::{Frame, Size, Widget, WidthDb}; + +/////////// +// State // +/////////// + +#[derive(Debug, Default, Clone)] +pub struct BellState { + // Whether the bell should be rung the next time the widget is displayed. + pub ring: bool, +} + +impl BellState { + pub fn new() -> Self { + Self::default() + } + + pub fn widget(&mut self) -> Bell<'_> { + Bell { state: self } + } +} + +//////////// +// Widget // +//////////// + +#[derive(Debug)] +pub struct Bell<'a> { + state: &'a mut BellState, +} + +impl Bell<'_> { + pub fn state(&mut self) -> &mut BellState { + self.state + } +} + +impl Widget for Bell<'_> { + fn size( + &self, + _widthdb: &mut WidthDb, + _max_width: Option, + _max_height: Option, + ) -> Result { + Ok(Size::ZERO) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + if self.state.ring { + frame.set_bell(true); + self.state.ring = false + } + Ok(()) + } +} From 57aa8c59308f6f0aa82bde415a42b56c3d6f7c4d Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 8 Mar 2025 19:30:44 +0100 Subject: [PATCH 3/3] Bump version to 0.3.4 --- CHANGELOG.md | 2 ++ Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c30abdf..bef60bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Procedure when bumping the version number: ## Unreleased +## v0.3.4 - 2025-03-8 + ### Added - `Frame::set_bell` to print a bell character when the frame is displayed - `widgets::bell` diff --git a/Cargo.toml b/Cargo.toml index 6868881..22967fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "toss" -version = "0.3.3" +version = "0.3.4" edition = "2021" [dependencies]