diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-15 19:51:27 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-15 19:51:27 +0800 |
| commit | 046ac064b6f4790f9053ad1e6109843bb38f89dd (patch) | |
| tree | 80c16af0d217f69001b3fad7a02dea49c2a3aebc /mingling/src/res/osc94 | |
| parent | aa2b0daa88089df32b3ea67d7c1831b5d8fd64f8 (diff) | |
feat: move OSC94 under its own module and add confirmer predicates
Extract OSC94 state and guard types from `res` into a dedicated
`osc94` module, and add `ConfirmerCount` and `ConfirmerPredicate`
types with `YesConfirm` and `TrueConfirm` implementations.
Diffstat (limited to 'mingling/src/res/osc94')
| -rw-r--r-- | mingling/src/res/osc94/state.rs | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/mingling/src/res/osc94/state.rs b/mingling/src/res/osc94/state.rs deleted file mode 100644 index c3d2bdf..0000000 --- a/mingling/src/res/osc94/state.rs +++ /dev/null @@ -1,74 +0,0 @@ -/// `OSC 9;4` 协议消息 -/// -/// 用于通过 ANSI 转义序列向终端发送任务进度通知消息 -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum OSC94State { - /// 清除/隐藏进度(任务完成时使用),对应状态码 `0` - Clean, - /// 正常状态,对应状态码 `1`,需要配合进度值(0-100) - Normal(f32), - /// 错误状态,对应状态码 `2`(通常显示为红色) - Error, - /// 不确定状态,对应状态码 `3`(显示为无限循环的动画,用于进度未知的任务) - Unknown, - /// 警告状态,对应状态码 `4`(通常显示为黄色) - Warn, -} - -impl OSC94State { - /// Returns the state code for the `OSC 9;4` protocol. - #[must_use] - pub const fn state_code(&self) -> u8 { - match self { - Self::Clean => 0, - Self::Normal(_) => 1, - Self::Error => 2, - Self::Unknown => 3, - Self::Warn => 4, - } - } - - /// Returns the progress value (0-100) for the `Normal` state, clamped to the valid range. - #[must_use] - pub const fn progress(&self) -> f32 { - match self { - Self::Normal(progress) => (progress.clamp(0.0, 1.0) * 100.0).round(), - _ => 0.0, - } - } - - /// Converts the message into the corresponding `OSC 9;4` escape sequence string. - #[must_use] - pub fn to_escape_sequence(&self) -> String { - format!("\x1b]9;4;{};{}\x07", self.state_code(), self.progress()) - } - - /// Sends the OSC 9;4 message to the terminal via stdout. - /// - /// # Panics - /// - /// Panics if the stdout stream cannot be flushed. - pub fn send(&self) { - use std::io::Write; - print!("{}", self.to_escape_sequence()); - std::io::stdout().flush().unwrap(); - } -} - -impl std::fmt::Display for OSC94State { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.to_escape_sequence()) - } -} - -impl From<OSC94State> for String { - fn from(msg: OSC94State) -> Self { - msg.to_escape_sequence() - } -} - -impl From<&OSC94State> for String { - fn from(msg: &OSC94State) -> Self { - msg.to_escape_sequence() - } -} |
