

命令行下光标颜色随输入法改变的尝试
source link: https://zhuanlan.zhihu.com/p/35559878
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

命令行下光标颜色随输入法改变的尝试
之前有在 vim 中文输入解决方案 中介绍过在 electron 中我们可以借助
组件让光标颜色随输入法的变化而变化。 最近突然冒出个想法,在命令行下是否也能让光标颜色随着输入法改变来改变, 这样在命令行下使用中文会更容易一点。
首先,我使用的 iTerm2 是支持通过 applescript 获取和设置光标颜色的,代码如下:
tell application "iTerm"
tell current session of current window
get cursor color
end tell
end tell
tell application "iTerm"
tell current session of current window
set cursor color {65535, 65535, 65535, 1}
end tell
end tell
使用命令行方式调用 osascript 可以很容易的完成获取和设置光标颜色的任务。
接下来的问题是如何监听到输入法改变的事件?首先尝试的就是
这个组件,
然后经过尝试发现这个组件使用 nodejs 调用是不会收到任何回调事件的。 起初我以为这是这个 node 的问题,于是用 objective-c 写原生,然后编译调用, 结果还是一样,回调压根不会触发! 但是在 app 里面确是可以触发的,所以得出结论,CFNotificationCenterAddObserver 以及类似的 api 是只能在 app 里面使用的。
命令行的方式不行于是就想到了另一种办法:修改 iTerm2 的源码,事实证明这个办法是可行的,iTerm2 本身就在 PTYSession 文件中对部分系统事件进行了监听,而且也可以支持直接设置光标颜色,以下就是修改的 patch:
diff --git a/sources/PTYSession.m b/sources/PTYSession.m
index 699f5697..c2de773d 100644
--- a/sources/PTYSession.m
+++ b/sources/PTYSession.m
@@ -474,6 +474,8 @@ static const NSUInteger kMaxHosts = 100;
iTermMetalGlue *_metalGlue NS_AVAILABLE_MAC(10_11);
int _updateCount;
+
+ NSString *_defaultCursorColor;
}
+ (void)registerSessionInArrangement:(NSDictionary *)arrangement {
@@ -615,6 +617,11 @@ static const NSUInteger kMaxHosts = 100;
selector:@selector(annotationVisibilityDidChange:)
name:iTermAnnotationVisibilityDidChange
object:nil];
+ [[NSDistributedNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(inputMethodDidChange:)
+ name:(NSString *)kTISNotifySelectedKeyboardInputSourceChanged
+ object:nil];
+
[self updateVariables];
if (!synthetic) {
@@ -3165,6 +3172,12 @@ ITERM_WEAKLY_REFERENCEABLE
}
_textview.highlightCursorLine = [iTermProfilePreferences boolForKey:KEY_USE_CURSOR_GUIDE
inProfile:_profile];
+
+ NSColor *color = [_colorMap colorForKey:kColorMapCursor];
+
+ _defaultCursorColor = [NSString stringWithFormat:@"#%02X%02X%02X",
+ (int) (color.redComponent * 0xFF), (int) (color.greenComponent * 0xFF),
+ (int) (color.blueComponent * 0xFF)];
}
- (NSColor *)tabColorInProfile:(NSDictionary *)profile
@@ -4418,6 +4431,21 @@ ITERM_WEAKLY_REFERENCEABLE
}
}
+- (void)inputMethodDidChange:(NSNotification *)notification {
+ TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
+ NSString *sourceId = (NSString *) TISGetInputSourceProperty(source, kTISPropertyInputSourceID);
+
+ NSColor *cursorColor;
+
+ if (YES == [sourceId isEqualToString:@"com.apple.keylayout.US"]) {
+ cursorColor = [NSColor colorFromHexString:_defaultCursorColor];
+ } else {
+ cursorColor = [NSColor colorFromHexString:@"#ffd700"];
+ }
+
+ [_screen terminalSetCursorColor:cursorColor];
+}
+
// Metal is disabled when any note anywhere is visible because compositing NSViews over Metal
// is a horror and besides these are subviews of PTYTextView and I really don't
// want to invest any more in this little-used feature.
不过可惜 iTerm2 这里有个 bug,就是每次设置颜色后光标形状会恢复成方块,如果你使用 neovim 这种输入状态下光标会变竖线的编辑器,那就有点蛋疼了。
已向 Term2 提出了这个问题,现在 master 分支已经修复了。
本文基于 CC BY-NC-ND 4.0 发布。
Recommend
-
35
前言“说到对图片进行处理,我们经常会想到PhotoShop这类的图像处理工具。作为前端开发者,我们经常会需要处理一些特效,例如根据不同的状态,让图标显示不同的颜色。或者是hover的时候,对图片的对比度,阴影进行处理。”你以为这些是经过PS软件处理出来的?不不
-
5
在 spawn 的子进程中保持命令行颜色 ...
-
5
改变终端下的光标颜色,包括 screen 和 tmux! 本文来自
-
28
Vim 配置光标形状和颜色(Windows Terminal、xterm) 发表于 2021-06-08 ...
-
7
爱马仕尝试在日本为品牌包装盒申请颜色商标 左 晓荔 2021-12-19 ...
-
10
Linux 环境 Android studio / IDEA 输入法光标不跟随问题 2021-12-31 本来没留意这个,毕竟也不是不能用,但网上找了下,解决方法也不复杂,就整了下。根本原因是 IDE 自带的 JDK runtime 没能很好的适配 Fctix,所以,换...
-
5
WPF 自定义文本框输入法 IME 跟随光标 本文告诉大家在 WPF 写一个自定义的文本框,如何实现让输入法跟随光标 本文非小白向,本文适合想开发自定义的文本框,从底层开始开发的文本库的伙伴。在开始之前,期望了解了文本库开发的基础知...
-
5
:::tip 最近在着手腾讯文档的输入体验优化,在其中有一个不起眼的小需求引起了我的注意,并顺便研究了一些事件监听机制相结合的特点,特此记录一下填坑过程。 ::: 模拟光标跟随 大部分的主流输入法都有这样一...
-
10
在之前介绍了双拼输入法之后,我其实没有停下折腾的心思。从小鹤换到自然码,又从自然码换回小鹤,同时也打起了形码的主意。这次实验中,我尝试了比较容易找到配置,也有相对较大的使用群体的形码输入法...
-
9
关于getImageData ctx.getImageData(),可以获取canvas的data图片数据,这个图片数据返回的data是一个对象,该对象包含指定的 ImageData 对象的图像数据。 对于 ImageData 对象中的每个像素,都存在着四方面的信息,即 RGBA 值: R - 红色 (...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK