9 分•作者: PretzelFisch•5 个月前
返回首页
最新
24 分•作者: tosh•5 个月前
33 分•作者: LucidLynx•5 个月前
1 分•作者: stared•5 个月前
1 分•作者: denhit10•5 个月前
1 分•作者: user_timo•5 个月前
1 分•作者: mpweiher•5 个月前
1 分•作者: argulane•5 个月前
1 分•作者: Jitesh117•5 个月前
1 分•作者: kauai1•5 个月前
1 分•作者: mohdmahmodi•5 个月前
1 分•作者: limbicsystem•5 个月前
1 分•作者: collectedparts•5 个月前
Claude Code 使用 Ink (用于 CLI 的 React) 通过光标移动来定位文本。当你从它的终端输出中复制文本时,每一行都会用尾随空格填充以填满终端宽度,并且每一行都会从 UI 界面获得一致的起始缩进。结果是文本在终端中看起来没问题,但粘贴出来就像垃圾一样。
我原以为这是一个难题——复制操作会破坏行边界,并将填充与缩进合并成一个模糊的空格团。错。一个十六进制转储 (`pbpaste | xxd`) 显示了每个行边界处真正的 `0a` 换行符,以及一致的起始空白。整个修复方法是:去除尾随空白,去缩进。
```python
#! /usr/bin/env python3
import re, sys
def clean(text):
text = re.sub(r'\x1b\[[0-9;]*[a-zA-Z]', '', text) # 去除 ANSI 转义序列
text = re.sub(r'\x1b\][^\x07]*\x07', '', text) # 去除 OSC 序列
text = re.sub(r'[ \t]+$', '', text, flags=re.MULTILINE) # 去除尾随空白
lines = text.split('\n')
while lines and not lines[0].strip(): lines.pop(0) # 删除前导空行
while lines and not lines[-1].strip(): lines.pop() # 删除尾随空行
indent = min((len(l) - len(l.lstrip()) for l in lines if l.strip()), default=0)
return '\n'.join(l[indent:] for l in lines) + '\n'
print(clean(sys.stdin.read()), end='')
```
用法:
```
pbpaste | ./unfuck-paste
pbpaste | ./unfuck-paste | pbcopy # 在剪贴板上原地修复
```
是的,我知道 Clean Clode 存在——但它是一个 Web 应用程序。如果我已经在终端中从终端复制内容,打开浏览器来修复终端输出感觉不对劲(并且不适用于任何敏感内容)。
1 分•作者: giancaIta•5 个月前
1 分•作者: Markoff•5 个月前
1 分•作者: ramimac•5 个月前
1 分•作者: vrganj•5 个月前
1 分•作者: zippode•5 个月前
1 分•作者: tokenomics•5 个月前
1 分•作者: mariuz•5 个月前