提问人:MrVocabulary 提问时间:11/3/2023 最后编辑:MrVocabulary 更新时间:11/6/2023 访问量:49
将就地更新的多行文本与控制台中的可滚动行混合(进度条)
Mixing multiple lines of text updated in place with scrollable lines in console (progress bar)
问:
我正在尝试实现我自己的进度条解决方案(用于学习和定制目的),但我被卡住了。
期望的行为:我想更新任意数量的行以粘在控制台底部并不断更新它们,但我无法可靠地超过一行并保持其他消息滚动而不出现非典型终端行为(溢出、滚动方向错误或就地更新)。问题本质上是将粘性行(即就地更新)与可滚动行分开设置。
下面是最小的几乎有效的示例(但为了简单起见没有进度条),但它复制了最上面的粘性行:
require 'io/console'
# This function overwrites the two bottom lines and clears the lines before writing.
def overwrite_bottom_two_lines(iteration)
rows, columns = IO.console.winsize
# Move to the second-last line and clear it before writing
print "\e[#{rows - 1};1H\e[2K"
puts "Sticky line 1: iteration #{iteration}"
# Move to the last line and clear it before writing
print "\e[#{rows};1H\e[2K"
puts "Sticky line 2: iteration #{iteration}"
end
# Clears the terminal
print "\e[2J"
# Initialize the bottom two lines
overwrite_bottom_two_lines(0)
# Start the iterations
20.times do |iteration|
# Go back up to right above the bottom two lines and clear the line before writing
rows, columns = IO.console.winsize
print "\e[#{rows - 3};1H\e[2K"
puts "iteration: #{iteration}"
# Overwrite the bottom two lines
overwrite_bottom_two_lines(iteration)
# Sleep for demonstration purposes
sleep(1)
end
如何避免此处出现重复的粘滞行错误?
答: 暂无答案
评论