python 如何制作命令行文本编辑器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/688302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:38:19  来源:igfitidea点击:

How do I make a command line text editor?

pythontext-editortui

提问by So8res

I have gotten to know my way around a few programming languages, and I'd like to try my hand at making a command-line text editor -- something that runs in the terminal, like vim/emacs/nano, but is pure text (no guis, please). Preferably, I'd like to do this in python. Where do I start? Are there any (python) libraries to do command-line applications?

我已经了解了几种编程语言,我想尝试制作一个命令行文本编辑器——在终端中运行的东西,比如 vim/emacs/nano,但是是纯文本(没有 guis,请)。最好,我想在 python 中做到这一点。我从哪里开始?是否有任何(python)库来执行命令行应用程序?

回答by linjunhalida

try python cursesmodule , it is a command-line graphic operation library.

试试python的curses模块,它是一个命令行图形操作库。

回答by Jim Blizard

Take a look at Curses Programming in Pythonand thisas well.

看看Python中的Curses 编程,还有这个

回答by S.Lott

Kids today! Sheesh! When I was starting out, curses was not in widespread use!

孩子们今天!嘘!当我刚开始时,curses 并没有被广泛使用!

My first text editors worked on actual mechanical Teletype devices with actual paper (not a philosophical "TTY" device with a scrolling screen!)

我的第一个文本编辑器使用实际的纸张在实际的机械电传设备上工作(不是带有滚动屏幕的哲学“TTY”设备!)

This still works nicely as a way to edit.

这仍然可以很好地作为一种编辑方式。

Use the cmdmodule to implement a bunch of commands. Use the 'ex' man page for hints as to what you need. Do not read about the vi commands; avoid reading about vim.

使用cmd模块来实现一堆命令。使用“ex”手册页获取有关您需要的提示。不要阅读 vi 命令;避免阅读有关 vim 的内容。

Look at older man pages for just the "EX COMMANDS" section. For example, here: http://www.manpagez.com/man/1/ex/.

查看旧手册页中的“EX 命令”部分。例如,这里:http: //www.manpagez.com/man/1/ex/

Implement the append, add, change, delete, global, insert, join, list, move, print, quit, substitute and write commands and you'll be happy.

实现追加、添加、更改、删​​除、全局、插入、加入、列出、移动、打印、退出、替换和写入命令,你会很高兴的。

回答by Ignacio Vazquez-Abrams

Another option if you want to write a TUI (Text User Interface) without having to descend to curses is Snack, which comes with Newt.

如果您想编写 TUI(文本用户界面)而不必下降到 curses ,另一个选择是Snack,它与Newt 一起提供

回答by Adam Davis

Curses type libraries and resources will get you into the textual user interfaces, and provide very nice, relatively easy to use windows, menus, editors, etc.

Curses 类型库和资源将使您进入文本用户界面,并提供非常漂亮、相对易于使用的窗口、菜单、编辑器等。

Then you'll want to look into code highlighting modules for python.

然后,您将需要查看 Python 的代码突出显示模块。

It's a fun process dealing with the limitations of textual interfaces, and you can learn a lot by going down this road. Good luck!

这是一个处理文本界面局限性的有趣过程,沿着这条路走下去,您可以学到很多东西。祝你好运!

-Adam

-亚当

回答by vezult

I would recommend the excellent urwid toolkit (http://excess.org/article/2009/03/urwid-0984-released) - it's much easier to use than straight curses.

我会推荐优秀的 urwid 工具包 ( http://excess.org/article/2009/03/urwid-0984-released) - 它比直接诅咒更容易使用。

回答by dmckee --- ex-moderator kitten

A not very serious suggestions: a line editorcan be implemented without curses.

一个不是很严肃的建议:行编辑器可以在没有诅咒的情况下实现。

These things are pretty primitive, of course, and not a lot of fun to work in. But they can be implemented with very little code, and would give you a chance to fool around with various schemes for maintaining the file state in memory pretty quickly.

当然,这些东西非常原始,工作起来并没有多少乐趣。但是它们可以用很少的代码实现,并且可以让您有机会使用各种方案来快速维护内存中的文件状态.

And they would put you in touch with the programmers of the early seventies (when they had teletypes and the first glass teletypes, but after punched cards were a bit passe...).

他们会让你接触 70 年代初的程序员(当时他们有电传打字机和第一台玻璃电传打字机,但打孔后有点过时了......)。

回答by sigjuice

Not quite a reference to a Python library, but The Craft of Text Editingby Craig A. Finseth might be of interest you.

不完全是对 Python 库的引用,但Craig A. Finseth 的The Craft of Text Editing可能会让您感兴趣。

回答by David Z

Well, what do you mean by a GUI? If you just want to create something that can be used on a console, look into the cursesmodule in the Python standard library, which allows you to simulate a primitive GUI of sorts on a console.

那么,GUI 是什么意思?如果您只想创建可以在控制台上使用的东西,请查看cursesPython 标准库中的模块,它允许您在控制台上模拟各种原始 GUI。

回答by Renato Aquino

Another option without curses is Python Slang

另一个没有诅咒的选择是Python Slang

[Newt](http://en.wikipedia.org/wiki/Newt_(programming_library)) is written on top of slang.

[Newt]( http://en.wikipedia.org/wiki/Newt_(programming_library)) 写在俚语之上。