8

我正在使用BoxElementfrom blessed来显示聊天记录。

使用 . 添加句子pushLine。为清楚起见,天数以行分隔(使用添加的另一个字符串pushLine)。每行都与 parent 一样宽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                                     │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
│                                                          │
└──────────────────────────────────────────────────────────┘
4

1 回答 1

6

它似乎blessed没有实现分隔符之类的东西,但我们可以简单地用一个简单的类自己实现它们,该类存储每个分隔符的行索引并在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();

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

一个可能的折衷办法是只接受比框宽度短的行居中,用适量的空格填充它们。

于 2020-08-07T13:30:46.893 回答