从Ruby获取终端位置
绝对有更好的方法来做到这一点从Ruby获取终端位置,ruby,terminal,terminfo,Ruby,Terminal,Terminfo,绝对有更好的方法来做到这一点 temp_file ||= Tempfile.new() system("stty -echo; tput u7; read -d R x; stty echo; echo ${x#??} > #{temp_file.path}") temp_file.gets.chomp.split(';').map(&:to_i) 基本上,我在子进程中运行bash脚本,然后从重定向文件读取输出 如果不使用C或任何gems(stdlib好的),有什么更好的方法可以
temp_file ||= Tempfile.new()
system("stty -echo; tput u7; read -d R x; stty echo; echo ${x#??} > #{temp_file.path}")
temp_file.gets.chomp.split(';').map(&:to_i)
基本上,我在子进程中运行bash脚本,然后从重定向文件读取输出
如果不使用C或任何gems(stdlib好的),有什么更好的方法可以做到这一点?交叉兼容性不是很重要。在stdlib中,但它很混乱。
你可以用Ruby试试这个。它似乎具有您需要的curX和curY功能。下面是一个获取光标位置的纯ruby实现:
require 'io/console'
class Cursor
class << self
def pos
res = ''
$stdin.raw do |stdin|
$stdout << "\e[6n"
$stdout.flush
while (c = stdin.getc) != 'R'
res << c if c
end
end
m = res.match /(?<row>\d+);(?<column>\d+)/
{ row: Integer(m[:row]), column: Integer(m[:column]) }
end
end
end
puts Cursor.pos #=> {:row=>25, :column=>1}
需要“io/控制台”
类游标
类别1}
tput u7
被替换为回声\e[6n
到$stdout
。它可能不太便于携带,但有助于我们只使用ruby代码。它是用于多行“状态栏”类型的东西,所以我尝试不使用完整的诅咒库。我可以不用它来完成这项工作,只是。太难看了。