如何创建支持ANSI转义码序列的可滚动控制台应用程序 [英] How to create scrollable console application that support ANSI escape code sequences

查看:137
本文介绍了如何创建支持ANSI转义码序列的可滚动控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此根据我所知道的知识对技术进行一些假设,但也欢迎其他技术建议.

我的目标:编写一个 ANSI Art 查看器,该查看器尽可能类似于在DOS上查看机器,最好没有运行dosbox的开销.这将在Raspberry Pi上运行.

我已经安装好控制台,以正确地用适当的字符,颜色等标记ANSI.查看器"的好处是我希望能够使用箭头键在文档中上下滚动,就像少"命令一样.

从我的研究能力来看,诅咒是一个完美的选择.问题是curses不支持ANSI转义码序列.有一个使用C ++编写的ANSI编辑器,它使用curses,但是它为解析转义码序列建立了自己的支持.现在,这是我的不得已.

所以我的问题是:是否有更好的方法来创建可滚动的控制台模式应用程序,以在Linux上的python中查看ANSI Art(代码页437 + ANSI转义代码序列)?

解决方案

实际上只有两种可能性:将ANSI序列解析为curses可以接受的内容,或者按原样使用ANSI序列.

起初,后者似乎更具吸引力.大多数限制要么与您无关,要么很容易解决:

  • 它仅适用于静态ANSI艺术作品,不适用于动画文件.这是很合理的,因为向上滚动"动画文件没有多大意义. (您当然可以将其动态渲染到画布上,然后在其中快速滚动一个窗口,但是一旦您开始考虑渲染和窗口化的含义是什么...就在解析ANSI艺术.)但这听起来像您只需要静态ANSI艺术.
  • 仅当您的终端与ANSI兼容(足够接近)时才可以使用...,但是(可以这样做)或者您的cat命令不起作用. (您可能仍然对颜色设置有疑问,但我想您也知道如何解决该问题.)
  • 仅在您的终端是cp437时才有效,这可能是一个更大的问题……但这很难解决;只需decode('cp437'),然后在您的代码中进行适当编码即可;转义序列将保持不变.
  • 您可能想要原始的键盘输入.但这就像tty.setraw(sys.stdin.fileno())一样简单,只需将stdin读取为未缓冲的文件即可. (好吧,您可能想隐藏原始的tcgetattr,以便以后可以恢复它,但这并不难.)
  • 您必须自己解析键盘输入转义序列.通常,这是很多工作……但是处理与ANSI艺术兼容的终端的向上和向下箭头很容易.
  • 您将必须知道如何将ANS文件映射到实际行.

最后一个听起来很简单,但事实并非如此.例如,我抓取了一个随机文件 GR-BANT ;它只有33行,但其中有154条换行符.这将是非常普遍的.在许多情况下,以esc-[-A开头的只是重叠线",您必须将其视为上一行的一部分,这并不难做到,但是在很多情况下,都需要不仅如此.

因此,无论如何,您都必须至少进行一些 ANSI解析.

一旦开始,我想您将通过完全解析并手动绘制到诅咒板上的最后一招"找到更轻松的时间. (当然,这样做的副作用是可以处理动画文件,在非ANSI终端上工作,更轻松地在所有终端上处理小键盘"键……)

但是,如果您想采用第二种方法,请此处是可以帮助您入门的快速技巧./p>

I am making some assumptions here on technology based on what I know, but other technology recommendations are welcome.

My goal: Write an ANSI Art viewer that as closely as possible resembles viewing on a DOS machine as possible, preferably without the overhead of running dosbox. This will run on a Raspberry Pi.

I have gotten my console to properly cat an ANSI with proper characters, colors, etc. The catch with the "viewer" is that I would like to be able to use the arrow keys to scroll up and down through the document, much like, say, the "less" command does.

From what I have been able to research, curses is a perfect candidate for this. The problem is that curses does not support ANSI escape code sequences. There is an ANSI editor written in C++ that uses curses, but it builds its own support for parsing the escape code sequences. Right now this is my last resort.

So my question is: Is there a better route to creating a scrollable console-mode application for viewing ANSI Art (Code Page 437 + ANSI escape code sequences) in python on linux?

解决方案

There are really only two possibilities: Parse the ANSI sequences into something curses can accept, or use the ANSI sequences as-is.

At first, the latter may seem more attractive. Most of the limitations are either irrelevant to you, or easy to deal with:

  • It only works for static ANSI art, not animated files. Which is pretty reasonable, because it wouldn't make much sense to "scroll up" in an animated file. (You could of course render it on the fly to a canvas and then scroll a window up and down within that, but once you start thinking about what that rendering and windowing means… you're parsing ANSI art.) But it sounds like you only need static ANSI art.
  • It only works if your terminal is (close enough to) ANSI compatible… but it is (or can be made so) or your cat command wouldn't work. (You may still have a problem with the color settings, but I assume you know how to work around that too.)
  • It only works if your terminal is cp437, which may be more of a problem… but that's trivial to work around; just decode('cp437') then encode as appropriately in your code; the escape sequences are going to pass through unchanged.
  • You probably want raw keyboard input. But this is as easy as tty.setraw(sys.stdin.fileno()) and just reading stdin as an unbuffered file. (Well, you may want to stash the original tcgetattr so you can restore it later, but that's not much harder.)
  • You'll have to parse keyboard input escape sequences yourself. This is a lot of work to do generally… but just handling the up and down arrows for ANSI-art-compatible terminals is easy.
  • You'll have to know how to map the ANS file to actual lines.

That last one sounds like the easy part, but it's not. For example, I grabbed a random file, GR-BANT; it's only 33 lines long, but it's got 154 newlines in it. And that's going to be pretty common. In many cases, it's just going to be "overlay lines" that start with esc-[-A, that you have to treat as part of the previous line, which is not hard to do, but there will be plenty of cases that require something more than that.

So, you're going to have to do at least some ANSI parsing, no matter what.

And once you start on that, I think you'll find an easier time with your "last resort" of doing a full parse and drawing manually to a curses pad. (And of course this has the side effects of making it possible to handle animated files, working on non-ANSI terminals, handling "keypad" keys more easily and on all terminals, …)

But if you want to go the second way, here is a quick hack that should get you started.

这篇关于如何创建支持ANSI转义码序列的可滚动控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