Terminal borders that don't cost a cell

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Terminal borders that don’t cost a cell 2026-11-13 :: tags: #Ghostty #Ratatui #TUI #Terminals

不占用单元格的终端边框 2026-11-13 :: 标签:#Ghostty #Ratatui #TUI #Terminals

This post (and the code behind it) was written with a lot of help from AI. I’m not going to apologize for that. If that bothers you, feel free to close the tab. I maintain Ratatui, so I see a lot of the TUIs people build. Many of them end up looking fairly same-ish: plain boxes on a boring background, missing a bunch of the things that good design in other mediums brings us.

这篇文章(以及背后的代码)是在人工智能的大力协助下完成的。我对此不会道歉。如果你对此感到困扰,请随意关闭标签页。我负责维护 Ratatui,所以我见过很多人构建的 TUI。其中许多看起来大同小异:枯燥背景上的简单方框,缺少了其他媒介中优秀设计所带来的许多元素。

Borders are part of the problem. They have to take up space, so they become part of the panels people want to build. One option is to drop them and just use color, but borders are really nice for delineating and surrounding things. There are lots of ways to sort of get borders in a terminal, but none that are 100% good. I’ve drawn enough ┌──┐ by hand to have opinions about this. The usual way is the Unicode Box Drawing block, which has 128 characters for lines, corners and joins. Somehow that’s still not enough:

边框是问题的一部分。它们必须占用空间,因此成为了人们想要构建的面板的一部分。一种选择是放弃边框仅使用颜色,但边框在界定和包围事物方面确实非常出色。在终端中实现边框的方法有很多,但没有一种是百分之百完美的。我亲手绘制过足够多的 ┌──┐,因此对此有自己的看法。通常的方法是使用 Unicode 制表符,它包含 128 个用于线条、拐角和连接的字符。不知何故,这仍然不够用:

So I’ve been working on a terminal protocol that draws thin lines on the edges of cells instead. I’ve got it working as a patch to Ghostty (and an earlier version in Rio before that), and I’ve built demo apps for it with Ratatui. I’d like feedback from terminal and TUI authors. Every line in that screenshot is a cell border. None of them use up a row or column.

所以我一直在研究一种终端协议,它改为在单元格的边缘绘制细线。我已经将其作为 Ghostty 的补丁实现(之前在 Rio 中也有过早期版本),并用 Ratatui 构建了演示应用程序。我希望得到终端和 TUI 作者的反馈。截图中的每一条线都是一个单元格边框。它们都不占用任何行或列。

The tab labels sit on the top edge of each pane, the text fields are underlined right under the text, and the buttons are one row tall and rounded. Here’s the same app in a terminal without cell borders, falling back to box-drawing characters: Each pane loses a row and a column on every side, so the log pane shows two fewer lines and the notes wrap differently. The tab labels move inside the frame. The text fields use the underline attribute instead, and the checkbox and buttons turn into brackets.

标签页标签位于每个窗格的顶部边缘,文本字段在文本下方有下划线,按钮高度为一行且呈圆角。这是同一个应用程序在没有单元格边框的终端中的样子,它回退到了制表符:每个窗格在每一侧都损失了一行一列,因此日志窗格显示的行数减少了两行,笔记的换行方式也不同了。标签页标签移动到了框架内部。文本字段改用下划线属性,复选框和按钮变成了括号。

What I’d use it for: Borders, obviously. Panes and panels that don’t eat a row and column on every side. Menus and popups that sit on top of other content and still look like they’re on top. Panel style apps, where the layout is a set of regions that each want an edge. Widgets like text fields, buttons and checkboxes, which want a line right up against their text. Making things look neater in general. A border on the outer edge of a panel’s background looks a lot cleaner, and text can run right up to it.

我会将其用于:显然是边框。不占用每一侧行和列的窗格和面板。位于其他内容之上且看起来确实在顶层的菜单和弹出窗口。面板风格的应用程序,其布局是一组各自需要边缘的区域。像文本字段、按钮和复选框这样需要紧贴文本绘制线条的小部件。总体上让界面看起来更整洁。面板背景外边缘的边框看起来干净得多,文本可以直接延伸到边框处。

Up close: The settings form, with cell borders and then with the box-drawing fallback: The tabs are the same story. With cell borders they sit on the pane’s top edge. With box characters they need their own row inside the frame:

近距离观察:设置表单,分别使用单元格边框和制表符回退方案:标签页也是一样的情况。使用单元格边框时,它们位于窗格的顶部边缘。使用制表符时,它们需要在框架内占用自己的一行:

Charts: Lines between cells also work for charts. btop-style braille graphs fill every cell with dots, so there’s nowhere to put a gridline or an average line without giving up a row. With cell borders they sit on the cell edges between the dots. Here’s a monitor in that style, built with Ratatui: And the fallback, which has to drop the gridlines:

图表:单元格之间的线条也适用于图表。btop 风格的盲文图表用点填充每个单元格,因此如果不放弃一行,就无法放置网格线或平均线。使用单元格边框时,它们位于点之间的单元格边缘。这是用 Ratatui 构建的此类风格的监视器:以及必须放弃网格线的回退方案:

