如何允许用户在终端中的单独应用程序中编辑数据?
我正在编写一个基于终端的应用程序,但我希望用户能够在单独的编辑器中编辑某些文本数据。例如,如果用户选择编辑当前用户名列表,则该列表应在用户最喜欢的编辑器(vim、gedit 等)中作为文本文件打开。这可能是一个环境变量,例如 $MYAPPEDITOR。这类似于 svn 中提交消息的工作方式。
最好的方法是在 /tmp 中创建一个临时文件,并在编辑器进程终止时读取它?或者有更好的方法来解决这个问题吗?
I am writing a terminal-based application, but I want the user to be able to edit certain text data in a separate editor. For example, if the user chooses to edit the list of current usernames, the list should open as a text file in the user's favorite editor (vim, gedit, etc.). This will probably be an environment variable such as $MYAPPEDITOR. This is similar to the way commit messages work in svn.
Is the best way to do this to create a temporary file in /tmp, and read it in when the editor process is terminated? Or is there a better way to approach this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已经有一个
$EDITOR
变量,这是非常标准的,我已经看到它在各种 UNIX 上工作。此外,vi
始终是任何风格的 UNIX 的一个选项。Debian 有一个
sensible-editor
命令,如果可以的话,它会调用$EDITOR
,否则会退回到一些标准命令。 Freedesktop.org 有一个 xdg-open 命令,该命令将检测正在运行的桌面环境并使用关联的应用程序打开文件。据我所知,sensible-editor
在其他发行版上不存在,当然xdg-open
在纯文本环境中会失败,但它不能'如果您认为桌面用户可以看到他们快乐闪亮的gedit
或kate
而不是可怕的旧vi< /code> 或
nano
。 ;)crontab 和 sudoedit 的工作方式也是在 /tmp 中创建一个文件。
git
将其放在 .git 下,而svn
实际上将其放在当前目录(而不是 /tmp)中。There's already a
$EDITOR
variable, which is extremely standard and I have seen it working on a wide variety of unixes. Also,vi
is always an option on any flavor of unix.Debian has a
sensible-editor
command that invokes$EDITOR
if it can, or falls back to some standard ones otherwise. Freedesktop.org has anxdg-open
command that will detect which desktop environment is running and open the file with the associated application. As far as I know,sensible-editor
doesn't exist on other distributions, and of coursexdg-open
will fail in a text-only environment, but it couldn't hurt to try as many options as possible, if you think it's important that a desktop user can see their happy shinygedit
orkate
instead of scary oldvi
ornano
. ;)The way
crontab
andsudoedit
work is also by making a file in /tmp.git
puts it under .git, andsvn
actually puts it in the current directory (not /tmp).svn 和 Mercurial 的方式是在 /tmp 中创建一个文件。
顺便说一句,你不需要 MYAPPEDITOR,在 nix 上已经有 EDITOR 了。
The way svn and mercurial do it is by making a file in /tmp.
BTW, you don't need a MYAPPEDITOR, on nix there's EDITOR already present.
既然您在帖子中提到了 svn,为什么不遵循相同的方法呢?
svn
打开一个具有特定名称的文件,其中包含$EDITOR
(或$SVN_EDITOR
)包含的内容 - 这实际上可能需要您做一些工作;确定每个支持的编辑器的参数。无论哪种情况,您都拥有已保存文件的名称(或者如果出现故障,则为应用程序的错误代码),并且您可以使用它。Since you mention
svn
in your post, why not just follow the same methodology?svn
opens a file with a particular name with whatever$EDITOR
(or$SVN_EDITOR
) contains - this might actually require some work on your part; determining the parameters to each supported editor. In either case, you have the name of the file that was saved (or the error code of the application if something failed) and you can just use that.