如何在nodejs中显示ASCII ART(终端中)旁边的文本?

发布于 2025-01-25 06:16:54 字数 491 浏览 1 评论 0原文

我想以这种格式显示一些ASCII艺术:

 _,-,  Name: Powerful Axe
T_  |  Rarity: Yellow Rarity (77.5)
||`-'  Attack: 100
||     Defense: 100
||     Speed: 100
~~     Gold Value: 10

an axe that is really rare

我希望它在列布局中,就像Neofetch一样,但在Nodejs中。 我从JSON文件中获取所有这些数据,并且我正在使用以下函数将关键对类型值格式化和上色:

const chalk = require('chalk')

function keyValue(key, value) {
    return chalk.green(key) + ": " + chalk.yellow(value)
}

所有这些都需要在终端中,最好不使用节点curses或有福。

I would like to display some ascii art in this format:

 _,-,  Name: Powerful Axe
T_  |  Rarity: Yellow Rarity (77.5)
||`-'  Attack: 100
||     Defense: 100
||     Speed: 100
~~     Gold Value: 10

an axe that is really rare

I want it in a column layout, kind of like neofetch does, but in NodeJS.
I get all this data from a json file, and I am using the following function to format and color the key pair type values:

const chalk = require('chalk')

function keyValue(key, value) {
    return chalk.green(key) + ": " + chalk.yellow(value)
}

This all needs to be in the terminal, and preferably not using node-curses or blessed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2025-02-01 06:16:54

我发现了一个非常低效且肿的解决方案:

function print2MultiLine(string, string2) {
    var arr1 = string.split("\n");
    var arr2 = string2.split("\n");
    if (arr1.length > arr2.length) {
        while (arr1.length > arr2.length) {
            arr2.push("");
        }
    } else if (arr1.length < arr2.length) {
        while (arr1.length < arr2.length) {
            arr1.push("");
        }
    }
    var longestLine = 0;
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i].length > longestLine) {
            longestLine = arr1[i].length;
        }
    }
    for (let i = 0; i < arr1.length; i++) {
        while (arr1[i].length < longestLine) {
            arr1[i] += " ";
        }
    }
    for (let i = 0; i < arr1.length; i++) {
        console.log(arr1[i] + "  " + arr2[i]);
    }
}

如果您喜欢注释:

function print2MultiLine(string, string2) {
    

    // for each line in each string, print it out 3 spaces away from the other
    var arr1 = string.split("\n");
    var arr2 = string2.split("\n");

    // if one string is longer than the other, make the shorter one the length of the longer one
    
    if (arr1.length > arr2.length) {
        while (arr1.length > arr2.length) {
            arr2.push("");
        }
    } else if (arr1.length < arr2.length) {
        while (arr1.length < arr2.length) {
            arr1.push("");
        }
    }

    // get the length of the longest line in arr1
    var longestLine = 0;
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i].length > longestLine) {
            longestLine = arr1[i].length;
            
        }
    }


    // make all other lines in arr1 the length of the longest line
    for (let i = 0; i < arr1.length; i++) {
        while (arr1[i].length < longestLine) {
            arr1[i] += " ";
        }
    }

    // print out the strings
    for (let i = 0; i < arr1.length; i++) {
        console.log(arr1[i] + "  " + arr2[i]);
    }
}

预览:

“

I found a very inefficient and bloated solution:

function print2MultiLine(string, string2) {
    var arr1 = string.split("\n");
    var arr2 = string2.split("\n");
    if (arr1.length > arr2.length) {
        while (arr1.length > arr2.length) {
            arr2.push("");
        }
    } else if (arr1.length < arr2.length) {
        while (arr1.length < arr2.length) {
            arr1.push("");
        }
    }
    var longestLine = 0;
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i].length > longestLine) {
            longestLine = arr1[i].length;
        }
    }
    for (let i = 0; i < arr1.length; i++) {
        while (arr1[i].length < longestLine) {
            arr1[i] += " ";
        }
    }
    for (let i = 0; i < arr1.length; i++) {
        console.log(arr1[i] + "  " + arr2[i]);
    }
}

if you like comments:

function print2MultiLine(string, string2) {
    

    // for each line in each string, print it out 3 spaces away from the other
    var arr1 = string.split("\n");
    var arr2 = string2.split("\n");

    // if one string is longer than the other, make the shorter one the length of the longer one
    
    if (arr1.length > arr2.length) {
        while (arr1.length > arr2.length) {
            arr2.push("");
        }
    } else if (arr1.length < arr2.length) {
        while (arr1.length < arr2.length) {
            arr1.push("");
        }
    }

    // get the length of the longest line in arr1
    var longestLine = 0;
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i].length > longestLine) {
            longestLine = arr1[i].length;
            
        }
    }


    // make all other lines in arr1 the length of the longest line
    for (let i = 0; i < arr1.length; i++) {
        while (arr1[i].length < longestLine) {
            arr1[i] += " ";
        }
    }

    // print out the strings
    for (let i = 0; i < arr1.length; i++) {
        console.log(arr1[i] + "  " + arr2[i]);
    }
}

Preview:

preview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文