【问题标题】:Formatting an Input Prompt in Clojure在 Clojure 中格式化输入提示
【发布时间】:2018-10-15 16:25:52
【问题描述】:

我正在尝试在 Clojure 中创建一个简单的输入循环。我们的想法是像这样读取一行文本:

> look
You see nothing, as this game hasn't actually been written.

我用来尝试的方法如下:

(defn get-input []
  (print "> ")
  (string/trim-newline (read-line)))

但是,输入循环看起来像这样:

look
> You see nothing, as this game hasn't actually been written.

如何在用户输入之前而不是之后打印角度引用?

【问题讨论】:

    标签: clojure


    【解决方案1】:

    这是一个缓冲问题。 "> " 只是少量文本,并且不包含换行符(并且由于您没有使用println,因此不会自动添加换行符),因此它会卡在外流缓冲区中。你只需要在printing 之后做一个flush

    当我在多个地方需要这样的print/flush 组合时,我通常会创建一个小辅助函数来整理东西:

    (defn print-fl [& messages]
      (apply print messages) ; Pass the strings to print to be printed
      (flush)) ; Then flush the buffer manually so small strings don't get stuck
    
    (defn get-input []
      (print-fl "> ")
      (string/trim-newline (read-line)))
    
    (get-input)
    > look
    "look"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 2015-03-26
      • 2017-03-06
      • 1970-01-01
      • 2010-12-30
      相关资源
      最近更新 更多