clojure - 在 Clojure 中格式化输入提示
问题描述
我正在尝试在 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.
如何在用户输入之前而不是之后打印角度报价?
解决方案
这是一个缓冲问题。"> "
只是少量的文本,并且不包含换行符(并且由于您没有使用,因此不会自动添加换行符println
),因此它会卡在 outstream 缓冲区中。你只需要做一个flush
after print
ing。
当我在多个地方需要这样的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"
推荐阅读
- keycloak - 使用 ORCiD 作为具有 keycloak 的身份提供者(每个人都得到错误:数值 (2225265999) 超出 int 范围)
- angular - 停用用户帐户时如何在登录页面中显示消息?
- javascript - 如何不打印多次?
- javascript - 搜索使用 javascript 对象生成的 HTML 表
- vue.js - 多个自定义复选框组件默认选中状态vuex问题
- javascript - Javascript 创建动态 html 输入在表单提交后不保留
- javascript - ajax表单提交jquery 5秒后重定向
- python - TypeError:Django 社交登录中的格式字符串参数不足
- react-native - 添加 Crashlytics 后,在构建过程中在 Android 中重复资源 com_crashlytics_export_strings.xml
- laravel - 如何将现有图像移动到 laravel 6 中的存储中?