绘制一条始终与其父 BoxElement 一样宽的线?

Drawing a line that's always as wide as its parent BoxElement?

我正在使用 blessed 中的 BoxElement 来显示聊天记录。

使用 pushLine 添加句子。为清楚起见,天数按行划分(使用 pushLine 添加的另一个字符串)。每行与父行一样宽 BoxElement.

但是,如果调整了 TUI 的大小,则该行不再适合。

我有两个问题:

  1. 那条线如何适应它的新宽度?
  2. (加分)我怎样才能使文本居中?

问题示例如下所示:

/**
 * Example.ts
 */
import * as blessed from 'blessed';

const screen = blessed.screen({
    smartCSR: true,
    title: 'Chatr',
    dockBorders: true
});

const chatBox = blessed.box({
    parent: screen,
    title: 'Chatbox',
    top: 'top',
    left: 'center',
    height: '100%',
    width: '100%',
    border: {
        type: 'line'
    },
});
screen.append(chatBox);
screen.render();

chatBox.pushLine("This is the first line");

 // This is the separator - and will not resize with the terminal 
chatBox.pushLine("_".repeat(chatBox.width as number - 2));

chatBox.pushLine("This is a second line");
screen.render();

当代码为 运行 ts-node ./Example.js 时,它呈现为:

┌────────────────────────────────────────────────────────────────────────────────────────┐
│This is a line                                                                          │
│________________________________________________________________________________________│
│This is a second line                                                                   │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
│                                                                                        │
└────────────────────────────────────────────────────────────────────────────────────────┘

调整终端大小得到这个结果:

┌──────────────────────────────────────────────────────────┐
│This is a line                                            │
│__________________________________________________________│
│______________________________                            │
│This is a second line                                     │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
└──────────────────────────────────────────────────────────┘

似乎 blessed 没有实现分隔符之类的东西,但我们可以简单地自己实现它们,使用一个简单的 class 存储每个分隔符的行索引并在 resize 事件。类似于:

import * as blessed from "blessed";

// The required Separators class
class Separators {
  private box: any;
  private separators: number[] = [];

  public constructor(box: any) {
    this.box = box;

    box.on("resize", () => {
      const sep = this.sep();

      this.separators.forEach(line => {
        box.deleteLine(line);
        box.insertLine(line, sep);
      });
    });
  }

  public add(): void {
    const { box, separators } = this;

    separators.push(box.getLines().length);
    box.pushLine(this.sep());
  }

  private sep(): string {
    return "_".repeat((this.box.width as number) - 3);
  }
}

const screen = blessed.screen({
  smartCSR: true,
  title: "Chatr",
  dockBorders: true
});

const chatBox = blessed.box({
  parent: screen,
  title: "Chatbox",
  top: "top",
  left: "center",
  height: "100%",
  width: "100%",
  border: {
    type: "line"
  }
});
const sep = new Separators(chatBox); // <- the new Separator bound to the box
screen.append(chatBox);
screen.render();

chatBox.pushLine("This is the first line");

// This is the separator - and it resize with the terminal
sep.add();

chatBox.pushLine("This is a second line");
chatBox.pushLine("While this is the third line");

// This is another separator - it resize with the terminal as well
sep.add();

chatBox.pushLine("And last this is the last line");

screen.render();

关于bonus point,现在应该很容易获得了;困难的部分是将一条比盒子宽度更长的线居中:如果我们将它分成更多的线到中心,所有的线索引(在分割中心线旁边)都会改变并且可能变得更难跟踪它们。

一个可能的折衷方案是只接受短于方框宽度的行居中,并用适当的空格量向左填充。