如何在控制台应用程序中生成彩色 GUI?

发布于 2024-07-13 19:11:51 字数 273 浏览 8 评论 0原文

对于以下问题,答案可能适用于 C/C++、C# 或 Python。 如果可能的话,我希望答案是跨平台的,但我意识到我可能需要 conioncurses

  1. 如何输出彩色文本?
  2. 我将如何制作像 topnethack 这样的 GUI,其中某些内容被“绘制”到终端中的某些空间?

如果可能的话,一个小的 oneliner 代码示例会很棒。

For the following questions, answers may be for C/C++, C#, or Python. I would like the answers to be cross platform if possible but I realize I will probably need conio or ncurses

  1. How do I output colored text?
  2. How would I do a GUI like top or nethack where certain things are "drawn" to certain spaces in the terminal?

If possible a small oneliner code example would be great.

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

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

发布评论

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

评论(6

ゃ人海孤独症 2024-07-20 19:11:51

是的,这些是 VT100 转义码。 最简单的事情就是使用一些 Curses 的风格。 一旦选择了curses 风格,执行1 和2 就非常简单了。

这是有关ncurses 的操作方法。

http://web.cs.mun.ca/~rod/ncurses/ ncurses.html

Yes, these are VT100 escape codes. The simplest thing is to use some flavor of Curses. Once, you choose a curses flavor it is pretty simple to do both 1 and 2.

Here's a HowTo on ncurses.

http://web.cs.mun.ca/~rod/ncurses/ncurses.html

岁月蹉跎了容颜 2024-07-20 19:11:51

从这个角度来看,控制台在很多方面只是经典终端设备的模拟。 Curses 最初是为了支持在不同终端类型上执行常见操作的一种方式而创建的,其中用户可以在登录序列中选择正在使用的实际终端。 这一传统在 ncurses 中仍然存在。

ncurses 库提供了调用函数来直接定位光标并发出文本,并且众所周知,它适用于 Windows 控制台(CMD.EXE 运行的位置)以及各种 *nix 平台等效项(例如 XTerms 等)。 如果您有这样的东西,它甚至可能通过串行线路与真正的 Dec VT100 一起工作...

VT100 和更高版本所理解的转义序列成为 ANSI 标准终端的基础。 但你真的不想知道这一点。 使用 ncurses 就不需要了。

依靠 conio 不会让你跨平台,因为那是 DOS/Windows 特定的 API。

编辑:显然 ncurses 库本身并不容易构建mingw,至少从谷歌搜索的快速尝试中观察到。 然而,一切并没有丢失,因为 ncurses 只是原始 curses 图书馆。

另一个是 PDCurses,已知它也可以为 Windows 控制台编译和运行至于X11和各种*nix平台。

(我刚刚在维基百科上搜索参考资料时想起,curses 是从编写游戏 rogue 而来的,它是 nethack 的祖先。它的一些代码也是从 vi 编辑器的光标管理模块“借用”的。所以在 nethack 源工具包中寻找想法可能根本不是一个疯狂的想法。 ..)

From this point of view, the console is in many ways just an emulation of a classic terminal device. Curses was created originally to support a way of doing common operations on different terminal types, where the actual terminal in use could be selected by the user as part of the login sequence. That heritage survives today in ncurses.

The ncurses library provides functions to call to directly position the cursor and emit text, and it is known to work for the Windows Console (where CMD.EXE runs), as well as on various *nix platform equivalents such as XTerms and the like. It probably even works with a true Dec VT100 over a serial line if you had such a thing...

The escape sequences understood by the VT100 and later models became the basis for the ANSI standard terminal. But you really don't want to have to know about that. Use ncurses and you won't have to.

Leaning on conio won't get you cross platform, as that is a DOS/Windows specific API.

Edit: Apparently the ncurses library itself is not easily built on mingw, at least as observed from a quick attempt to Google it up. However, all is not lost, as ncurses is only one of the descendants of the original curses library.

Another is PDCurses which is known to compile and run for Windows Consoles, as well as for X11 and a variety of *nix platforms.

(I was just reminded from chasing references at Wikipedia that curses came out of writing the game rogue, which is the ancestor of nethack. Some of its code was "borrowed" from the cursor management module of the vi editor, as well. So spelunking in the nethack source kit for ideas may not be a crazy idea at all...)

酒与心事 2024-07-20 19:11:51

大多数终端窗口都理解 ANSI 转义序列,它允许着色、光标移动等。您可以找到它们的列表 在这里

使用这些序列可能看起来有点“老派”,但是您可以在诅咒不真正适用的情况下使用它们。 例如,我在 bash 脚本中使用以下函数以红色显示错误消息:

color_red()
{
    echo -e "\033[01;31m$1\033[00m"
}

然后您可以这样说:

color_red "something has gone horribly wrong!"
exit 1

Most terminal windows understand the ANSI escape sequences, which allow coloring, cursor movement etc. You can find a list of them here.

Use of these sequences can seem a bit "old school", but you can use them in cases where curses isn't really applicable. For example, I use the folowing function in my bash scripts to display error messages in red:

color_red()
{
    echo -e "\033[01;31m$1\033[00m"
}

You can then say things like:

color_red "something has gone horribly wrong!"
exit 1
送君千里 2024-07-20 19:11:51

不是跨平台,但适用于 Windows / C# 颜色,请参阅

为控制台文本着色 (C#)

c++

Not cross platform but for Windows / C# colour, see

Color your Console text (C#)

c++

女皇必胜 2024-07-20 19:11:51

在 C# 中,您可以分别通过 Console.ForegroundColor 和 Console.BackgroundColor 属性设置文本颜色和背景颜色。 有关有效颜色的列表,请参阅此 MSDN 文档

In C#, you can set the text color and the background color via the Console.ForegroundColor and Console.BackgroundColor properties, respectively. For a list of valid colors, see this MSDN doc.

我ぃ本無心為│何有愛 2024-07-20 19:11:51

您可以使用简单的库,例如 termcol

You can use a simple library like termcol.

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