In motion: Here’s the tiling panes demo moving panes around, opening menus and the about dialog: Partway through, it turns on a checkerboard of the cells, which shows how everything lines up with the grid. Toggle it here: Show the cell grid.

动态演示:这是平铺窗格的演示,展示了移动窗格、打开菜单和关于对话框的过程:在演示过程中,它开启了单元格的棋盘格显示,展示了所有内容如何与网格对齐。在此处切换:显示单元格网格。

What box characters can’t do: The root of it is that box-drawing characters are text. Each line is a glyph that takes up a whole cell, and pretty much everything annoying about them falls out of that. Space is the obvious one. On my 16” laptop with a font I can actually read, the terminal is something like 200x50 (your prefs will move that around a bit). Columns are mostly fine at that size. Rows aren’t. A layout with a couple of levels of nested panels can easily spend 6 or 8 of those 50 rows on nothing but lines. That’s a lot of screen to give up for decoration.

制表符无法做到的事情:根本原因在于制表符是文本。每一条线都是一个占用整个单元格的字形,关于它们的所有烦人之处几乎都源于此。空间是显而易见的问题。在我 16 英寸的笔记本电脑上,使用我能看清的字体,终端大约是 200x50(你的偏好设置会略有不同)。在这个尺寸下,列数通常没问题,但行数不行。一个具有几层嵌套面板的布局很容易在仅仅绘制线条上就消耗掉 50 行中的 6 到 8 行。为了装饰而放弃这么多屏幕空间实在太多了。

You also can’t put a line between two cells that both have something in them. So there’s no way to underline a text field right under its text, or to outline a button, without adding padding around it. Rounded corners sort of work, except the corner glyph has one background color, and when the panel’s background is different from what’s around it you get a little square of the wrong color in each corner. And the line itself is whatever the font decides it is, weight and position included.

你也不能在两个都包含内容的单元格之间放置线条。因此,如果不添加内边距,就无法在文本字段下方为其添加下划线,也无法为按钮添加轮廓。圆角勉强可行,但拐角字形具有特定的背景颜色,当面板背景与周围环境不同时,每个拐角处都会出现一个颜色错误的小方块。而且线条本身完全取决于字体,包括粗细和位置。

Then there’s copy and paste. Select some text inside a box and you get the box too. Everyone who’s pasted from a TUI into Slack knows the little │ cleanup dance. Sometimes you do want the box (pasting a table into a chat, say), but mostly it’s junk. Cell borders aren’t text, so they never get copied, which cuts both ways.

还有复制和粘贴的问题。选择方框内的文本时,你也会把方框一起复制下来。每个从 TUI 粘贴到 Slack 的人都知道那种清理 │ 符号的繁琐过程。有时你确实需要方框(比如将表格粘贴到聊天中),但大多数情况下它只是垃圾信息。单元格边框不是文本,所以它们永远不会被复制,这有利也有弊。

How it works: A cell border is a cell attribute, like a background color. Each cell can carry up to twelve line segments: the two halves of each of its four edges, and four arms from its center. Each segment has its own color, width, placement and style, and the terminal draws them over the cell grid. Borders are stored in the cells, so they scroll, erase, insert and reflow with the text.

工作原理:单元格边框是一种单元格属性,类似于背景颜色。每个单元格最多可以携带十二个线段:其四个边缘中每一边的两个半段,以及从中心延伸出的四条臂。每个线段都有自己的颜色、宽度、位置和样式,终端会将它们绘制在单元格网格之上。边框存储在单元格中,因此它们会随文本一起滚动、擦除、插入和重排。

The edges are split into halves so borders line up with quadrant block characters (▖▗▘▝ and friends). That makes it easy to draw a shape that’s a half cell in from the grid and still put a neat border around it. Yes, that still uses a character for the fill, and there might be a better way to do that part. One problem at a time. The same idea could stretch to sextants and octants, or to quarter and eighth widths, if designs need them.

边缘被分成两半,以便边框与象限块字符(▖▗▘▝ 等)对齐。这使得绘制一个从网格向内缩进半个单元格的形状,并同时在其周围加上整洁的边框变得很容易。是的,这仍然使用字符进行填充,也许有更好的方法来处理那部分。一次解决一个问题。如果设计需要,同样的想法可以扩展到六分块和八分块,或者四分之一和八分之一宽度。

Styles and joints: Lines can be solid, double, dashed or dotted. Each corner and center of a cell can be square or rounded, with a configurable radius and anti-aliased arcs. A rounded corner can name an outside color, so the area outside the arc is painted correctly when the panel’s background differs from what’s behind it. When two neighboring cells both put a line on their shared edge, they merge into one line. The stroke with the higher priority wins, then the wider one, then the one to the right or below.

样式和连接:线条可以是实线、双线、虚线或点线。单元格的每个拐角和中心都可以是方形或圆形的,具有可配置的半径和抗锯齿圆弧。圆角可以指定外部颜色,以便当面板背景与其背后的内容不同时,圆弧外部的区域能被正确绘制。当两个相邻单元格都在其共享边缘上放置线条时,它们会合并为一条线。优先级较高的笔触胜出,其次是较宽的,然后是位于右侧或下方的。