Linux-可视化控制台光标 [英] Linux - moving the console cursor visual

查看:337
本文介绍了Linux-可视化控制台光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Linux设计一个CLI界面,由于各种原因,我无法使用ncurses .我只使用C ++和Qt框架.

I'm currently designing a CLI interface for linux, and for various reasons I am not able to use ncurses. I am using exclusively C++ and the Qt framework.

因此,为了拥有一个用户友好的界面,我必须在单独的线程中运行此getch循环:

Therefore, in order to have a user-friendly interface, I have to run this getch loop in a separate thread:

https://stackoverflow.com/a/912796/3605689

这基本上意味着我必须自己实现所有基本功能(例如退格键).我已经实现了命令完成和命令历史记录(例如当您在Linux中按tab或uparrow/downarrow时),但是我不知道如何实现leftarrow/rightarrow(也就是通过预输入来查找).

Which basically means I have to implement all basic functionalities (such as backspace) by myself. I have already implemented command completion and command history(like when you press tab or uparrow/downarrow in linux), but I can't figure out how to implement leftarrow/rightarrow (aka seeking through the typeahead).

通常,我是这样实现的:在每个不等于-1的gech上,我都会检查用户是否按下了特殊键(一个以某种方式修改了预输入的键).然后,使用以下功能清除标准输出:

Normally, I implement it like this: upon every gech which is not equal to -1, I check whether the user has pressed a special key (one that modifies the typeahead somehow). I then clear the stdout using the following function:

void inputobject::clear_line(int nletters)
{
    QTextStream(stdout) << "\033[2K";

    for(int i = 0; i < nletters;i++){
        QTextStream(stdout) << "\b";
    }

    rewind(stdout);
}

并用其他替换它,有效地模拟了预输入.例如,在退格的情况下,我将保存命令调用clear_line,然后再次打印该命令,只用少一个字母,其行为便与普通控制台应用程序完全一样.

And replace it with something else, effectively simulating the typeahead. For example, in the case of backspace, I would save the command call clear_line, and print the command out again, just with one less letter, behaving exactly as a normal console application would.

我真正的问题是光标,在向左/向右箭头的情况下,我需要将光标移动到视觉上,以便能够指示用户在文本中的位置:

My real problem is with the cursor, in the case of left/rightarrow, I need to move the cursor visual in order to be able to indicate where in the text is the user seeking:

由于我是如何重写给定的stdout行以模拟提前输入的,因此,只要光标停留在同一行上,光标真正位于的位置就无关紧要-只是视觉效果很重要.如何在Linux上实现视觉上的游标移动?

Because of the nature of how I rewrite the given stdout line to simulate the typeahead, it does not really matter where the cursor REALLY is, as long as it stays on the same line - it is just the visual that matters. How can I achieve moving the cursor visual on linux?

推荐答案

答案由Evilruff在评论中提供:

The answer was provided in the comment by Evilruff:

光标移动

ANSI转义序列允许您随意在屏幕上移动光标.这对于由Shell脚本生成的全屏用户界面更为有用,但也可以在提示中使用.运动逃逸顺序如下:

ANSI escape sequences allow you to move the cursor around the screen at will. This is more useful for full screen user interfaces generated by shell scripts, but can also be used in prompts. The movement escape sequences are as follows:

  • 定位光标: \ 033 [; H 或者 \ 033 [L; Cf 将光标置于L行和C列.
  • 将光标向上移动N行: \ 033 [NA
  • 将光标向下移动N行: \ 033 [NB
  • 将光标向前移动N列: \ 033 [NC
  • 向后移动光标N列: \ 033 [ND

  • Position the Cursor: \033[;H Or \033[L;Cf puts the cursor at line L and column C.
  • Move the cursor up N lines: \033[NA
  • Move the cursor down N lines: \033[NB
  • Move the cursor forward N columns: \033[NC
  • Move the cursor backward N columns: \033[ND

清除屏幕,移至(0,0): \ 033 [2J

Clear the screen, move to (0,0): \033[2J

擦除到行尾: \ 033 [K

Erase to end of line: \033[K

保存光标位置: \ 033 [s

Save cursor position: \033[s

这篇关于Linux-可视化控制台光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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