【问题标题】:Keeping Scanner Input at the bottom of the console将扫描仪输入保留在控制台底部
【发布时间】:2020-02-22 20:47:42
【问题描述】:

我目前正在为一个打算介绍套接字编程和多线程编程的学校项目编写一个小型 CLI。我们基本上是在实现一种文件传输协议。我目前在格式化 CLI 时遇到了一些问题。由于程序的多线程特性,有时会请求输入,然后显示输出。这使得用户和 CLI 之间的整体交互变得尴尬,因为现在用户必须在输出发生后输入输入。

我想知道是否有一种方法可以通过标准 Java I/O 将输入始终保持在 CLI 的底部,即使有新消息进入也是如此。

例如,当前的输出如下所示:

Connected to 127.0.0.1:5000 and 127.0.0.1:5001
mytftp> pwd
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> pwd &
mytftp> 
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
ls
.DS_Store
sdf
clientFile.txt
mytftp> 

但我期待这样的事情:

Connected to 127.0.0.1:5000 and 127.0.0.1:5001
mytftp> pwd
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> pwd &
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> ls
.DS_Store
sdf
clientFile.txt
mytftp> 

注意:这里的“&”表示“在单独的线程上运行此命令”。这就是为什么在下一次执行期间,首先出现“mytftp>”提示,然后显示线程的结果。我只是希望当前提示始终保持在底部。旁注,pwd 命令上的 & 未添加以提高性能。它通常仅用于文件上传和下载命令,但此处仅用作示例。

我不确定这是否可行,但我们将不胜感激。

重申一下,流程想

Connection Established: 连接到 127.0.0.1:5000 和 127.0.0.1:5001
Command is entered into new thread:mytftp> pwd &
Next prompt is shown ready for a new command:mytftp>

一旦pwd & 的结果出现,我希望提示下来:

Connection Established: 连接到 127.0.0.1:5000 和 127.0.0.1:5001
Command is entered into new thread: mytftp> pwd &
Space was made for the prompt and results shown: /Users/Shawn/Desktop/Computer Science/CSCI4780 分布式系统/Project2/server
Next prompt is shown ready for a new command:mytftp>

【问题讨论】:

    标签: java io java.util.scanner


    【解决方案1】:

    我最终想出的解决方案是使用\r 字符并重新排列提示流。而不是:

    while true:
        show prompt
        get input
        do some socket programming here
    

    我选择使用以下架构

    show prompt
    while true:
        get input
        do some socket programming here and each socket response (system.out) should start with \r, followed by the message, followed by "\nmytftp>".
    

    这意味着来自服务器的每条消息都会删除当前提示,但使用回车,然后将提示添加回它之前。

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 2019-02-20
      • 2016-06-12
      • 2014-06-05
      • 2016-06-20
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多