how a stray "j" ruined my evening
How a stray “j” ruined my evening
一个多余的“j”是如何毁掉我的夜晚的
Some time ago I decided to wrangle a silly link shortener that I called shirts linkener, shirts for short. There’s nothing special about it in particular, rather it’s in my run-once-forget-forever shell script that copies the generated links to a clipboard using wl-clipboard.
前段时间,我决定折腾一个简单的短链接生成器,我把它叫做“shirts linkener”,简称“shirts”。它本身没什么特别的,主要是因为我写了一个“运行一次就忘掉”的 Shell 脚本,利用 wl-clipboard 将生成的链接复制到剪贴板。
#!/bin/sh
shirt=$(curl -s -X POST "$(pass show shirts/url)" \
-H "Authorization: Bearer $(pass show shirts)" \
-H "Content-Type: application/json" \
-d "{\"url\": \"${1?missing url}\"}")
echo "$shirt" | jq
echo "$shirt" | jq -r '.short_url' | wl-copy
Nothing out of the ordinary here. It shortens the link and copies it to a clipboard. By then I have been happily using it for a couple of months without an issue. 这里看起来没什么异常。它缩短链接并将其复制到剪贴板。在那之前,我已经愉快地使用了几个月,没出过任何问题。
The fun began when I decided to try out gurk, a Signal TUI written in Rust. Those who know me, know my obsession with terminal interfaces so this was an obvious match.
有趣的事情开始了,当时我决定尝试一下 gurk——一个用 Rust 编写的 Signal 终端界面(TUI)。认识我的人都知道我对终端界面有着执念,所以这简直是绝配。
One evening I was sharing a link to an image hosted on S3 when I noticed people were saying it shows 404 to them. After pasting the link into Firefox, it worked. What is going on? I send them the link and it works now… 一天晚上,我分享了一个托管在 S3 上的图片链接,结果发现大家说链接显示 404。我把链接粘贴到 Firefox 里,它却能正常打开。这是怎么回事?我重新发给他们,这次又好了……
Time passes and I get the same responses from multiple friends - 404. After some time looking at the messages I notice a pattern - each url ends with “j”. 过了一段时间,我从多个朋友那里得到了同样的反馈——404。在仔细检查了这些消息后,我发现了一个规律:每个 URL 的末尾都多了一个“j”。
Turns out I was copying the link with a \n at the end, which in most cases went unnoticed, but in ANSI newline delimiter is translated as “j”, hence the stray jay at the end of each link.
原来,我在复制链接时末尾带上了一个换行符 \n。在大多数情况下这不会被察觉,但在 ANSI 换行符的解析中,它被转换成了“j”,这就是每个链接末尾那个多余的“j”的由来。
After having a solid five minutes of immense shame I found that jq has --join-output argument, which prevents exactly my case: --join-output / -j: Like -r but jq won't print a newline after each output.
在经历了整整五分钟的极度羞愧后,我发现 jq 有一个 --join-output 参数,它正好能解决我的问题:--join-output / -j:类似于 -r,但 jq 不会在每次输出后打印换行符。
Moral of this story is that terminals are fun and shooting myself in the foot repeatedly acts as a great learning opportunity. 这个故事的教训是:终端很有趣,而不断地“搬起石头砸自己的脚”也是一种极好的学习机会。
Some of the sources around control codes and escape sequences: 关于控制代码和转义序列的一些参考资料: