标准二极管和命令历史记录

发布于 2024-09-09 02:30:27 字数 81 浏览 3 评论 0 原文

在播放和扩展 stdiodemo.py 时, a 想出了添加命令行历史记录的想法。 这可能吗? 有什么提示吗?

谢谢 安东尼斯·K.

While playing and extending stdiodemo.py,
a came up with a thought of adding command line history.
Is this possible?
Any hints?

Thanks
Antonis K.

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

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

发布评论

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

评论(1

缪败 2024-09-16 02:30:27

这当然是可能的。历史记录可以在某种程度上独立于输入进行处理,因此理想情况下,您可以使用 addLinepreviousLine 等方法拥有一个代表历史记录的对象。然后,您可以将其粘贴到您选择的用户界面,无论是 Gtk 应用程序中的输入框还是 stdio 上的其他内容。

作为(未完成的)IRC 客户端的一部分,我写了这样的内容: https://github.com/exarkun/invective/blob/master/invective/history.py

实际上,在同一个项目中,您会发现 LineInputWidget 将其连接到 stdio,并且还实现了 emacs 风格的 Kill 和 yank、forwards- 和 然而,stdiodemo.py 无法处理向上

箭头和向下箭头之类的东西,您可能需要这些来进行合理的历史导航。相反,您需要使用一些知道如何解释终端控制序列的代码在原始模式下处理 stdio。如果您曾经运行过“cat”并按下向上箭头或任何其他功能键,那么您就会知道每个键都有一个特殊的字节序列。你的程序中的某些东西需要解释这些序列并将它们变成有意义的东西。这就是 twisted.conch.insults.insults.ServerProtocol 的作用。它将连接到终端的字节传输转变为一种不同的、更丰富的传输:这种传输可以告诉您字节何时到达,还可以告诉您何时按下各种特殊键。您可以通过运行以下命令来查看运行具有输入历史记录的基于行的协议的示例:

python -m twisted.conch.stdio

这使用 ServerProtocol 和 Twisted 本身中的输入历史记录类之一运行 Python REPL(此 REPL 的特殊之处在于它让反应器在处理输入的同时运行,这在普通的 Python REPL 中是一个挑战)。

您可以在twisted/conch/stdio.py 中找到其源代码。重要的 stdio 连接代码位于 runWithProtocol 类中。了解它如何实例化 ServerProtocol 并使用 StandardIO 将其连接到 stdio(因此它只是在 stdiodemo.py 的基础上构建更多内容)。不过,ServerProtocol 仅解释来自终端的字节。它没有您的应用程序逻辑。因此,您需要给它一个实现您的应用程序逻辑的类。这就是正是 Invective 的作用

It's certainly possible. History can be dealt with somewhat independently of input, so ideally you could have an object representing your history with methods like addLine and previousLine and so on. Then you'd glue this to a user-interface of your choice, be it an input box in a Gtk application or something on stdio.

As part of an (unfinished) IRC client, I've written something like this: https://github.com/exarkun/invective/blob/master/invective/history.py

And actually, in the same project, you'll find LineInputWidget which hooks this up to stdio, and also implements things like emacs-style kill and yank, forwards- and backwards-word, etc.

stdiodemo.py can't handle things like up arrow and down arrow, though, which you'll probably want for sensible history navigation. Instead, you need to handle stdio in raw mode with some code that knows how to interpret terminal control sequences. If you've ever run "cat" and hit up arrow or any other function key, then you know there's a special sequence of bytes for each of these. Something in your program needs to interpret these sequences and turn them into something sensible. This is what twisted.conch.insults.insults.ServerProtocol does. It turns a byte transport connected to a terminal into a different, richer kind of transport: a transport that can tell you when bytes have arrived, but also when various special keys are pressed. You can see an example of running a line-based protocol with input history by running:

python -m twisted.conch.stdio

This runs a Python REPL using ServerProtocol and one of the input history classes in Twisted itself (the special thing about this REPL is that it has the reactor running simultaneously with handling your input, something that's challenge to do in the normal Python REPL).

You can find the source for this in twisted/conch/stdio.py. The important stdio hookup code is in the runWithProtocol class. See how it instantiates a ServerProtocol and connects it to stdio with StandardIO (so it's just building more on top of what stdiodemo.py does). ServerProtocol only interprets the bytes from the terminal, though. It doesn't have your application logic. So you need to give it a class that implements your application logic. And that's exactly what invective does.

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