ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

WezTerm 输入法组合状态实战:用 window:composition_status() 打造输入状态提示条

WezTerm 输入法组合状态实战:用 window:composition_status() 打造输入状态提示条 WezTerm 输入法组合状态实战用 window:composition_status() 打造输入状态提示条【免费下载链接】weztermA GPU-accelerated cross-platform terminal emulator and multiplexer written by wez and implemented in Rust项目地址: https://gitcode.com/GitHub_Trending/we/wezterm本篇指南聚焦 WezTermGPU 加速的跨平台终端模拟器与多路复用器使用 Rust 实现中的窗口 APIwindow:composition_status()它返回当前死键dead key或 IME 输入法组合composition状态文本。读完本文你将掌握如何通过update-right-status事件把“正在组合输入”的实时状态渲染到状态栏并用compose_cursor改变光标颜色从而解决中文/日文输入法、欧洲重音字符输入时“看不到当前未提交输入”的痛点。一、什么是 composition组合输入状态在终端里输入某些字符时键盘并不会一次就提交完整的字符。典型的场景包括死键dead key例如按下^后终端进入等待下一个字符的“保持”状态随后按下e才组合出êIME 输入法组合中文拼音、日文假名等输入法在候选词确定之前会先出现一段“未提交”的预编辑文本Leader Key 模式按下 Leader 键后短暂处于一种模态修饰状态。在上述状态下WezTerm 的光标处会显示一段“组合中”的占位文本直到组合完成或取消。window:composition_status()返回的正是这段文本——它与光标位置正在显示的组合文本完全一致。该 API 在{{since(20220319-142410-0fcdea07)}}版本引入并且与 window:leader_is_active() 同时发布二者经常搭配使用一个表示“组合输入中”一个表示“Leader 键激活中”。二、函数签名与返回值window:composition_status()是一个异步方法无参数返回值类型为string | nil若输入层不处于组合状态返回nil若输入层正在组合返回一个字符串内容为当前未提交的组合文本与光标处显示的占位文本一致。源码级的返回值映射从 GUI 窗口 Lua 绑定 可以看到该方法通过TermWindowNotif::Apply将查询请求发回 GUI 主线程再把DeadKeyStatus枚举映射为 Lua 返回值methods.add_async_method(composition_status, |_, this, _: ()| async move { let (tx, rx) smol::channel::bounded(1); this.window .notify(TermWindowNotif::Apply(Box::new(move |term_window| { tx.try_send(match term_window.composition_status() { DeadKeyStatus::None None, DeadKeyStatus::Composing(s) Some(s.clone()), }) .ok(); }))); // ... Ok(result) });其中DeadKeyStatus定义在 window/src/lib.rspub enum DeadKeyStatus { /// 未处于死键/组合处理的保持状态 None, /// 保持中等待组合完成字符串是未提交的组合文本用于作为占位显示 Composing(String), }而TermWindow::composition_status()只是直接返回窗口持有的状态字段见 termwindow/keyevent.rspub fn composition_status(self) - DeadKeyStatus { self.dead_key_status }dead_key_status字段在窗口创建时初始化为DeadKeyStatus::None并通过WindowEvent::AdviseDeadKeyStatus事件被更新见 termwindow/mod.rs 与 termwindow/mod.rs例如启用debug_key_events时会把状态变化打印到日志中。也就是说Lua 侧读到的状态与渲染层用于绘制光标占位文本的状态是同一个来源。三、实战示例在状态栏显示组合状态官方文档给出的经典用法是在update-right-status事件中调用该方法把“正在组合”提示放到右侧状态区域同时用compose_cursor把光标染成橙色。下面直接给出可复制到~/.wezterm.lua的完整配置local wezterm require wezterm wezterm.on(update-right-status, function(window, pane) local compose window:composition_status() if compose then compose COMPOSING: .. compose end window:set_right_status(compose or ) end) return { colors { compose_cursor orange, }, }配置逐行解读片段作用wezterm.on(update-right-status, ...)注册状态栏更新回调状态变化时由 WezTerm 自动触发window:composition_status()获取当前组合文本未组合时为nilcompose COMPOSING: .. compose为组合文本加前缀明确提示用户当前处于组合状态window:set_right_status(compose or )把结果写入右侧状态栏nil时清空为避免残留旧文本colors.compose_cursor orange设置组合/Leader 激活状态下的光标颜色效果当你在中文拼音或日文输入法中打出未确认的拼音/假名时WezTerm 右侧状态栏会实时显示COMPOSING: 当前未提交文本当组合结束候选上屏后状态栏自动恢复为空。光标同时变为橙色作为第二重视觉提示。可以进一步增强的写法状态栏不仅支持文本还可以拼接格式化元素。例如给组合状态加上醒目颜色其他状态保持原样local wezterm require wezterm local function colorize(text, color) return { { Text }, { Foreground { Color color } }, { Text text } } end wezterm.on(update-right-status, function(window, pane) local compose window:composition_status() if compose then window:set_right_status(colorize(COMPOSING: .. compose, orange)) else window:set_right_status() end end) return {}四、compose_cursor组合状态下的光标配色colors.compose_cursor是控制组合/Leader 状态下光标颜色的配置项定义于 config/src/color.rs/// The color to use for the cursor when a dead key or leader state is active pub compose_cursor: OptionRgbaColor,注意它的语义是当死键、组合状态或 Leader 键激活时使用的光标颜色。它在底层渲染中与dead_key_or_leader标志绑定见 termwindow/render/pane.rsdead_key_or_leader: self.term_window.dead_key_status ! DeadKeyStatus::None || self.term_window.leader_is_active(),也就是说只要dead_key_status ! None或者 Leader 键处于激活状态光标就会切换为compose_cursor指定的颜色。该配置项支持 CSS 颜色名、#rrggbb十六进制等形式。它同样是内置配色方案color scheme的组成部分例如在 scheme_data.rs 中Catppuccin Mocha 方案设置为compose_cursor #f2cdcd、carbonfox 设置为#3ddbd9、Dracula 设置为#ffb86c。你可以参考这些内置方案挑选与主题协调的组合光标色。五、与 leader_is_active() 的关系与差异window:composition_status()与 window:leader_is_active() 属于同一批引入的窗口状态查询 API常被一起使用composition_status()关注的是输入法/死键组合产生的未提交文本leader_is_active()关注的是Leader 键模态状态例如leader { key a, mods CTRL }配置下按下CTRLA后的一小段时间内 Leader 处于激活状态二者共享compose_cursor光标配色只要任一状态激活光标都会变色。一个典型的组合用法是同时在状态栏分别展示两种状态local wezterm require wezterm wezterm.on(update-right-status, function(window, pane) local parts {} if window:leader_is_active() then table.insert(parts, LEADER) end local compose window:composition_status() if compose then table.insert(parts, COMPOSING: .. compose) end window:set_right_status(table.concat(parts, )) end) return { leader { key a, mods CTRL }, colors { compose_cursor orange, }, }配置leader { key a, mods CTRL }后按CTRLA会在状态栏短暂显示LEADER输入拼音时则显示COMPOSING: ni之类的实时组合文本光标同时保持橙色提示。六、运行与验证将上面的配置保存为~/.wezterm.lua或按需放入wezterm的配置搜索路径启动 WezTerm 后切换到你常用的中文/日文输入法开始输入拼音或假名——在候选词确认前右侧状态栏应实时出现COMPOSING: ...光标变为compose_cursor指定的颜色在英文/直接输入模式下状态栏为空光标恢复默认配色若希望进一步调试可临时设置debug_key_events true此时 termwindow/mod.rs 中的处理逻辑会把DeadKeyStatus的变化以log::info级别打印到终端日志便于确认状态切换时机。小结window:composition_status()是 WezTerm 暴露输入法组合状态的窗口级 Lua API。通过它你可以在update-right-status事件中实时获知死键/IME 组合文本配合colors.compose_cursor实现“状态栏提示 光标变色”的双重反馈让非 ASCII 输入过程不再黑盒。从源码可以看到该 API 与光标渲染共享同一个DeadKeyStatus状态源window/src/lib.rs因此你看到的状态提示与屏幕上的光标占位文本始终一致可以作为可靠的输入状态信号。【免费下载链接】weztermA GPU-accelerated cross-platform terminal emulator and multiplexer written by wez and implemented in Rust项目地址: https://gitcode.com/GitHub_Trending/we/wezterm创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表