您如何在终端中编辑现有文本(并在其中移动光标)? [英] How do you edit existing text (and move the cursor around) in the terminal?

查看:97
本文介绍了您如何在终端中编辑现有文本(并在其中移动光标)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经看到过这个演示,它打印出一段文本(就像您键入some-command --help时所得到的一样),然后它跳回到文本中的几个关键字并更改了文本颜色在终端上已经打印出来之后.

I saw this demo once that printed out a paragraph of text (like you'd get when typing some-command --help), and it then jumped back up to a couple keywords in the text and changed the text color, after it was already printed out in the terminal.

在我看来这很疯狂.他们是怎么做到的?

That seems crazy to me. How did they do that?

开始考虑,我猜stdoutstdin从技术上来说是"IO流",所以也许这是一个持久变量,可以跟踪光标的位置?我记得在构建语言解析器时会做类似的事情.

Starting to think about it, I guess stdout and stdin are technically an "IO stream", so maybe that's a persistent variable that keeps track of the position of a cursor? I remember doing something like that when building a language parser.

目标是这样的:说您在控制台中键入以下内容,它会输出一个空白数组,因为在Node.js中,它们都是异步的,我们不想每次都在控制台中编写异步函数:

The goal would be this: say you type the following into the console, and it outputs a blank array because in Node.js, it's all async and we don't want to write the async function everytime in the console:

$ node app.js
> App.User.all()
=> []

然后在执行异步回调时,返回并编辑=> []以包含结果:

Then when the async callback executes, you go back and edit the => [] to include the result:

$ node app.js
> App.User.all()
=> [#<User id:1>, #<User id:2>...]

即使还有很多其他问题需要解决(至少与此问题无关,我至少知道如何实现也很棒).而且我知道您可以定义全局回调并执行类似App.User.all(_c)的操作

That would be awesome to at least know how to implement, even if there are a lot of other issues to work through (unrelated to this question. And I know you can define a global callback and do something like App.User.all(_c)).

在终端输出已打印后,如何编辑?

How do you edit the terminal output after it's already been printed?

推荐答案

最终发现演示":

  • https://github.com/asyncly/cdir/blob/223fe0039fade4fad2bb08c2f7affac3bdcf2f89/cdir.js#L24
  • http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
  • http://ascii-table.com/ansi-escape-sequences-vt-100.php

将光标置于\033[<L>;<C>H\033[<L>;<C>f(将光标置于L行和C列)

Position the Cursor: \033[<L>;<C>H or \033[<L>;<C>f (puts the cursor at line L and column C)

这篇关于您如何在终端中编辑现有文本(并在其中移动光标)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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