默认情况下,祝福的"提示"是黑色黑色 - 我该如何设计它?

ame*_*hes 5 javascript blessed

我正在使用祝福,我正在尝试为我的应用程序添加提示.它工作正常,但我无法阅读其文本.我准备了一个最小的例子,说明了我所看到的.

我想知道如何在输入中设置文本样式.文档中提到样式属性似乎没有任何效果.

这就是我所看到的(输入和两个按钮上有文字,但黑色是黑色).

在此输入图像描述

这是使用标准终端和标准主题在Debian 9上重现错误的代码:

var blessed = require('blessed');
var screen = blessed.screen({});

var prompt = blessed.prompt({
    left: 'center',
    top: 'center',
    height: 'shrink',
    width: 'shrink',
    border: 'line',
});

screen.append(prompt);

screen.key(['q', 'C-c'], function quit() {
    return process.exit(0);
});

screen.render();

prompt.input('Search:', 'test', function() {});
Run Code Online (Sandbox Code Playgroud)

sou*_*zin 1

免责声明:我对代码库不太熟悉blessed,因此可能有一种更原生的方法来执行此操作。如果没有,那么听起来应该请求/实现此功能。

观察 1 - 您的终端的颜色设置导致了该问题

根据您提供的屏幕截图,您的终端的默认颜色为黑色前景和白色背景。如果您在终端设置中反转它,您应该能够看到预期的行为。

但!无论用户的设置是什么,您的应用程序都应该如此,所以这不是一个好的解决方案......

观察 2 -Prompt构造函数将其子级硬编码为黑色背景

如有疑问,请前往源头!以下是prompt.js截至 2017 年 9 月 30 日的部分内容:

// ...
function Prompt(options) {
  // ...
  Box.call(this, options);

  this._.input = new Textbox({
    // ...
    bg: 'black'
  });

  this._.okay = new Button({
    // ...
    bg: 'black',
    hoverBg: 'blue',
  });

  this._.cancel = new Button({
    // ...
    bg: 'black',
    hoverBg: 'blue',
  });
}
// ...
Run Code Online (Sandbox Code Playgroud)

因此,解决问题的唯一方法似乎是在创建后覆盖这些子级的样式属性Prompt

解决方案 1 - 创建后覆盖子样式属性

创建提示后,您可以覆盖每个子项的样式。将前景设为白色(应该是白色)可能是最简单的......

另外,为了可维护性,这个 hack 确实应该在它自己的功能中。

function createBlessedPrompt(options) {
    var prompt = blessed.prompt(options);

    // NOTE - Not sure if blessed has some sortof `children()` selector.
    //        If not, we probably should create one.
    //        Nevertheless, temporarily hardcoding the children here...
    //
    var promptChildren = [prompt._.input, prompt._.okay, prompt._.cancel];

    promptChildren.forEach(x => {
        Object.assign(x.style, {
            fg: 'white',
            bg: 'black'
        });
    });

    return prompt;
}
Run Code Online (Sandbox Code Playgroud)

解决方案 2 - 向受祝福的存储库提交错误修复

这看起来确实是一个有福本身的问题。如果您能想到一种Prompt正确处理这种情况的方法,您应该完全帮助您的编码员同事并编写一个问题/拉取请求来解决此问题。

祝你好运!