gpt4 book ai didi

javascript - 绘制一条总是与其父 BoxElement 一样宽的线?

转载 作者:行者123 更新时间:2023-12-01 15:08:42 25 4
gpt4 key购买 nike

我正在使用 BoxElement来自 blessed显示聊天记录。
使用 pushLine 添加句子.为清楚起见,天以行分隔(使用 pushLine 添加的另一个字符串)。每行与父行一样宽BoxElement .
但是,如果调整 TUI 的大小,则该行不再适合。
我有两个问题:

  • 那条线如何适应它的新宽度?
  • (加分)我怎样才能使该行中间的文本居中?

  • 该问题的一个示例如下所示:
    /**
    * 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没有实现分隔符之类的东西,但我们可以简单地使用一个简单的类来实现它们,该类存储每个分隔符的行索引并在 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();
    关于加分,现在应该很容易实现;困难的部分是让一条比盒子宽度更长的线居中:如果我们将它分成更多的线来居中,所有的线索引(在分割的居中线旁边)都会改变并且可能变得更难跟踪它们。
    一个可能的折衷办法是只接受比框宽度短的行居中,用适量的空格填充它们。

    关于javascript - 绘制一条总是与其父 BoxElement 一样宽的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63249530/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com