在 Java 控制台应用程序中保持输入/输出分离

发布于 2024-09-18 07:02:45 字数 336 浏览 1 评论 0原文

我正在用 Java 编写一个控制台应用程序。它类似于聊天客户端:输入和输出是异步进行的。问题是,如果用户在打字过程中进行某些输出,屏幕上的行将变得混乱。

我正在寻找一种解决方案,允许我将输入区域与输出区域分开。目前我正在使用一个额外的线程来轮询 System.in 上的 BufferedReader。

该程序需要在 Linux 服务器上运行并通过 ssh 会话进行访问。因此,任何仅在这种环境下有效的提示都是可以的。

有没有可以做到这一点的高级库?或者是否有使用终端/ANSI 代码的聪明技巧? ANSI 代码 s(保存光标)和 r(恢复光标)可能会有所帮助,但我如何知道跳转到哪里进行输出以及如何处理滚动?

I am writing a console application in Java. It is similar to a chat client: Input and output are asynchronously made. The problem is that if some output is made while the user is in the middle of typing, the lines will get mixed up on the screen.

I am looking for a solution which allows me to have a input area separate from the output area. At the moment I am using an extra thread which polls a BufferedReader on System.in.

The program needs to run on a Linux server and be accessed via an ssh session. So any hints that only work in this environment are fine.

Are there any high level libraries which can do this? Or is there a smart trick using terminal / ANSI codes? The ANSI codes s (save cursor) and r (restore cursor) might be helpful but how do i know where to jump to do the output and how do i handle scrolling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-09-25 07:02:45

我记得很久以前就使用过类似的东西,但是是在 C++ 中。当时我正在使用 ncurses 库。查看 javacurses ,它似乎是 ncurses 之类的 Java 实现。

I recall a long time ago working with similar things but in C++. I was using the ncurses library then. Check out javacurses which seems to be a Java implementation of something like ncurses.

枯叶蝶 2024-09-25 07:02:45

听起来你需要使用 Curses。 JCurses 是 Curses 库的 Java 实现,可以让您控制终端以允许滚动,定位等

Sounds like you need to use Curses. JCurses is a Java implementation of the Curses library and will give you control of the terminal to allow scrolling, positioning etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文