[英]Refresh first N lines and reset cursor to the end of current line with escape sequences?
Here's the situation, I want to make some terminal interaction, I want to start child thread to refresh the first N lines, and use the main thread to handle user input. 这是情况,我想做一些终端交互,我想启动子线程来刷新前N行,并使用主线程来处理用户输入。
After that the program will print changeable string, maybe some logs. 之后,程序将打印可更改的字符串,也许是一些日志。
The child thread like this: 这样的子线程如下:
let mut count: i32 = 0;
loop {
println!("\x1B[2F\x1B[2KHi user1, count:{}\n", count);
count += 1;
let ten_millis = time::Duration::from_millis(1000);
thread::sleep(ten_millis);
}
eg: 例如:
----------------
Hi user1, count: 0
Input: 1+1
Output: 2
----------------
The refresher code works well, but the cursor will reset to the start of line, and I want to move it always to the end of the last line. 刷新代码运行良好,但光标将重置为行的开头,我想将它始终移动到最后一行的末尾。 How can I do this trick? 我怎么能这样做?
Any help would be great appreciated. 任何帮助将非常感谢。
When it boils down to moving the cursor around, you might be interested in simple ANSI escape Sequences : 当归结为移动光标时,您可能对简单的ANSI转义序列感兴趣:
ANSI escape sequences allow you to move the cursor around the screen at will. ANSI转义序列允许您随意在屏幕上移动光标。 This is more useful for full screen user interfaces generated by shell scripts, but can also be used in prompts. 这对于shell脚本生成的全屏用户界面更有用,但也可以在提示中使用。 The movement escape sequences are as follows: 运动转义序列如下:
- Position the Cursor:
\\033[<L>;<C>H
or\\033[<L>;<C>f
puts the cursor at line L and column C. 定位光标:\\033[<L>;<C>H
或\\033[<L>;<C>f
将光标置于L行和C列。- Move the cursor up N lines:
\\033[<N>A
将光标向上移动N行:\\033[<N>A
- Move the cursor down N lines:
\\033[<N>B
将光标向下移动N行:\\033[<N>B
- Move the cursor forward N columns:
\\033[<N>C
将光标向前移动N列:\\033[<N>C
- Move the cursor backward N columns:
\\033[<N>D
向后移动光标N列:\\033[<N>D
- Clear the screen, move to (0,0):
\\033[2J
清除屏幕,移至(0,0):\\033[2J
- Erase to end of line:
\\033[K
擦除到行尾:\\033[K
- Save cursor position:
\\033[s
保存光标位置:\\033[s
- Restore cursor position:
\\033[u
恢复光标位置:\\033[u
source: Bash Prompt HOWTO: Cursor movement source: Bash Prompt HOWTO:光标移动
While these ANSI escape sequences work very nicely, you might, from time to time be interested in the usage of tput
as it gives you more readablilty of your scripts. 虽然这些ANSI转义序列的工作非常好,但您可能会不时对tput
的使用感兴趣,因为它可以让您更加轻松地阅读脚本。 Explaining tput
here would be a bit overkill, but the above commands can be done as: 解释tput
位置会有点矫枉过正,但上面的命令可以做到:
- Position the Cursor:
tput cup <L> <C>
puts the cursor at line L and column C. 定位光标:tput cup <L> <C>
将光标放在第L行和第C列。- Move the cursor up N lines:
tput cuu <N>
将光标向上移动N行:tput cuu <N>
- Move the cursor down N lines:
tput cud <N>
将光标向下移动N行:tput cud <N>
- Move the cursor forward N columns:
tput cuf <N>
向前移动光标N列:tput cuf <N>
- Move the cursor backward N columns:
tput cub <N>
向后移动光标N列:tput cub <N>
- Clear the screen, move to (0,0):
tput clear
清除屏幕,移至(0,0):输入tput clear
- Erase to end of line:
tput el
删除到行尾:tput el
- Save cursor position:
tput sc
保存光标位置:tput sc
- Restore cursor position:
tput rc
恢复光标位置:tput rc
There are many many many other options available. 还有许多其他选择。 See 看到
man tput
man 5 terminfo
You could make the child "know" that input is in progress, and in that case, follow the println!
您可以让孩子“知道”输入正在进行中,在这种情况下,请按照println!
\\x1B[B
) or next-line ( \\x1B[E
), 附加光标向下(即\\x1B[B
]或下一行( \\x1B[E
), \\x1B[C
with the number of columns as a parameter before the C
). 然后使用提示的长度加上当前输入的长度,例如光标向前(即, \\x1B[C
,其中列数作为C
之前的参数),在该行中移动。 Something like 就像是
if (input_length) {
cursor_down();
cursor_forward(prompt_length + input_length);
}
While not quite answering your question, the standard bash way to do this is watch
虽然没有完全回答你的问题,但执行此操作的标准bash方法是watch
For example: 例如:
watch df -h
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.