在Java中移动命令行接口的打印位置,而不使用外部库 [英] Move printing position of Command Line Interface in Java without using External library

查看:138
本文介绍了在Java中移动命令行接口的打印位置,而不使用外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,我回忆起我可以相对于行和字符位置在命令行界面屏幕上移动不可见的插入符号,这意味着我可以让程序在屏幕上的任何地方打印任何文本。我们在Java中有这样的命令吗?



例如,这里是C中的伪代码:

  int main(){
printf(launching program\\\
);
moveTo(4,3); //移动到屏幕上字符索引3处的第4行。
printf(AAA);
moveTo(3,0); //移动到屏幕上字符索引0处的第3行。
printf(BBB);
moveTo(2,1); //移动到屏幕上字符索引1处的第2行。
printf(CCC);
return 0;
}

这将在命令行界面提供以下输出:

 启动程序

CCC
BBB
AAA



在这种情况下,我们有没有使用任何外部或第三方库的Java中的等效方法?

解决方案

这样做的能力是终端的属性,而不是语言。因此,原则上,如果你连接到一个有足够能力的终端模拟器,那么是的,当然是可能的。



像ncurses这样的库的目的是抽象出来终端依赖的光标移动的血腥细节等。你不需要像ncurses这样的东西,你总是可以直接发出适当的代码为目标终端。



By,在Java中有一个等价的方法,你的意思是有库还可以提供终端不可知的抽象?是(见其他答复)。但是没有什么会使每个主机系统对JVM提供VT100仿真器。例如,在Windows上祝你好运。在这个意义上,Java中的2D图形比终端环境更通用!


In C, I recalled that I can move the invisible caret around the command line interface screen with respect to the line and character position, meaning I can make the program print any text anywhere on the screen. Do we have such command in Java?

For instance, here is a pseudocode in C:

int main(){
    printf("launching program\n");
    moveTo(4,3); //move to line 4 at character index 3 on the screen.
    printf("AAA");
    moveTo(3,0); //move to line 3 at character index 0 on the screen.
    printf("BBB");
    moveTo(2,1); //move to line 2 at character index 1 on the screen.
    printf("CCC");
    return 0;
}

This will give the following output in the command line interface:

launching program

 CCC
BBB
    AAA

Do we have an equivalent method in Java without using any external or third party library in this case?

解决方案

The ability to do this is a property of the terminal, not the language. So in principal, if you're connected to a sufficiently capable terminal emulator, then yes, of course it's possible.

The purpose of a library like ncurses is to abstract away the gory details of terminal-dependent cursor movement, etc. You don't need something like ncurses, you could always just directly emit the appropriate codes for your target terminal.

By, "is there an equivalent method in Java," do you mean are there libraries which also can provide you terminal-agnostic abstractions? Yes (see the other responses). But nothing is going to make every host system to the JVM provide a VT100 emulator. For example, good luck on Windows. In this sense, 2D graphics in Java are more universal than the terminal environment!

这篇关于在Java中移动命令行接口的打印位置,而不使用外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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